Javascript JQuery无法解析JSP EL

Javascript JQuery无法解析JSP EL,javascript,java,jquery,jsp,el,Javascript,Java,Jquery,Jsp,El,我的JQueryprocess.js文件无法解析JSP EL表达式。我只是想在JSP中显示一个列表对象。如果我只使用${students}对象,它会正常工作并显示所有学生,但是如果我尝试使用JQuery显示它,它只会显示浏览器中的${students}字符串。我已经在我的web.xml中禁用了脚本,并使用EL显示数据 还有别的办法吗 浏览器输出: Hello World from JQuery! From JSP: [Student{id=1, name=Jack}, Student{id

我的JQuery
process.js
文件无法解析JSP EL表达式。我只是想在JSP中显示一个列表对象。如果我只使用
${students}
对象,它会正常工作并显示所有学生,但是如果我尝试使用JQuery显示它,它只会显示浏览器中的
${students}
字符串。我已经在我的
web.xml
中禁用了脚本,并使用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>
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;
    }
}
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>

*.jsp
真的
30
index.jsp

EL表达式仅由
JspServlet
*.jsp
)和
FacesServlet
*.xhtml
)计算。但是,默认情况下,
.js
文件不是由这些servlet处理的,而是由容器的内置默认servlet处理的。这不是jQuery的错。这只是你的错

一个快速的技巧是将
*.js
URL模式添加到
JspServlet
映射中。在计算出容器内置的
JspServlet
的名称后,将以下条目添加到您的webapp的
web.xml

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

jsp
*.js
备选方案包括:

  • 将其重构为webservice调用并使用
    $.getJSON()
  • 将其打印为HTML5
    data
    属性并使用
    $.data()
  • 在包含所需脚本之前,将其作为JSP中的内联脚本变量打印

  • .js文件不是由服务器端进程执行的,因此任何服务器端变量都将简单地呈现为纯文本。如果您需要这种行为,您需要将
    ${students}
    输出放置在正确呈现的位置,例如HTML中的
    data
    属性中,然后通过JS通过DOM读取。我认为您错过了一个核心概念。JSP EL在服务器端执行,并生成发送到浏览器的html/javascript。jQuery在客户端浏览器上运行谢谢!通过使用上面建议的第二种方法,即使用HTML5
    data
    属性并使用
    $.data()
    jquery方法,它可以正常工作
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>