关于JSP中使用EL表达式无法取出request.setAttribute中设置的值
首先看代码
在LoginServlet.java中
String result="";
if (loginCustomer!=null) {
request.getRequestDispatcher("/index.jsp").forward(request, response);
}else {
result = "用户名或密码错误";
request.setAttribute("result",result);
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
验证用户名密码是否正确,如果正确内部跳转主页,如果不正确将result保存到request域中
在登录页面login.jsp中
<script type="text/javascript">
//使用EL表达式取到result值
var result = '${result}';
//如果result不为空
if(result){
alert(result);
}
</script>
如果发现EL表达式无法取到result的值,在弹窗中显示
${result}
则有可能为WEB-INF目录下的web.xml中dtd版本过低
解决方式有两种:
-
修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> </web-app>
-
在jsp页面指令中添加
isELIgnored="false"
添加到
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
问题解决
over