1

我有以下代码:

<%@ page language="java" session="true" contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<% 
request.setAttribute("action", request.getParameter("action"));
%>

<c:choose>
    <c:when test="${action == null}">
        NULL
    </c:when>
    <c:when test="${action == view}">
        VIEW
    </c:when>
</c:choose>

但是,当我使用 传递 URL 时?action=view,它不会显示VIEW.

我哪里错了?

4

1 回答 1

2

EL 表达式${view}正在页面、请求、会话或应用程序范围中寻找与该名称完全相同的属性,就像${action}工作原理一样。但是,您打算将其与字符串进行比较。然后,您应该使用引号使其成为真正的字符串变量,就像这样${'view'}。它也可以在普通的 Java 代码中以这种方式工作。

<c:choose>
    <c:when test="${action == null}">
        NULL
    </c:when>
    <c:when test="${action == 'view'}">
        VIEW
    </c:when>
</c:choose>

顺便说一句,使用该scriptlet将请求参数复制为请求属性是很笨拙的。您根本不应该使用scriptlet。HTTP 请求参数在 EL 中已由${param}地图提供。

<c:choose>
    <c:when test="${param.action == null}">
        NULL
    </c:when>
    <c:when test="${param.action == 'view'}">
        VIEW
    </c:when>
</c:choose>

这样你就可以摆脱整<% .. %>条线。

也可以看看:

于 2013-10-11T17:46:19.980 回答