我想将所有404错误重定向到带有prettyfaces的自定义网址。 我尝试了很多东西,但我无法找到适合我案例的解决方案(尽管有大量关于此事的帖子)
这里有一些信息
的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>
在网页\ modules \ website
下<?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>
当我尝试&#34; / page-introuvable&#34; JSF向我显示页面......但如果我尝试&#34; / somethingElse&#34;我的浏览器只有标准的404错误
我该怎么办?
感谢
答案 0 :(得分:-1)
在你的web.xml中设置这个cody块
<error-page>
<error-code>404</error-code>
<location>/404page.xhtml</location>
</error-page>
它将拦截任何404错误,并将页面重定向到另一个页面,您在<location>/404page.xhtml</location>
定义页面。