我在我的 JSP 项目中使用 Spring:
我对所有 HTML 请求都有以下映射:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
和
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
所以每个请求都会发送给调度程序。
我有一个 JSP 页面,其中有session.getAttribute()
,它将会话变量返回到该页面。
如果某些用户直接访问该页面,则会话变量将返回为 null。
为了避免这种情况,我尝试在 JSP 页面中添加以下行:
<%@page errorPage="error.jsp"%>
JSP 文件位于该页面所在的同一目录中,但没有运气。我收到以下异常:-> 404
我还尝试了一种组合,因此 errorPage 将指向如下:
<%@page errorPage="../error.jsp"%>
还是行不通
然后,我在 Controller 中添加了一个条目,如下所示:
@RequestMapping("/xyz/result/error")
public ModelAndView showErrorPageEng() {
return new ModelAndView("errorpage", "command", null);
}
更新部分问题:
我有错误页面的以下磁贴条目:
<definition name="errorpage" extends="basic"
template="/error/error_layout.jsp">
<put-attribute name="error" value="/error/error.jsp"/>
</definition>
并将 JSP 中的代码修改为:
<%@page errorPage="error.html"%>
上述路径/xyz/result/error如下:
ROOT/xyz/result/error
我可以访问该文件mydomain/xyz/result/error.html
,但没有发生同样的情况,但有以下例外:
开始为
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:745)
at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:716)
at org.apache.jsp.error_jsp._jspService(error_jsp.java:63)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:745)
**and ends as**
at org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:220)
at org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
at org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:103)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:659)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:678)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:633)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:322)
at org.springframework.web.servlet.view.tiles2.TilesView.renderMergedOutputModel(TilesView.java:124)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549
)