【问题标题】:How to change the start page of my web project?如何更改我的 Web 项目的起始页?
【发布时间】:2011-09-01 14:59:13
【问题描述】:

当我在 Eclipse 中创建一个新项目时,它自动为我创建了一个 index.jsp 页面,我不希望起始页面是 .jsp,我希望它是一个 .xhtml 这就是我在 web.xml 中所做的:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<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>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>myStartPage.xhtml</welcome-file>
</welcome-file-list>
</web-app> 

当我在 localhost 中运行项目时,上面的代码不允许我将页面 myStartPage.xhtml 作为第一页。

我应该如何修改它以使浏览器为我显示起始页。 我也不想使用任何 url 模式。这是强制性的吗?(我尝试删除该标签,但没有构建)。

【问题讨论】:

  • 目前点击http://server:port/appContext/时会显示什么??
  • 当我尝试时:http://localhost:8080/projectname/ 我得到 HTTP 状态 500 javax.servlet.ServletException: The FacesServlet cannot have a url-pattern of /*.

标签: java eclipse jsf jsf-2 java-ee-6


【解决方案1】:

试试这个 servlet 映射:

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

这适用于 Glassfish 3。

【讨论】:

  • 没错。现在我将该页面视为起始页面。但是有一点我不明白:为什么是 URL,当我 lauch 只是说:http://localhost:8080/projectname/ 而不是 http://localhost:8080/scarecrow1/myStartPage.xhtml
  • 因为它在内部转发请求,所以 URL
  • @sfrj - 有关这方面的最新文档,请参阅 Servlet 3.0 spec 的第 10.10 章。
  • 非常有用的 .pdf,我阅读了第 10.10 章,我现在了解欢迎文件现在是如何工作的。谢谢你:)
【解决方案2】:

据我所知,总是会显示 index.jsp。您可以向 index.jsp 添加重定向:

<% response.sendRedirect("myStartPage.xhtml"); %>

但可能有更好的解决方案。

【讨论】:

  • 我知道我可以重定向,但这不是我需要的。
猜你喜欢
  • 2012-07-22
  • 2013-02-16
  • 2019-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
  • 2015-03-25
相关资源
最近更新 更多