gpt4 book ai didi

java - jsp 上没有打印任何内容来添加模型对象

转载 作者:行者123 更新时间:2023-12-01 12:20:20 27 4
gpt4 key购买 nike

我在下面遇到了奇怪的行为:我在 Spring Controller 中添加一个模型对象,当我在 jsp 中使用 jSTL 访问相同的模型对象时,我什么也没得到。

我的项目是基于 REST 构建的,基于 Java 1.8+ Tomcat 8.0+ Maven。

我的 Maven 项目名为 reporting,具有 reporting-rest 和 reporting-dao 模块。Jsp 页面和 Controller 位于 reporting-rest 中。

pom。 reporting-rest 中的 xml 内容

<?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 内容(父级)

<?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>

网络。 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>
<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>

用于reporting-rest的Servlet xml文件

<?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>

reporting-rest 上的 Controller 类

@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 位于 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>

输出上面的jsp打印:

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

没有打印任何内容并在${}中获取诸如此类的内容

请帮忙解决这个问题。

最佳答案

问题已使用 <%@ page isELIgnored="false"%> 解决。感谢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>

关于java - jsp 上没有打印任何内容来添加模型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26709325/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com