我的页面中有一个特定的 div,我想在其中加载所有子页面(为此我使用 JQuery)。在加载它们之前,我需要在服务器中进行一些操作,所以我调用了我的 servlet,在完成所有操作之后,我想加载 .jsp 页面......但我不知道如何。
responseText 为:
/SAJ/WebContent/WEB-INF/views/administracion/semestres/semestreIndex.jsp
(在我的项目结构中存储需要 .jsp 的路径)
我得到它在div中写responseText:
<div id="content" class="testDiv" >
使用这个:
function load() {
$.ajax({
type: "POST",
url: "/SAJ/pages/semestre",
success: function(responseText) {
$('#content').text(responseText);
}
});
}
但我不知道如何加载整个页面。我的第一个想法是这样做:
function load() {
$.ajax({
type: "POST",
url: "/SAJ/pages/semestre",
success: function(responseText) {
$('#content').load(responseText);
}
});
}
但我在 Chrome 的控制台中收到 404 错误:
GET http://localhost:8080/SAJ/WebContent/WEB-INF/views/administracion/semestres/semestreIndex.jsp 404 (Not Found)
我想比我使用错误的路线?但是我怎样才能找到哪一个是正确的呢?我只是按照我的项目结构:
小服务程序代码:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
try {
Action action = ((ActionFactory) getServletContext().getAttribute("actions")).getAction(request);
String view = action.execute(request, response);
if(view.equals("index")){
request.getRequestDispatcher("/WEB-INF/" + view + ".jsp").forward(request, response);
}else{
response.getWriter().write("/SAJ/WebContent/WEB-INF/" + view + ".jsp");
}
} catch (Exception e) {
throw new ServletException("Executing action failed.", e);
}
}
servlet 调用的操作:
public class SemestreListarAction implements Action{
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
request.setAttribute("test", "HELLO WORLD!");
System.out.println("I'm nice so I did some stuff for you!");
return "views/administracion/semestres/semestreIndex";
}
}
web.xml
<servlet>
<servlet-name>Redirector</servlet-name>
<servlet-class>com.saj.controller.Redirector</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Redirector</servlet-name>
<url-pattern>/pages/*</url-pattern>
</servlet-mapping>
呼叫者:
<a href="#" onclick="load()">Load</a>