【问题标题】:how can i pass the string from spring controller to my jsp我如何将字符串从弹簧控制器传递到我的jsp
【发布时间】:2017-07-25 02:26:23
【问题描述】:

我的控制器包含以下代码

@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”

【问题讨论】:

    标签: spring jsp spring-mvc annotations


    【解决方案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 依赖。

    相关问题:

    JSTL c:out not showing the variable's value

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

    【讨论】:

      【解决方案2】:

      很久以前发生在我身上,问题在于 servlet 版本 如果您使用 xml 类型的应用程序初始化程序又名 web.xml 然后检查 servlet 版本,请参阅下面的代码 sn-p ... web-app xmlns="http://java.sun.com/xml/ns/j2ee" ...... version="使用更高版本大概3.0+"

      【讨论】:

        【解决方案3】:

        在jsp页面的顶部添加这个。

        <%@ page isELIgnored="false" %>
        

        【讨论】:

          猜你喜欢
          • 2014-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多