我試圖做一些非常簡單的事情。我只想在Spring MVC應用程序中顯示JSP頁面上的列表。我GOOGLE了很多,有很多的例子(包括一些在Stackoverflow),但它只是不適合我。我使用的是Maven和Spring 3.2.4。這是我的文件:使用Spring MVC在JSP頁面上未解決的EL表達式顯示列表
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>
當我運行的AP摺疊,我得到的是:
Naked List
${item.value}
我真的希望我失去了一些非常明顯的配置,這是一個快速簡單的解決方法。
我似乎無法重現你的問題。你能告訴我們你的'Item'類和你的pom.xml嗎? –
@SotiriosDelimanolis,感謝您花時間嘗試。我已經添加了Item類和我的pom.xml。 –