通过< welcome-file>设置默认主页在JSF项目中 [英] Set default home page via <welcome-file> in JSF project
问题描述
当我使用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.xhtml
或 welcome.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.html
和 index.xhtml
code>文件夹,希望至少有一个将被显示。但是,什么也没有。浏览器总是只显示 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浏览器中的作用与内部Eclipse Web浏览器不同。内部一直是空白页面 - 但外部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运行 login.xhtml
和 welcome.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 AM org.apache.tomcat.util.digester.SetPropertiesRule开始
警告:[SetPropertiesRule ] {服务器/服务/引擎/主机/上下文}将属性'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 start
"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.
推荐答案
首先,< welcome -file>
不表示默认主页的路径。当文件夹如 /
,时,它表示您要作为默认文件提供的文件夹中包含的物理文件的文件名code> / foo / , / 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
前缀< url-pattern>
/ 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 / *
,则不会被调用。然后,浏览器将检索未解析的原始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
作为欢迎文件时,会得到一个干净的白色(空白)页面(一些劣质的web浏览器,如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>
这样欢迎文件应该可以正常工作,只需打开JSF页面即可直接请求其物理URL,而不需要用虚拟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.
- JSF Facelets:有时我看到URL是.jsf,有时是.xhtml。为什么?
- 在WAS服务器上设置应用程序URL,/ faces /来自哪里?
- 使用.jsp或.xhtml创建JSF页面有什么区别?或.jsf扩展名
- 为什么web.xml欢迎文件位于WEB-INF中
- 如何使用子文件夹作为web.xml欢迎目录
- JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
- Setting application URL on WAS server, where does /faces/ come from?
- What is the difference between creating JSF pages with .jsp or .xhtml or .jsf extension
- Why can web.xml welcome-file be located inside WEB-INF
- How to use a sub-folder as web.xml welcome directory
对于这些警告,他们是不相关的,但很可笑。
As to those warnings, they are unrelated but quite googlable.
这篇关于通过< welcome-file>设置默认主页在JSF项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!