通过 <welcome-file> 设置默认主页在 JSF 项目中 [英] Set default home page via <welcome-file> in JSF project

查看:33
本文介绍了通过 <welcome-file> 设置默认主页在 JSF 项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从 Eclipse 使用 Tomcat 8.0 启动 Java EE 项目时,我无法设置在浏览器中加载的默认页面.我正在尝试学习 JSF,所以我关注了 本教程

I am unable to set a default page that loads in the browser when I start a Java EE project using Tomcat 8.0 from Eclipse. I am trying to learn JSF, so I followed this tutorial

一切正常,但是当我右键单击 login.xhtmlwelcome.xhtml 文件并选择运行方式/运行"时,我只能看到创建的页面在服务器上".

Everything works fine, but I can only see the created pages when I right click on the login.xhtml or welcome.xhtml file and choose "Run As/Run on Server".

到目前为止,当我启动整个项目时,我创建的所有其他 Web 应用程序都加载了默认页面.默认行为是加载 index.html 页面(或者可能是 index.jsp,如果有的话).所以我在项目中的 WEB-INF 文件夹中添加了 index.htmlindex.xhtml 页面,希望其中至少有一个被展示.然而,什么也没有发生.浏览器总是只显示 localhost:8080/JSFFaceletsTutorial/ URL 上的页面,但页面是干净的,甚至没有错误消息.我想我在解决这个问题的过程中一直收到错误 404,但是,我不再能够重现这个错误,我不记得是什么原因造成的.

So far, all the other web applications I have created loaded default page when I started the entire project. The default behavior is to load index.html page (or maybe index.jsp if there is some). So I added index.html and index.xhtml pages into my WEB-INF folder in the project, hoping that at least one of them will be shown. However, nothing happens. The browser always shows just the page on localhost:8080/JSFFaceletsTutorial/ URL, but the page is white clean, not even an error message. I think I have been getting error 404 in the process of solving this issue along the way, however, I am no longer able to reproduce this error and I don't remember what caused it.

我发现可以更改默认起始页面

但是,它对我也不起作用.无论我是否编辑 web.xml 文件,我都会得到相同的结果.

However, it doesn't work for me either. Regardless if I edit the web.xml file or not, I am getting the same result.

更令人费解的是,当我尝试更改 Web 浏览器时:窗口/Web 浏览器/..."在一段时间内,它在外部 Web 浏览器中的行为与在内部 Eclipse Web 浏览器中不同.内部总是空白页面 - 但外部网络浏览器曾经设法显示 index.html 页面 - 但它是一些过时的版本.尽管我非常确定我编辑了它,保存了更改,重新启动了服务器......但它仍然向我展示了该页面的过时版本.即使在这种情况下,它仍然忽略在 web.xml 文件中所做的更改.但是当我现在尝试时,它再次在所有浏览器中显示空白页面.除了编辑 web.xml 文件之外,我不知道我所做的任何更改...

What is even more puzzling, is that when I tried to change the web browser: "Window/Web Browser/..." it acted for a while differently in the external web browsers than in the internal Eclipse web browser. The internal had always blank page - but the external web browsers once managed to show the index.html page - but it was some outdated version. Despite I made absolutely sure that I edited it, saved the changes, restarted the server... and still, it showed me the outdated version of the page. And even in this case, it still ignored the changes made in the web.xml file. But when I am trying it now, it again shows blank white page in all browsers. I am not aware of any change I made except for editing web.xml file...

我的猜测是问题出在我还没有完全掌握的 JSF 技术中.这是因为当我选择使用右键单击Run As/Run on Server"运行 login.xhtmlwelcome.xhtml 页面时,这些页面的 URL 位于 localhost:8080: 带有路径 /JSFFaceletsTutorial/faces/login.xhtml/JSFFaceletsTutorial/faces/welcome.xhtml.这很奇怪,因为我的项目中没有任何目录faces".输入以下所有可能的排列:

My guess is that the problem is in the JSF technology I don't fully grasp yet. It's because when I choose to run the login.xhtml and welcome.xhtml pages using right click "Run As/Run on Server", the URL of those pages are on localhost:8080: with path /JSFFaceletsTutorial/faces/login.xhtml and /JSFFaceletsTutorial/faces/welcome.xhtml. That is weird, because I don't have any directory "faces" in my project. Typing all possible permutations of:

<welcome-file-list>
    <welcome-file>faces/index.html</welcome-file>
    <welcome-file>faces/index.xhtm</welcome-file>
</welcome-file-list>

web.xml 中也没有帮助.当我在那里输入完整地址时也无济于事.

in the web.xml didn't help either. It didn't help when I typed the full address there either.

这是我在控制台中收到的警告(我跳过了 INFO 日志条目):

Here are warnings I am getting in the console (I skipped the INFO log entries):

2014 年 12 月 19 日上午 9:39:55 org.apache.tomcat.util.digester.SetPropertiesRule 开始警告:[SetPropertiesRule]{Server/Service/Engine/Host/Context} 将属性source"设置为org.eclipse.jst.jee.server:JSFFaceletsTutorial"没有找到匹配的属性....警告:JSF1074:名为loginBean"的托管 bean 已被注册.用 com.tutorial.LoginBean 替换现有的托管 bean 类类型 com.tutorial.LoginBean.2014 年 12 月 19 日上午 9:39:57 org.apache.coyote.AbstractProtocol 开始"

