在jsp页面中使用了el表达式但是页面显示${...}

代码如下:

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

<html>
<head>
    <title>Title</title>
</head>
<body>
<h4>if测试</h4>
<hr>

<form action="coreif.jsp" method="get">
    <input type="text" name="username" value="${param.username}">
    <input type="submit" value="登录">
</form>

<c:if test="${param.username=='admin'}" var="isAdmin">
    <c:out value="管理员欢迎您"/>
</c:if>

<c:out value="${isAdmin}"/>
</body>
</html>

运行情况如下:

 

可以看出EL表达式没有识别,原因是因为没有在jsp中加上:

<%@page isELIgnored="false"%>

如果设定为真,那么JSP中的表达式被当成字符串处理。比如下面这个表达式<p>${2000 % 20}</p>在isELIgnored="true"时输出为${2000 % 20},而isELIgnored="false"时输出为100。Web容器默认isELIgnored="false"。

至于为什么狂神的java教程中并没有加着一句话,是因为我每次创建项目并没有改web.xml为最新版,默认版本为2.3,而2.3版本的isELIgnored="true",之前懒得改觉得没用,现在看来还是要改啊。

web.xlm最新版代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
</web-app>

 

posted @ 2022-03-11 22:35  塵暘  阅读(268)  评论(0编辑  收藏  举报