2014-11-04 99 views
0

我想将所有404错误重定向到带有漂亮面板的自定义网址。 我尝试了很多的事情,但我无法找到合适的解决方案,以我的情况(尽管很多职位的这个)使用Prettyfaces 3.3.X和JSF 2.2重定向404错误

以下一些信息

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    version="3.1"> 

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- JSF configuration 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->  
<context-param> 
    <param-name>javax.faces.PROJECT_STAGE</param-name> 
    <param-value>Development</param-value> 
</context-param> 

<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>*.xhtml</url-pattern> 
</servlet-mapping> 

<context-param> 
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name> 
    <param-value>true</param-value> 
</context-param>  

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- PrettyFaces configuration 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->  
<context-param> 
    <param-name>com.ocpsoft.pretty.DEVELOPMENT</param-name> 
    <param-value>true</param-value> 
</context-param> 

<context-param> 
    <param-name>com.ocpsoft.pretty.BASE_PACKAGES</param-name> 
    <param-value>com.lagoon.project.web</param-value> 
</context-param> 


<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- Server configuration 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> 
<session-config> 
    <session-timeout>30</session-timeout> 
</session-config> 

<welcome-file-list> 
    <welcome-file>/</welcome-file> 
</welcome-file-list> 
<error-page> 
    <exception-type>404</exception-type> 
    <location>/page-introuvable</location> 
</error-page> 
</web-app> 

下网页\模块\网站

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>TODO supply a title</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
</head> 
<body> 
    <div>404</div> 
</body> 
</html> 

import com.ocpsoft.pretty.faces.annotation.URLMapping; 
import javax.enterprise.context.RequestScoped; 
import javax.faces.bean.ManagedBean; 


@RequestScoped 
@URLMapping(id = "pageWebSitePageNotfound", pattern = "/page-introuvable", viewId = "/modules/website/pageNotFound.xhtml") 
@ManagedBean(name = "pageWebSitePageNotfound") 
public class PageWebSitePageNotfound { 

} 

和POM文件

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 

<groupId>com.lagoon</groupId> 
<artifactId>project</artifactId> 
<version>0.1.0-SNAPSHOT</version> 
<packaging>war</packaging> 

<name>project</name> 

<properties> 
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <java.version>1.7</java.version> 
    <jsf.version>2.2.8-02</jsf.version>  
    <spring.version>4.1.1.RELEASE</spring.version>    
</properties> 

<dependencies> 

    <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    - Web 
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> 
    <dependency> 
     <groupId>com.sun.faces</groupId> 
     <artifactId>jsf-api</artifactId> 
     <version>${jsf.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>com.sun.faces</groupId> 
     <artifactId>jsf-impl</artifactId> 
     <version>${jsf.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>com.ocpsoft</groupId> 
     <artifactId>prettyfaces-jsf2</artifactId> 
     <version>3.3.3</version> 
    </dependency> 

    <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    - Misc 
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> 
    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-web-api</artifactId> 
     <version>7.0</version> 
     <scope>provided</scope> 
    </dependency> 

</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
       <source>${java.version}</source> 
       <target>${java.version}</target> 
       <compilerArguments> 
        <endorseddirs>${endorsed.dir}</endorseddirs> 
       </compilerArguments> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>2.3</version> 
      <configuration> 
       <failOnMissingWebXml>false</failOnMissingWebXml> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <version>2.6</version> 
      <executions> 
       <execution> 
        <phase>validate</phase> 
        <goals> 
         <goal>copy</goal> 
        </goals> 
        <configuration> 
         <outputDirectory>${endorsed.dir}</outputDirectory> 
         <silent>true</silent> 
         <artifactItems> 
          <artifactItem> 
           <groupId>javax</groupId> 
           <artifactId>javaee-endorsed-api</artifactId> 
           <version>7.0</version> 
           <type>jar</type> 
          </artifactItem> 
         </artifactItems> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 
</project> 

当我尝试“/页introuvable” JSF告诉我的网页...但如果我尝试“/ somethingElse”我只是有一个标准404错误从我的导航

我该怎么办?

感谢

回答

-1

在你的web.xml文件中科迪

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

的该块它会拦截所有404错误,将页面重定向到另一个页面,你在<location>/404page.xhtml</location>定义页面。

+0

请编辑您的[现有答案](https://stackoverflow.com/a/28726027),而不是发布一个新的。 – 2015-02-25 18:07:06