"Dec 19, 2014 9:39:55 AM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:JSFFaceletsTutorial' did not find a matching property. ... WARNING: JSF1074: Managed bean named 'loginBean' has already been registered. Replacing existing managed bean class type com.tutorial.LoginBean with com.tutorial.LoginBean. Dec 19, 2014 9:39:57 AM org.apache.coyote.AbstractProtocol start"

我不确定这是否有帮助.我现在没有想法了.

I am not sure this is helpful though. I am out of ideas now.

推荐答案

首先, 不代表默认主页"的路径.它代表包含在文件夹中的物理文件的文件名,当 文件夹//foo/<请求/code>、/foo/bar/ 等.

First of all, the <welcome-file> does not represent the path to the "default home page". It represents the filename of the physical file contained in the folder which you'd like to serve up as default file when a folder like /, /foo/, /foo/bar/, etc is requested.

所以,在 JSF 2.x 风格中,基本上是:

So, in JSF 2.x flavor, that would basically be:

<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
</welcome-file-list>

这样,如果最终用户请求 / 并且您有 /index.xhtml,那么它将被提供.或者,如果最终用户请求 /foo 而你有 /foo/index.xhtml,那么它将被提供,等等.如果没有这样的文件,那么 404将返回错误.

This way, if the enduser requests / and you have /index.xhtml, then it will be served. Or, if the enduser requests /foo and you have /foo/index.xhtml, then it will be served, etc. If there is no such file, then a 404 error will be returned.

现在,您似乎已将 FacesServlet 映射到 /faces/* 的前缀 上.这是 JSF 1.0/1.1 时代遗留下来的,现在真的不推荐了.也许您正在阅读一个针对 JSF 1.x 的过时教程,或者一个维护不佳的教程,该教程最初是为 JSF 1.x 编写的,然后不小心针对 JSF 2.x 进行了更新,而不是从头开始重写.

Now, you appear to have mapped your FacesServlet on a prefix <url-pattern> of /faces/*. This is a leftover from JSF 1.0/1.1 ages and really not recommended these days. Perhaps you were reading an outdated tutorial targeted at JSF 1.x, or a poorly maintained tutorial which was originally written for JSF 1.x and then uncarefully updated for JSF 2.x instead of rewritten from scratch.

该教程似乎也没有向您解释一些 servlet 基础知识.即,为了让 XHTML 页面中的 JSF 组件运行并生成一些 HTML 输出,必须在请求 XHTML 页面时调用 FacesServlet.当您像这样请求 XHTML 页面 /index.xhtml,而 FacesServlet 被映射到 /faces/* 时,它不会t 被调用.然后浏览器将检索未解析的原始 JSF 源代码,而不是生成的 HTML 输出.您可以通过右键单击来查看它,在 webbrowser 中查看源代码.您应该像这样请求页面 /faces/index.xhtml 以便 FacesServlet 可以运行并生成浏览器可以理解和呈现的 HTML 输出.

That tutorial did also not seem to have explained you some servlet basics. Namely, in order to get JSF components in the XHTML page to run and generate some HTML output, the FacesServlet has to be invoked when the XHTML page is being requested. When you request the XHTML page like so /index.xhtml, while the FacesServlet is being mapped on /faces/*, then it won't be invoked. The browser would then retrieve the raw unparsed JSF source code instead of the generated HTML output. You can see it by rightclick, View Source in webbrowser. You should have requested the page like so /faces/index.xhtml so that the FacesServlet can run and produce HTML output which the browser can understand and present.

这与欢迎文件搭配得不好.这完全解释了为什么在使用 index.xhtml 作为欢迎文件时你会得到一个干净的白色"(空白)页面(一些像 IE 这样的劣质网络浏览器会混淆 提示下载对话框,因为包含原始 XHTML 源代码的响应中缺少/错误的内容类型).FacesServlet 根本没有被调用.只需摆脱老式的 /faces/* URL 模式并使用 JSF 2.x 思想的 *.xhtml URL 模式代替.

That only doesn't go well together with welcome files. This totally explains why you get a "clean white" (blank) page when using index.xhtml as welcome file (some inferior webbrowsers like IE would confusingly prompt a download dialog because of missing/wrong content type on the response containing raw XHTML source code). The FacesServlet was simply not being invoked. Just get rid of the old fashioned /faces/* URL pattern and use the JSF 2.x minded *.xhtml URL pattern instead.

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

这样欢迎文件应该可以工作,您可以通过直接请求其物理 URL 来打开 JSF 页面,而无需使用虚拟 URL.这在 JSF 1.x 中是不可能的,因为它会让 FacesServlet 在调用自身的无限循环中运行并导致堆栈溢出.

This way the welcome files should work and you can just open JSF pages by directly requesting its physical URL without hassling with virtual URLs. This was not possible in JSF 1.x because it would let the FacesServlet run in an infinite loop calling itself and cause a stack overflow.

至于这些警告,它们无关,但可以通过谷歌搜索.

As to those warnings, they are unrelated but quite googlable.

这篇关于通过 &lt;welcome-file&gt; 设置默认主页在 JSF 项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