我如何将字符串从spring控制器传递给我的jsp

时间:2017-07-25 02:26:24

标签: spring jsp spring-mvc annotations

我的控制器包含以下代码

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(Model model) {
model.addAttribute("msg", "Hello world buddy");
return "helloworld";
}

我的jsp页面为

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib uri="http://www.springframework.org/tags/form"
prefix="springForm"%>
<!DOCTYPE html">
<html>
<head>
<title>Spring MVC -HelloWorld</title>
</head>
<body>
<springForm:form name="hello" commandName="command" method="GET"
    action="/hello">
    <c:out value='${msg}' />
</springForm:form>
</body>
</html>

但我得到输出

${msg}

我希望输出“Hello world buddy”

3 个答案:

答案 0 :(得分:1)

它也发生在我身上,在我的情况下我的解决方案是纠正pom.xml,只是验证你使用的是正确版本的jstl。这是我使用的pom.xml:

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.timbuchalka</groupId>
    <artifactId>spring-mvc-demo-1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
</project>
  1. 清理所有maven存储库

  2. 从maven重新加载所有依赖项,如果你正在使用eclipse,请点击项目选择maven并更新项目。

  3. 我使用的pom.xml示例可能没有您正在使用的所有依赖项,但它可以让您知道您的jstl依赖项是否正确。

  4. 还有其他与此问题相关的帖子,这取决于很多原因,在我的情况下只是maven依赖。

  5. 相关问题:

    JSTL c:out not showing the variable's value

    spring-mvc-not-getting-value-inside-jsp-view

答案 1 :(得分:1)

很长一段时间发生在我身上,问题出在servlet版本上 如果你使用xml类型的应用程序初始化程序aka web.xml然后检查servlet版本,请参考下面的代码片段... web-app xmlns =“http://java.sun.com/xml/ns/j2ee”......版本=“使用更高版本可能3.0 +”

答案 2 :(得分:0)

在jsp页面上添加它。

<%@ page isELIgnored="false" %>