【发布时间】:2013-09-14 15:29:27
【问题描述】:
我的页面中有一个特定的 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)
我想不是我使用了错误的路线?但是我怎样才能找到哪一个是正确的呢?我只是按照我的项目结构:
Servlet 代码:
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>
【问题讨论】:
-
您是否尝试过使用
.html()? -
是的,我得到了与我的第一个测试相同的结果(使用 .text()),因为我的请求返回的是带有 jsp' 路径的文本而不是 html 页面。
-
.getScript()呢? -
使用 $('#content').getScript(responseText);给出:未捕获的类型错误:对象 [object Object] 没有方法“getScript”。但是我为什么要使用那个,我不是要加载脚本而是加载jsp页面。