【发布时间】:2014-02-16 05:03:24
【问题描述】:
我使用 JSF 和其他一些依赖项创建了一个基本的 web maven 项目。 基本上项目是一个没有任何java类的HelloWorld,只有两个页面:index.jsp和olamundo.xhtml。问题是当我访问链接“localhost:8082/FinanceiroWeb/olamundo.jsf”发生http 404错误,但是当我访问链接“localhost:8082/ FinanceiroWeb/olamundo.xhtml”有效。不知道我做错了什么或没有做。
我使用 Apache Tomcat 7.0。
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"
version="3.0">
<display-name>FinanceiroWeb</display-name>
<servlet>
<display-name>Faces Servlet</display-name>
<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>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.xhtml</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.financeiro</groupId>
<artifactId>FinanceiroWeb</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>FinanceiroWeb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-digester3</artifactId>
<version>3.2</version>
<classifier>with-deps</classifier>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Java Server Faces -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
<build>
<finalName>FinanceiroWeb</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
olamundo.xhtml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Teste inicial JSF</title>
</h:head>
<h:body>
<h:outputText value="Olá Mundo!" />
</h:body>
</html>
index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
【问题讨论】:
-
使用给定的配置,如果 olamundo.xhtml 存在,您将在键入 olamundo.jsf 和原始 facelet 代码时检索它正确处理键入 olamundo.xhtml。 faces servlet 的 url-pattern 部分指定哪些请求将访问 faces servlet。
-
好的,我明白了。但是当我尝试通过 url olamundo.jsf 访问页面时显示 404 错误,并且当访问 olamundo.xhtml url 时有效。什么设置有问题?
-
你有很多无关紧要或与问题无关的东西。 JSF 2 旨在使用 facelets 格式 xhtml。忘记其他扩展,因为它们无关。坚持使用 xhtml。还将您的 servlet 映射配置为也使用 xhtml 模式。我不明白为什么你有几个欢迎文件,即使它们似乎与你的问题无关......
-
事实上,当我创建项目时,欢迎的各种文件都已经配置好了,我认为这没有问题。我正在研究一本关于 JSF 的书,作者在整本书中都使用了 url-pattern *.jsf,为了在本书中没有任何问题,我决定和作者一样。您能告诉我或向我推荐有关 JSF 最佳实践的材料吗?
-
使用纯 JSF 2 教程。自 1.x 时代以来,有很多事情发生了变化。其中之一是 url-pattern,以前建议为
*.jsf,现在是*.xhtml,这样您始终将您的 facelet 原始代码 (.xhtml) 文件隐藏给最终用户。 JSF 2 第一个规范是在 2009 年完成的,它已经足够成熟,不再考虑新项目的 1.x。
标签: java maven jsf-2 web http-status-code-404