我的JQuery process.js
文件无法解析JSP EL表达式。我只是在我的JSP中显示一个List对象。如果我只使用${students}
对象,它可以正常工作,并显示所有学生,但如果我尝试使用JQuery显示它,它只会显示${students}
字符串,因为它在浏览器中。我在我的web.xml
中禁用了脚本,并使用EL来显示数据。JQuery无法解析JSP EL
有没有其他办法可以解决这个问题?
浏览器输出:
Hello World from JQuery!
From JSP:
[Student{id=1, name=Jack}, Student{id=2, name=Jill}]
From JQuery:
${students}
针对home.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="js/process.js"></script>
</head>
<body>
<div id="mydiv"></div>
<h2>From JSP: <p/> ${students}</h2>
<h3>From JQuery: <div id="studentDiv"></div></h3>
</body>
</html>
process.js
$(document).ready(function() {
//static message
var msg = "Hello World from JQuery!";
$("#mydiv").text(msg);
//dynamic message
var students = '${students}';
$("#studentDiv").text(students);
});
HomePageController.java
@WebServlet(name = "HomePageController", urlPatterns = {"/homePageController.do"})
public class HomePageController extends BaseController {
private static final String VIEW_NAME = "/jsp/home.jsp";
@Override
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Student> students = getAllStudents();
request.setAttribute("students", students);
request.getServletContext().getRequestDispatcher(VIEW_NAME).forward(request, response);
}
private List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
Student student1 = new Student(1, "Jack");
Student student2 = new Student(2, "Jill");
students.add(student1);
students.add(student2);
System.out.println("students = " + students);
return students;
}
}
BaseController.java
public abstract class BaseController extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
public abstract void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;
}
的web.xml
<?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_3_1.xsd"
version="3.1">
<!-- Disable scripting -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
.js文件不会被服务器端进程执行,因此任何服务器端变量都将被简单地渲染为纯文本。如果你需要这种行为,你需要把'$ {students}'输出放在正确的位置,比如HTML中的'data'属性,然后通过JS通过DOM读取它。 –
我想你错过了一个核心概念。 JSP EL在服务器端执行并生成发送到浏览器的html/javascript。 jQuery在客户端浏览器上运行 –