【问题标题】:Error Handling when try to access non-existent page X.html尝试访问不存在的页面 X.html 时的错误处理
【发布时间】:2013-07-08 08:23:36
【问题描述】:

我正在使用 spring 3.2.2、hibernate 4.2.2 和 JSF 2.1 (mojora) 和 RichFaces 4.3.2 运行应用程序

我的配置运行良好。我可以通过 .xhtml,.jsf,.jsp 访问页面,但是...

当您尝试访问一个不存在的页面(例如没有创建的 blabla.xhtml)时,问题出现在显示屏上。如何处理错误?

感谢大家的帮助。

这是错误,这会导致无限递归:

ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException

这是我的 web.xml:

  <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>


  <!-- JSF Mapping -->  
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
    <url-pattern>*.xhtml</url-pattern>
    <url-pattern>*.jsp</url-pattern>
  </servlet-mapping>

 <!-- Change to "Production" when you are ready to deploy -->
  <context-param>
  <param-name>javax.faces.PROJECT_STAGE</param-name>
  <param-value>Development</param-value>
  </context-param>

  <!-- Faces ConfigPath Context --> 
  <context-param>
   <param-name>javax.faces.CONFIG_FILES</param-name> 
      <param-value>classpath:webConfiguration/faces-config.xml</param-value> 
   </context-param> 

   <!-- Add Spring Context  -->
  <context-param>
  <param-name> contextConfigLocation </param-name>
   <param-value>classpath:webConfiguration/applicationContext.xml</param-value>
 </context-param>

    <!-- Apache EL Faces Context --> 
    <context-param>
    <param-name>com.sun.faces.expressionFactory</param-name>
    <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
   </context-param>

    <!-- Richfaces Skin Context --> 
    <context-param>
       <param-name>org.richfaces.SKIN</param-name>
       <param-value>laguna</param-value>
    </context-param>

   <!-- Add Support for Spring -->
  <listener><listener-class>
  org.springframework.web.context.ContextLoaderListener
  </listener-class></listener>
  <listener>
  <listener-class>
  org.springframework.web.context.request.RequestContextListener
  </listener-class></listener>

【问题讨论】:

    标签: spring jsf-2 richfaces http-status-code-404


    【解决方案1】:

    对于不存在的页面,您应该得到一个错误状态代码 404。您可以通过在 web.xml 中定义以下片段来处理错误代码

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

    【讨论】:

    • 好吧,如果你添加 Mubin 之前所说的,它就可以工作,你还需要删除 servlet 映射中的 *.jsp 行
    • 奇怪的是它显然已经为您修复了它。因为,如果您不指定,则将显示服务器默认值。显然删除不正确的 URL 模式是 实际 解决方案,而不是指定自定义错误页面。因此,这个答案是完全错误的,不应该被接受。
    • 是的,这是真的;但我接受了答案,因为我认为我的评论可以理解。但不是全部。对不起。
    【解决方案2】:

    这很容易。在您的根控制器中设置此映射功能,但重要的是您的子控制器没有任何带有 * 字符的映射

    根控制器功能示例:

    @RequestMapping(value={"/*", "/**"})
    public String pageNotFound() {
    
        return "app/pagenotfound";
    }
    

    子链接或子映射控制器的映射示例

    @RequestMapping(value={"/something", "/something/"})
    

    如果你像这样贴标子映射控制器:

    @RequestMapping(value={"/something", "/something/*"})
    

    然后根控制器将省略此控制器的pageNotFound映射函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-06
      • 2010-10-17
      • 2021-11-03
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多