JQuery无法解析JSP EL
JQuery not able to parse JSP EL
我的JQuery process.js
文件无法解析JSP EL表达式。我只是想在JSP中显示一个List对象。如果我只是使用${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>
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>
EL表达式仅由JspServlet
(*.jsp
)和FacesServlet
(*.xhtml
)求值。但是,默认情况下,.js
文件不由这些servlet处理,而是由容器内置的默认servlet处理。这不是jQuery的错。这都是你的错。
*.js
URL模式添加到JspServlet
映射中。在确定了容器的内置JspServlet
(它位于Tomcat中并克隆了jsp
)的名称后,将以下条目添加到web应用程序的web.xml
中:
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
替代:
- 将其重构为webservice调用,并使用
$.getJSON()
。 - 打印为HTML5
data
属性并使用$.data()
。 - 在包含所需脚本之前,将其作为内联脚本变量打印到JSP中。
相关文章:
- 使用JSP从服务器检索和显示图像
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- JQuery需要帮助理解(i,el)
- servlet中的请求对象,而不是从jsp接收参数值
- 使用Javascript生成包含JSP的HTML
- 转义在jsp's脚本
- 显示&作为&在jsp中使用angularjs而不使用ng-bind-html
- 在JSP中运行pdftk
- 使用javascript和jsp将数据插入到融合图中
- 如何将输入类型值或id从一个jsp传递到另一个jsp页面
- 在更改名称时,使用ajax jsp在下拉菜单中填充多个文件夹的数据
- 在使用cq:include包含外部JS之前,如何在JSP中包含全局JS变量
- 如何将日期id从一个jsp传递到另一个jsp页面
- getting id from jsp
- 将多个参数从jsp页面传递给javascript函数
- 将对Ajax PUT的json响应重定向到要由EL解析的JSP中
- 如何将EL表达式嵌入jquery/javascript生成的HTML字符串中,以便插入JSP页面)
- 在jsp中的el表达式中使用javascript变量
- JQuery无法解析JSP EL
- ${}模板文字(ES2015)与JSP EL语法冲突