带有Spring MVC的未解析的EL表达式在JSP页面上显示列表 [英] Unresolved EL expression displaying list on JSP page with Spring MVC

查看:79
本文介绍了带有Spring MVC的未解析的EL表达式在JSP页面上显示列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

此处的问题是您的Web应用程序版本.您正在使用Servlet 2.3

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>

在此版本中,默认情况下,EL被忽略 .您需要添加

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

<%@ page isELIgnored ="false" %> 

位于JSP的顶部.

我真的建议您更新servlet版本.我们现在要升级到3.1版,而升级到2.3版是没有道理的.

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.

相关

这篇关于带有Spring MVC的未解析的EL表达式在JSP页面上显示列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