Nothing printed on jsp for added model object

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

I am getting a strange behavior below: I am adding a model object in Spring Controller, when I access the same using jstl in jsp, I get nothing over there.

My project is REST based built on Java 1.8+ Tomcat 8.0+ Maven.

My maven project is named reporting having modules reporting-rest and reporting-dao. Jsp pages and controllers are located in reporting-rest.

pom. xml content in reporting-rest

<?xml version="1.0"?> <project     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"     xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">     <modelVersion>4.0.0</modelVersion>     <parent>         <groupId>org.reporting</groupId>         <artifactId>reporting</artifactId>         <version>0.0.1-SNAPSHOT</version>     </parent>     <artifactId>reporting-rest</artifactId>     <packaging>war</packaging>     <properties>         <servlet.version>2.5</servlet.version>          <spring.version>3.2.4.RELEASE</spring.version>         <log4j.version>1.2.17</log4j.version>          <jdk.version>1.7</jdk.version>         <context.path>SpringRestSecurityOauth</context.path>         <spring.security.version>3.2.5.RELEASE</spring.security.version>     </properties>     <name>reporting-rest Maven Webapp</name>     <url>http://maven.apache.org</url>     <dependencies>      <!--    <dependency>             <groupId>org.apache.commons</groupId>             <artifactId>commons-io</artifactId>             <version>1.3.2</version>         </dependency> -->         <dependency>             <groupId>commons-io</groupId>             <artifactId>commons-io</artifactId>             <version>2.4</version>         </dependency>          <!-- log4j -->         <dependency>             <groupId>log4j</groupId>             <artifactId>log4j</artifactId>             <version>${log4j.version}</version>         </dependency>         <dependency>             <groupId>cglib</groupId>             <artifactId>cglib</artifactId>             <version>2.2.2</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-core</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-web</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-webmvc</artifactId>             <version>${spring.version}</version>         </dependency>          <!-- Spring Security -->         <dependency>             <groupId>org.springframework.security</groupId>             <artifactId>spring-security-web</artifactId>             <version>${spring.security.version}</version>         </dependency>         <dependency>             <groupId>org.springframework.security</groupId>             <artifactId>spring-security-config</artifactId>             <version>${spring.security.version}</version>         </dependency>         <dependency>             <groupId>org.springframework.security.oauth</groupId>             <artifactId>spring-security-oauth2</artifactId>             <version>1.0.0.RELEASE</version>         </dependency>         <dependency>             <groupId>com.google.code.gson</groupId>             <artifactId>gson</artifactId>             <version>2.2.2</version>         </dependency>         <dependency>             <groupId>org.codehaus.jackson</groupId>             <artifactId>jackson-mapper-asl</artifactId>             <version>1.9.10</version>         </dependency>         <dependency>             <groupId>commons-httpclient</groupId>             <artifactId>commons-httpclient</artifactId>             <version>3.1</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-context-support</artifactId>             <version>${spring.version}</version>         </dependency>         <dependency>             <groupId>javax.servlet</groupId>             <artifactId>javax.servlet-api</artifactId>             <version>3.0.1</version>             <scope>provided</scope>         </dependency>         <dependency>         <groupId>jstl</groupId>             <artifactId>jstl</artifactId>             <version>1.2</version>         </dependency>         <dependency>             <groupId>org.reporting.dao</groupId>             <artifactId>reporting-dao</artifactId>             <version>0.0.1-SNAPSHOT</version>         </dependency>     </dependencies>     <build>         <finalName>reporting-rest</finalName>                 <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-eclipse-plugin</artifactId>                 <version>2.9</version>                 <configuration>                     <downloadSources>true</downloadSources>                     <downloadJavadocs>false</downloadJavadocs>                     <wtpversion>2.0</wtpversion>                 </configuration>             </plugin>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <version>2.3.2</version>                 <configuration>                     <source>${jdk.version}</source>                     <target>${jdk.version}</target>                 </configuration>             </plugin>              </plugins>     </build>  </project> 

pom.xml content in reporting (parent)

