我在将 ModelMap 属性传递给 tsp 页面时遇到问题。 Intelij IDEA 可以很好地识别传递的变量,但是当我在 tomcat 上部署应用程序并加载实际页面时,我只能看到变量名,而不是变量值。尝试将一些数据打印到 .jsp View 。

web.xml:

    <!DOCTYPE web-app PUBLIC 
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
        "http://java.sun.com/dtd/web-app_2_3.dtd" > 
 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <display-name>Orange Home Web Application</display-name> 
 
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
        <param-name>contextConfigLocation</param-name> 
        <param-value> 
            /WEB-INF/mvc-dispatcher-servlet.xml 
            /WEB-INF/spring/root-context.xml 
            /WEB-INF/spring/application-security.xml 
        </param-value> 
    </context-param> 
 
    <filter-mapping> 
        <filter-name>springSecurityFilterChain</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <filter> 
        <filter-name>springSecurityFilterChain</filter-name> 
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
 
    <!-- Processes application requests --> 
    <servlet> 
        <servlet-name>mvc-dispatcher</servlet-name> 
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
 
    <servlet-mapping> 
        <servlet-name>mvc-dispatcher</servlet-name> 
        <url-pattern>/</url-pattern> 
    </servlet-mapping> 
 
    <!-- Creates the Spring Container shared by all Servlets and Filters --> 
    <listener> 
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
 
    <listener> 
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
    </listener> 
 
</web-app> 

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/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>com.boro</groupId> 
<artifactId>orange</artifactId> 
<packaging>war</packaging> 
<version>1.0-SNAPSHOT</version> 
<name>orange Maven Webapp</name> 
<url>http://maven.apache.org</url> 
 
<!-- Properties definitions --> 
<properties> 
    <org.springframework.version>4.0.9.RELEASE</org.springframework.version> 
    <org.springframework.security.version>3.2.3.RELEASE</org.springframework.security.version> 
    <org.apache.tiles.version>3.0.3</org.apache.tiles.version> 
</properties> 
 
<!-- Project dependencies --> 
<dependencies> 
    <dependency> 
        <groupId>junit</groupId> 
        <artifactId>junit</artifactId> 
        <version>3.8.1</version> 
        <scope>test</scope> 
    </dependency> 
    <!-- Spring --> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-core</artifactId> 
        <version>${org.springframework.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-web</artifactId> 
        <version>${org.springframework.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-webmvc</artifactId> 
        <version>${org.springframework.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-orm</artifactId> 
        <version>${org.springframework.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-tx</artifactId> 
        <version>${org.springframework.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-context</artifactId> 
        <version>${org.springframework.version}</version> 
    </dependency> 
    <!-- Spring Security --> 
    <dependency> 
        <groupId>org.springframework.security</groupId> 
        <artifactId>spring-security-web</artifactId> 
        <version>${org.springframework.security.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.security</groupId> 
        <artifactId>spring-security-taglibs</artifactId> 
        <version>${org.springframework.security.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.security</groupId> 
        <artifactId>spring-security-config</artifactId> 
        <version>${org.springframework.security.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.security</groupId> 
        <artifactId>spring-security-crypto</artifactId> 
        <version>${org.springframework.security.version}</version> 
    </dependency> 
    <!-- Hibernate --> 
    <dependency> 
        <groupId>org.hibernate</groupId> 
        <artifactId>hibernate-core</artifactId> 
        <version>4.3.5.Final</version> 
    </dependency> 
    <!-- Java Servlet and JSP dependencies --> 
    <dependency> 
        <groupId>javax.servlet</groupId> 
        <artifactId>servlet-api</artifactId> 
        <version>2.5</version> 
    </dependency> 
    <dependency> 
        <groupId>javax.servlet.jsp</groupId> 
        <artifactId>jsp-api</artifactId> 
        <version>2.1</version> 
        <scope>provided</scope> 
    </dependency> 
    <!-- JSTL dependency --> 
    <dependency> 
        <groupId>jstl</groupId> 
        <artifactId>jstl</artifactId> 
        <version>1.2</version> 
    </dependency> 
    <!-- Apache Commons DBCP dependency --> 
    <dependency> 
        <groupId>org.apache.commons</groupId> 
        <artifactId>commons-dbcp2</artifactId> 
        <version>2.0</version> 
    </dependency> 
    <!-- MySQL Connector Java dependency --> 
    <dependency> 
        <groupId>mysql</groupId> 
        <artifactId>mysql-connector-java</artifactId> 
        <version>5.1.30</version> 
    </dependency> 
    <!-- Logger --> 
    <dependency> 
        <groupId>org.apache.logging.log4j</groupId> 
        <artifactId>log4j-api</artifactId> 
        <version>2.2</version> 
    </dependency> 
    <dependency> 
        <groupId>org.apache.logging.log4j</groupId> 
        <artifactId>log4j-core</artifactId> 
        <version>2.2</version> 
    </dependency> 
    <!-- Apache tiles dependencies --> 
    <dependency> 
        <groupId>org.apache.tiles</groupId> 
        <artifactId>tiles-api</artifactId> 
        <version>${org.apache.tiles.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.apache.tiles</groupId> 
        <artifactId>tiles-core</artifactId> 
        <version>${org.apache.tiles.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.apache.tiles</groupId> 
        <artifactId>tiles-jsp</artifactId> 
        <version>${org.apache.tiles.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.apache.tiles</groupId> 
        <artifactId>tiles-servlet</artifactId> 
        <version>${org.apache.tiles.version}</version> 
    </dependency> 
    <dependency> 
        <groupId>org.apache.tiles</groupId> 
        <artifactId>tiles-template</artifactId> 
        <version>${org.apache.tiles.version}</version> 
    </dependency> 
    <!-- Logger tool --> 
    <dependency> 
        <groupId>log4j</groupId> 
        <artifactId>log4j</artifactId> 
        <version>1.2.16</version> 
    </dependency> 
    <dependency> 
        <groupId>net.sourceforge.htmlunit</groupId> 
        <artifactId>htmlunit</artifactId> 
        <version>2.15</version> 
    </dependency> 
    <dependency> 
        <groupId>xml-apis</groupId> 
        <artifactId>xml-apis</artifactId> 
        <version>1.4.01</version> 
    </dependency> 
 
    <dependency> 
        <groupId>org.slf4j</groupId> 
        <artifactId>slf4j-log4j12</artifactId> 
        <version>1.5.8</version> 
    </dependency> 
</dependencies> 
<build> 
    <finalName>orange</finalName> 
</build> 

这是我的 Controller :

@RequestMapping(value = "/", method = RequestMethod.GET) 
public String home(ModelMap model) { 
 
    model.addAttribute("hello", "Hello"); 
    log.info("Loading home.jsp"); 
 
    return "home"; 
} 

这里是 home.jsp 页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<c:set var="contextPath" value="${pageContext.request.contextPath}"/> 
<div class="content-wrapper"> 
    <!-- Main content --> 
    <section class="content"> 
        <h1> 
            Parameter: ${hello} 
            Parameter: <c:out value="${hello}"/> 
        </h1> 
    </section> 
</div> 

加载主页时,我可以看到变量名而不是值: 参数:${hello} 参数:${hello}

请您参考如下方法:

<!DOCTYPE web-app PUBLIC 
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd" > 
 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 

当您查看标题时,您会注意到 <!DOCYPE指向 Web 应用程序 2.3 的真正旧版本的行,这会禁用 EL。要修复,只需删除该行即可。

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
      version="3.0"> 

这应该可以解决您的问题。


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!