gpt4 book ai didi

java - 使用 Spring MVC 在 JSP 页面上显示列表的未解析 EL 表达式

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

我正在尝试做一些非常简单的事情。我只想在 Spring MVC 应用程序的 JSP 页面上显示一个列表。我在谷歌上搜索了很多,并且有很多例子(包括 Stackoverflow 上的一些),但它对我来说不起作用。我正在使用 Maven 和 Spring 3.2.4。这些是我的文件:

Item.java

public class Item 
{
private final String value;

public Item(String value)
{
this.value = value;
}

public String getValue()
{
return value;
}
}

MainController.java

@Controller
public class MainController
{
@RequestMapping("/")
public String home(ModelMap map)
{
final List<Item> items = new ArrayList<Item>();
items.add(new Item("Value 1"));
items.add(new Item("Value 2"));
items.add(new Item("Value 3"));
items.add(new Item("Value 5"));
map.addAttribute(items);
return "list";
}
}

list.jsp

<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<html lang="en">
<head>
<title>Naked List</title>
<body>
<h2>Naked List</h2>
<p>
<c:forEach items="${itemList}" var="item">
<c:out value="${item.value}"/>
<br/>
</c:forEach>
</p>
</body>
</html>

applicationContext.xml

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="...">

<context:annotation-config />
<context:component-scan base-package="org.ne.nakedlist" />

<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

</beans>

webxml

<!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>Naked List</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.ne.nakedlist</groupId>
<artifactId>org.ne.nakedlist</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Naked List</name>
<description>Naked List</description>
<properties>
<java.version>1.7</java.version>
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
</properties>
<dependencies>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-runner</artifactId>
<version>7.4.5.v20110725</version>
<destFileName>jetty-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>nakedlist</finalName>
</build>

当我运行该应用程序时,我得到的是:

Naked List

${item.value}

我真的希望我错过了一些非常明显的配置,这是一个快速简单的修复。

最佳答案

这里的问题是您的网络应用程序版本。您使用的是 Servlet 2.3

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

在此版本中,EL is ignored by default 。您需要添加

<%@ page isELIgnored ="false" %> 

位于 JSP 的顶部。

我真的建议您更新您的 servlet 版本。我们现在即将推出 3.1,2.3 没有意义。

<小时/>

相关

关于java - 使用 Spring MVC 在 JSP 页面上显示列表的未解析 EL 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18935974/

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