JQuery not able to parse JSP EL

老子叫甜甜 提交于 2019-12-11 13:52:13

问题


My JQuery process.js file is not able to parse JSP EL expression. I am just tyring to display a List object in my JSP. If I just use the ${students} object it works correctly and displays all the students but if I try to display it using JQuery it just displays the ${students} string as it is in the browser. I have disabled scripting in my web.xml and using EL for displaying data.

Is there any other way to handle this?

Browser Output:

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>

回答1:


EL expressions are evaluated by JspServlet (*.jsp) and FacesServlet (*.xhtml) only. However, .js files are by default not processed by those servlets, but by container's builtin default servlet. This is not jQuery's fault. This is just your fault.

A quick trick is to add *.js URL pattern to the JspServlet mapping. After figuring out the name of the container's builtin JspServlet, which is in Tomcat and clones jsp, add the below entry to your webapp's web.xml:

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

Alternatives are:

  1. Refactor it to webservice call and use $.getJSON().
  2. Print it as HTML5 data attribute and use $.data().
  3. Print it as an inline script variable in JSP before desired script is included.


来源:https://stackoverflow.com/questions/33151409/jquery-not-able-to-parse-jsp-el

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!