问题描述: el表达式${}标签的数据在页面没有显示。
首先看我的servlet和jsp页面。
Servlet代码:
@WebServlet(urlPatterns = "/demo1")
public class Servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 准备数据
List<Brand> brands = new ArrayList<>();
brands.add(new Brand(1,"三只松鼠","三只松鼠",100,"三只松鼠,好吃不上火",1));
brands.add(new Brand(2,"优衣库","优衣库",200,"优衣库,服适人生",0));
brands.add(new Brand(3,"小米","小米科技有限公司",1000,"为发烧而生",1));
// 2. 存储到request域中
request.setAttribute("brands", brands);
// 3. 转发到 el-demo.jsp中
request.getRequestDispatcher("/el-demo.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
JSP代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${brands}
</body>
</html>
在浏览器中访问:http://localhost:8080/Jsp_demo/demo1,发现使用setAttribute封装在brands域中的数据并没有显示出来
原因:JSP和Servlet版本导致el功能默认关闭,加入<%@page isELIgnored="false"%>标签手动开启el功能。
修改后JSP代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%> <%-- 加入该标签手动开启el功能 --%>
<html>
<head>
<title>Title</title>
</head>
<body>
${brands}
</body>
</html>
再次在浏览器中访问:http://localhost:8080/Jsp_demo/demo1, el表达式${}可以正常显示了。