Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

java - Unresolved EL expression displaying list on JSP page with Spring MVC

I'm trying to do something really simple. I just want to display a list on a JSP page from in a Spring MVC application. I've googled plenty and there are lots of examples (including some on Stackoverflow), but it's just not working for me. I'm using Maven and Spring 3.2.4. These are my files:

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>

When I run the application, what I get is:

Naked List

${item.value} 

I'm really hoping I'm missing some really obvious configuration and this is a quick easy fix.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem here is your version of web application. You are on 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>

In this version, EL is ignored by default. You need to add

<%@ page isELIgnored ="false" %> 

at the top of your JSP.

I would really recommend you update your servlet version. We're coming up on 3.1 right now, doesn't make sense to be on 2.3.


Related


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...