내 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
파일은 기본적으로 해당 서블릿에 의해 처리되지 않고 컨테이너의 내장 기본 서블릿에 의해 처리됩니다. 이것은 jQuery의 잘못이 아닙니다. 이것은 당신의 잘못입니다.
빠른 트릭은 *.js
URL 패턴을 JspServlet
매핑 에 추가 하는 것입니다. JspServlet
Tomcat 및 clones 에있는 컨테이너의 builtin 이름을 파악한 후 jsp
아래 항목을 webapp에 추가합니다 web.xml
.
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
대안은 다음과 같습니다.
$.getJSON()
.data
속성 으로 인쇄하고 $.data()
.이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다