<?xml version="1.0" encoding="UTF-8"?> <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>org.reporting</groupId>     <artifactId>reporting</artifactId>     <version>0.0.1-SNAPSHOT</version>     <packaging>pom</packaging>     <properties>          <org.springframework.version>3.2.4.RELEASE</org.springframework.version>          <org.slf4j-version>1.7.5</org.slf4j-version>         <org.json.version>20080701</org.json.version>         <jackson-mapper.version>1.9.12</jackson-mapper.version>         <jsp.version>2.2</jsp.version>          <junit.version>4.11</junit.version>         <org.postgresql.version>9.0-801.jdbc4</org.postgresql.version>         <commons-dbcp.version>1.4</commons-dbcp.version>         <cglib.version>2.2.2</cglib.version>         <servlet.version>2.5</servlet.version>         <spring-security.version>3.1.4.RELEASE</spring-security.version>         <org.aspectj.version>1.6.12</org.aspectj.version>         <spring-aop.version>3.2.5.RELEASE</spring-aop.version>         <org.testng.version>6.8.7</org.testng.version>         <spring-test.version>3.2.5.RELEASE</spring-test.version>         <org.aspectj.version>1.6.2</org.aspectj.version>         <env>dev</env>         <project.parent.basedir>../</project.parent.basedir>     </properties>     <modules>         <module>reporting-rest</module>         <module>reporting-dao</module>     </modules>     <dependencies>         <dependency>             <groupId>junit</groupId>             <artifactId>junit</artifactId>             <version>3.8.1</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.slf4j</groupId>             <artifactId>slf4j-log4j12</artifactId>             <version>${org.slf4j-version}</version>             <scope>runtime</scope>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-core</artifactId>             <version>${org.springframework.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-expression</artifactId>             <version>${org.springframework.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-beans</artifactId>             <version>${org.springframework.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-aop</artifactId>             <version>${org.springframework.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-context</artifactId>             <version>${org.springframework.version}</version>         </dependency>          <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-context-support</artifactId>             <version>${org.springframework.version}</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-tx</artifactId>             <version>${org.springframework.version}</version>         </dependency>     </dependencies> </project> 

web. xml file

<!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>   <display-name>Archetype Created Web Application</display-name>     <context-param>         <param-name>contextConfigLocation</param-name>         <param-value>classpath*:config/*-context.xml</param-value>     </context-param>     <listener>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     </listener>     <servlet>         <servlet-name>reporting</servlet-name>         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>         <load-on-startup>0</load-on-startup>     </servlet>     <servlet-mapping>         <servlet-name>reporting</servlet-name>         <url-pattern>/</url-pattern>     </servlet-mapping> <!--    <filter>         <filter-name>springSecurityFilterChain</filter-name>         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>     </filter>      <filter-mapping>         <filter-name>springSecurityFilterChain</filter-name>         <url-pattern>/*</url-pattern>     </filter-mapping> -->      <session-config>         <session-timeout>60</session-timeout>     </session-config> </web-app> 

Servlet xml file for reporting-rest

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"     xmlns:context="http://www.springframework.org/schema/context"     xmlns:mvc="http://www.springframework.org/schema/mvc"     xmlns:task="http://www.springframework.org/schema/task" xmlns:oxm="http://www.springframework.org/schema/oxm"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd                 http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd                 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd                 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd                 http://www.springframework.org/schema/security                 http://www.springframework.org/schema/security/spring-security-3.1.xsd">      <context:component-scan base-package="org.reporting.rest" />     <!-- <context:annotation-config/> -->     <mvc:annotation-driven />      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />       <property name="prefix" value="/WEB-INF/jsp/" />       <property name="suffix" value=".jsp" />    </bean>      </beans> 

Controller class on reporting-rest

@Controller @RequestMapping("/api/admin/") public class AdminController {     @RequestMapping(value = "/clients", method = RequestMethod.GET)     public ModelAndView listClients() {         ModelAndView model = new ModelAndView("clientDetails");         //List<Client> clients=adminDAO.getClients();         List<String> lists = new ArrayList<String>();         lists.add("List A");         lists.add("List B");         lists.add("List C");         lists.add("List D");         lists.add("List 1");         lists.add("List 2");         lists.add("List 3");;         model.addObject("lists",lists );         model.addObject("matter","This is another matter" );         return model;     } } 

jsp view located on reporting-rest

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <body>     <h2>List Clients</h2>     ${lists}      Matter:: ${matter}     <c:if test="${not empty lists}">          <ul>             <c:forEach var="listValue" items="${lists}">                 <li>${listValue}</li>             </c:forEach>         </ul>      </c:if> </body> </html> 

Output jsp above Prints:

List Clients ${lists} Matter:: ${matter}  

No content is printed and got the things as such in ${}

Kindly help on this.

回答1:

Problem Resolved using <%@ page isELIgnored="false" %> . Thanks to http://www.mkyong.com/spring-mvc/modelandviews-model-value-is-not-displayed-in-jsp-via-el/

<!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web Application 2.4//EN"  "http://java.sun.com/dtd/web-app_2_4.dtd" > <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <%@ page isELIgnored="false" %> </head> <body>     <h2>List Clients</h2>     <c:out value="${lists}"/>      Matter:: ${matter}     <c:if test="${not empty lists}">          <ul>             <c:forEach var="listValue" items="${lists}">                 <li>${listValue}</li>             </c:forEach>         </ul>      </c:if> </body> </html> 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!