使用jstl <c:when>标记未正确计算表达式

时间:2016-05-10 19:05:31

标签: jsp jstl

我正在尝试使用c:选择标记和c:当jstl标记在以下代码中但表达式未正确计算。我有一个servlet,可以在会话范围内正确设置bean Seller。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    
<!DOCTYPE html>
<html lang="en">
    <c:choose>
        <c:when test="${not empty sessionScope.seller}">
            <c:out value="${seller.firstName}"/>
        </c:when>
        <c:otherwise>
            <c:out value="none"/>
        </c:otherwise>
    </c:choose>
</html>

servlet代码如下:

package com.senachat.presentation;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.senachat.domain.Seller;

public class SenachatServlet extends HttpServlet{

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    resp.setContentType("text/html");

    Seller seller = new Seller();
    seller.setFirstName("John");

    HttpSession session = req.getSession();
    session.setAttribute("seller", seller);

    RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/jsp/index.jsp");
    dispatcher.forward(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doGet(req, resp);
}

}

加载index.jsp页面时,将打印值“none”而不是firstName“John”的值。我无法确定我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

经过进一步调查,我意识到表达语言声明甚至没有被推测出来。

所以我转到以下链接https://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html

的文档

它提到默认情况下,对于servlet spec 2.3及更早版本禁用了EL。当然,我在web.xml中声明了旧版本的servlet spec 2.3