【问题标题】:JSP expression language not workingJSP 表达式语言不起作用
【发布时间】:2012-01-05 15:53:05
【问题描述】:

我无法在我的 .jsp 页面上使用 ${} 表达式。

displayAllCustomers.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


<html>
    <body>
        <h3>Our Entire Customer Database</h3>
        <ul>
            <c:forEach items="${allCustomers}" var="customer">
                <li>${customer.name}</li>
            </c:forEach>
        </ul>
    </body>
</html>

dispatcher-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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                                      http://www.springframework.org/schema/tx
                                      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                                      http://www.springframework.org/schema/aop 
                                      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <import resource="applicationContext.xml"/>     

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


    <bean name="/displayAllCustomers" class="mypackage.DisplayAllCustomersController">
        <property name="customerManagementService" ref="customerManagementService" />
    </bean>     

</beans>

DisplayAllCustomersController.java

public class DisplayAllCustomersController {

    private CustomerManagementService customerManagementService;
    public void setCustomerManagementService(CustomerManagementService customerManagementService) {
        this.customerManagementService = customerManagementService;
    }

    @RequestMapping("/displayAllCustomers")
    public ModelAndView displayAllCustomers() {
        List<Customer> allCustomers = customerManagementService.getAllCustomers();
        return new ModelAndView("displayAllCustomers", "allCustomers", allCustomers);
    }
}

当我显示页面时,我只显示标题“我们的整个客户数据库”。

这让我发疯,我无法弄清楚我错过了什么。

有人可以帮我理解为什么会这样吗?

非常感谢。

【问题讨论】:

  • 您的标题与问题描述冲突。如果 taglibs/EL 不工作,你会在 webbrowser 中通过右键单击 > View Source 在生成的 HTML 输出中看到它们。这是真的?您在生成的 HTML 输出中看到了什么?你看到&lt;c:forEach&gt;了吗?你看到${customer.name}了吗?如果你把${allCustomers} 放在&lt;h3&gt; 里面呢?
  • @BalusC:我不知道我错误地从org.springframework.web.portlet.portal 包中导入了ModelAndView 类,否则我遇到的问题不会有这个标题。但你是对的。

标签: jsp spring-mvc spring-3


【解决方案1】:

将以下内容添加到顶部

<%@ page isELIgnored="false"%>

【讨论】:

  • 在顶部添加了&lt;%@ page isELIgnored="false"%&gt;,但它仍然不起作用。我正在从数据库中获取所有数据,因为我通过在控制台上打印它来检查它,但我无法显示它。
  • 你可以试试下面两个,输出是什么 ${allCustomers}
  • &lt;%= request.getAttribute("allCustomers") %&gt; 显示 null${allCustomers} 没有显示任何不应该显示的内容。 allCustomers 在传递给 displayAllCustomers.jsp 视图的 Model 中迷失在哪里?
  • 如果我添加这个,我会在我的浏览器上看到原始的 html 文本。任何机构都面临这个问题?
  • web.xml 中 web-app 标签的版本属性是什么?它应该是这样的: java.sun.com/xml/ns/javaee" xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:schemaLocation="java.sun.com/xml/ns/javaee java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
【解决方案2】:

我错误地使用了错误的导入。

当我认为我已经导入了 org.springframework.web.servlet.ModelAndView; 时,我自动导入了 import org.springframework.web.portlet.ModelAndView;

差点把我逼疯了。

谢谢。

【讨论】:

    【解决方案3】:

    我遇到了一些问题,上述解决方案奏效了。但问题是什么?我运行另一个项目,但我不执行此导入。但它仍然可以正常工作。

    我指的是这个解决方案:

        <%@ page isELIgnored="false"%>
    

    【讨论】:

    • 对我来说,是错误的导入导致了这个问题,我使用自动导入错误地导入了 import org.springframework.web.portlet.ModelAndView;,而我应该导入 org.springframework.web.servlet.ModelAndView;
    • 上述解决方案对我有用,但问题是什么..我已经导入了正确的 ModelAndView。
    【解决方案4】:

    现在考虑您使用的是 jsp 3.x 或更高版本。 而且您还使用 spring.io 存储库中的 spring maven。

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    
    <repositories>
        <repository>
            <id>io.spring.repo.maven.release</id>
            <url>http://repo.spring.io/release/</url>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
    </repositories>
    

    您必须检查 maven 库是否包含 tomcat-embeded-el。 如果您没有使用 spring 的嵌入式 tomcat 服务器,那么请删除上述 (tomcat-embeded-*) 库或编写新的 pom 依赖项。
    喜欢

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.6.RELEASE</version>
            <scope>runtime</scope>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.6.RELEASE</version>
            <scope>runtime</scope>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.6.RELEASE</version>
            <scope>runtime</scope>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.6.RELEASE</version>
            <scope>runtime</scope>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.1.6.RELEASE</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
            <scope>runtime</scope>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.6.RELEASE</version>
            <scope>runtime</scope>
        </dependency>
    
    </dependencies>
    
    <repositories>
        <repository>
            <id>io.spring.repo.maven.release</id>
            <url>http://repo.spring.io/release/</url>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
    </repositories>
    

    这应该可行!

    【讨论】:

      猜你喜欢
      • 2011-01-11
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多