自定义404错误页面不使用Struts 2。

时间:2022-10-09 21:07:40

I am converting a JSP/Servlet dynamic web project to Struts 2 and the custom error pages (that previously worked with the JSP/Servlet version) have stopped working.

我正在将一个JSP/Servlet动态web项目转换为Struts 2,而自定义错误页面(以前使用的JSP/Servlet版本)已经停止工作。

When I induce a 404 error by accessing a non-existent page like abc.jsp, it throws below exception:

当我通过访问一个不存在的页面(如abc)引发404错误时。jsp,它抛出以下异常:

SEVERE: Servlet.service() for servlet jsp threw exception
The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
    at org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60)
    at org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)
    at org.apache.jsp._404_jsp._jspx_meth_s_005finclude_005f0(_404_jsp.java:111)
    at org.apache.jsp._404_jsp._jspService(_404_jsp.java:71)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    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:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:467)
    at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:338)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:203)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

Nov 18, 2014 11:14:56 PM org.apache.catalina.core.StandardHostValve custom
SEVERE: Exception Processing ErrorPage[errorCode=404, location=/404.jsp]
org.apache.jasper.JasperException: An exception occurred processing JSP page /404.jsp at line 6

3: <html>
4: <head>
5: <title>My Site - WHOOPS!</title>
6: <s:include value="css/style2.css" />
7: </head>
8: 
9: <body class="errorpage">


Stacktrace:
    at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    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:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:467)
    at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:338)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:203)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has passed through its servlet filter, which initializes the Struts dispatcher needed for this tag. - [unknown location]
    at org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60)
    at org.apache.struts2.views.jsp.StrutsBodyTagSupport.getStack(StrutsBodyTagSupport.java:44)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doStartTag(ComponentTagSupport.java:48)
    at org.apache.jsp._404_jsp._jspx_meth_s_005finclude_005f0(_404_jsp.java:111)
    at org.apache.jsp._404_jsp._jspService(_404_jsp.java:71)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    ... 23 more

Below is my project structure:

下面是我的项目结构:

自定义404错误页面不使用Struts 2。

web.xml:

web . xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>My Site</display-name>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>404</error-code>
        <location>/404.jsp</location>
    </error-page>

    <error-page>
        <error-code>500</error-code>
        <location>/500.jsp</location>
    </error-page>

    ...

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

How do I make the custom error pages work with Struts 2?

如何使自定义错误页面与Struts 2一起工作?

2 个解决方案

#1


2  

You are calling a JSP directly, and trying to use a Struts tag inside it:

您直接调用JSP,并尝试在其中使用Struts标记:

6: <s:include value="css/style2.css" />

Turn this to plain html and it will work.

将其转换为纯html,它就可以工作了。

Otherwise, you need to call an action.

否则,您需要调用一个操作。

#2


2  

Instead of custom error page create a custom error action that returns a result to a custom error page.

而不是自定义错误页面创建一个自定义错误动作,它将结果返回到自定义错误页面。

struts.xml:

struts.xml:

<action name "error" class="ErrorAction">
  <result name="404">/404.jsp</result>
  <result name="500">/500.jsp</result>
</action> 

ErrorAction:

ErrorAction:

public class ErrorAction extends ActionSupport {

  private Integer statusCode;
  //getter and setter

  @Override
  public String execute() {
    if (stausCode != null)
    switch (statusCode){
      case 404:
        return "404";
      case 500:
        return "500";
    }
  }
}

web.xml:

web . xml:

<error-page>
    <error-code>404</error-code>
    <location>/error_code.jsp</location>
</error-page>

<error-page>
    <error-code>500</error-code>
    <location>/error_code.jsp</location>
</error-page>

error_code.jsp:

error_code.jsp:

<% 
 Integer statusCode = (Integer) request
                       .getAttribute("javax.servlet.error.status_code");

 response.sendRedirect(pageContext.getServletContext().getContextPath()
                       +"/error?statusCode="+statusCode ); 
%>

#1


2  

You are calling a JSP directly, and trying to use a Struts tag inside it:

您直接调用JSP,并尝试在其中使用Struts标记:

6: <s:include value="css/style2.css" />

Turn this to plain html and it will work.

将其转换为纯html,它就可以工作了。

Otherwise, you need to call an action.

否则,您需要调用一个操作。

#2


2  

Instead of custom error page create a custom error action that returns a result to a custom error page.

而不是自定义错误页面创建一个自定义错误动作,它将结果返回到自定义错误页面。

struts.xml:

struts.xml:

<action name "error" class="ErrorAction">
  <result name="404">/404.jsp</result>
  <result name="500">/500.jsp</result>
</action> 

ErrorAction:

ErrorAction:

public class ErrorAction extends ActionSupport {

  private Integer statusCode;
  //getter and setter

  @Override
  public String execute() {
    if (stausCode != null)
    switch (statusCode){
      case 404:
        return "404";
      case 500:
        return "500";
    }
  }
}

web.xml:

web . xml:

<error-page>
    <error-code>404</error-code>
    <location>/error_code.jsp</location>
</error-page>

<error-page>
    <error-code>500</error-code>
    <location>/error_code.jsp</location>
</error-page>

error_code.jsp:

error_code.jsp:

<% 
 Integer statusCode = (Integer) request
                       .getAttribute("javax.servlet.error.status_code");

 response.sendRedirect(pageContext.getServletContext().getContextPath()
                       +"/error?statusCode="+statusCode ); 
%>