JSF返回带有纯/原始XHTML/XML/EL源的空白/未解析页面,而不是呈现的HTML输出 [英] JSF returns blank/unparsed page with plain/raw XHTML/XML/EL source instead of rendered HTML output

查看:89
本文介绍了JSF返回带有纯/原始XHTML/XML/EL源的空白/未解析页面,而不是呈现的HTML输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Facelets文件,如下所示.

I have some Facelets files like below.


WebContent
 |-- index.xhtml
 |-- register.xhtml
 |-- templates
 |    |--userForm.xhtml
 |    `--banner.xhtml
 :

两个页面都使用/templates目录中的模板.我的/index.xhtml在浏览器中可以正常打开.我得到了生成的HTML输出.我在/index.xhtml文件中有一个链接到/register.xhtml文件.

Both pages are using templates from /templates directory. My /index.xhtml opens fine in browser. I get the generated HTML output. I have a link in /index.xhtml file to /register.xhtml file.

但是,我的/register.xhtml没有得到解析,并以纯XHTML/原始XML而不是其生成的HTML输出形式返回. #{...}形式的所有EL表达式均按原样显示,而不是打印其结果.当我在浏览器中右键单击页面并执行查看页面源代码时,我仍然看到原始的XHTML源代码,而不是生成的HTML输出.例如,<h:body>没有成为<body>.看起来模板没有被执行.

However, my /register.xhtml is not getting parsed and returns as plain XHTML / raw XML instead of its generated HTML output. All EL expressions in form of #{...} are displayed as-is instead of that their results are being printed. When I rightclick page in browser and do View page source, then I still see the original XHTML source code instead of the generated HTML output. For example, the <h:body> did not become a <body>. It looks like that the template is not being executed.

但是,当我在浏览器的地址栏中打开/faces/register.xhtml之类的/register.xhtml时,它将正确显示.这是怎么引起的,我该如何解决?

However, when I open the /register.xhtml like /faces/register.xhtml in browser's address bar, then it displays correctly. How is this caused and how can I solve it?

推荐答案

有三个主要原因.

  1. FacesServlet不被调用.
  2. XML名称空间URI丢失或错误.
  3. 已加载多个JSF实现.
  1. FacesServlet is not invoked.
  2. XML namespace URIs are missing or wrong.
  3. Multiple JSF implemenations have been loaded.


1.确保URL与FacesServlet映射

相匹配

链接的URL(您在浏览器的地址栏中看到的URL)必须与web.xml中定义的FacesServlet<url-pattern>相匹配,以便运行所有JSF作品. FacesServlet是负责解析XHTML文件,收集提交的表单值,执行转换/验证,更新模型,调用操作并生成HTML输出的文件.如果不通过URL调用FacesServlet,那么您将获得的所有内容(并通过右键单击浏览器中的 View Source 查看)确实是原始的XHTML源代码.


1. Make sure that URL matches FacesServlet mapping

The URL of the link (the URL as you see in browser's address bar) has to match the <url-pattern> of the FacesServlet as definied in web.xml in order to get all the JSF works to run. The FacesServlet is the one responsible for parsing the XHTML file, collecting submitted form values, performing conversion/validation, updating models, invoking actions and generating HTML output. If you don't invoke the FacesServlet by URL, then all you would get (and see via rightclick, View Source in browser) is indeed the raw XHTML source code.

如果<url-pattern>例如是*.jsf,则链接应指向/register.jsf,而不是/register.xhtml.如果像您一样,例如/faces/*,则链接应指向/faces/register.xhtml,而不是/register.xhtml.避免这种混淆的一种方法是将<url-pattern>/faces/*更改为*.xhtml.因此,以下是理想的映射:

If the <url-pattern> is for example *.jsf, then the link should point to /register.jsf and not /register.xhtml. If it's for example /faces/*, like you have, then the link should point to /faces/register.xhtml and not /register.xhtml. One way to avoid this confusion is to just change the <url-pattern> from /faces/* to *.xhtml. The below is thus the ideal mapping:

<servlet>
    <servlet-name>facesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

如果由于某种原因不能将<url-pattern>更改为*.xhtml,那么您可能还希望阻止最终用户通过URL直接访问XHTML源代码文件.在这种情况下,您可以在*.xhtml<url-pattern>上添加<security-constraint>,并在web.xml中添加一个空的<auth-constraint>,以防止出现这种情况:

If you can't change the <url-pattern> to *.xhtml for some reason, then you probably would also like to prevent endusers from directly accessing XHTML source code files by URL. In that case you can add a <security-constraint> on the <url-pattern> of *.xhtml with an empty <auth-constraint> in web.xml which prevents that:

<security-constraint>
    <display-name>Restrict direct access to XHTML files</display-name>
    <web-resource-collection>
        <web-resource-name>XHTML files</web-resource-name>
        <url-pattern>*.xhtml</url-pattern>
    </web-resource-collection>
    <auth-constraint />
</security-constraint> 

2017年4月推出的JSF 2.3已通过在webapp启动期间在*.xhtml的URL模式中自动注册FacesServlet来解决以上所有问题.因此,替代方法是简单地升级到应该是JSF 2.3或更高版本的最新可用JSF版本.但是理想情况下,您仍然应该仅在*.xhtml的一个URL模式上显式注册FacesServlet,因为对于完全相同的资源(例如/register.xhtml/register.jsf/register.faces/faces/register.xhtml)拥有多个可能的URL对SEO不利.

JSF 2.3 which was introduced April 2017 has already solved all of above by automatically registering the FacesServlet on an URL pattern of *.xhtml during webapp's startup. The alternative is thus to simply upgrade to latest available JSF version which should be JSF 2.3 or higher. But ideally you should still explicitly register the FacesServlet on only one URL pattern of *.xhtml because having multiple possible URLs for exactly the same resource like /register.xhtml, /register.jsf, /register.faces and /faces/register.xhtml is bad for SEO.

  • Set default home page via <welcome-file> in JSF project
  • Opening Facelets page errors with "This XML file does not appear to have any style information associated with it."
  • Sometimes I see JSF URL is *.jsf, sometimes *.xhtml and sometimes /faces/*. Why?
  • JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used
  • Which XHTML files do I need to put in /WEB-INF and which not?
  • Our servlets wiki - to learn the mandatory basics about servlets

自从JSF 2.2引入以来,另一个可能的原因是XML名称空间与JSF版本不匹配.如下所示的xmlns.jcp.org是JSF 2.2以来的新功能,不适用于较旧的JSF版本.症状几乎与未调用FacesServlet相同.

Since introduction of JSF 2.2, another probable cause is that XML namespaces don't match the JSF version. The xmlns.jcp.org like below is new since JSF 2.2 and does not work in older JSF versions. The symptoms are almost the same as if the FacesServlet is not invoked.

<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

如果无法升级到JSF 2.2或更高版本,则需要使用旧的java.sun.com XML名称空间:

If you can't upgrade to JSF 2.2 or higher, then you need to use the old java.sun.com XML namespaces instead:

<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">

但是理想情况下,您应该始终使用最新版本.

But ideally you should always use the latest version where available.

  • Which XML namespace to use with JSF 2.2 and up
  • JSF tags not executed
  • Warning: This page calls for XML namespace http://xmlns.jcp.org/jsf/XXX declared with prefix XXX but no taglibrary exists for that namespace

另一个可能的原因是您的Web应用程序已加载了多个JSF实现,彼此冲突和破坏.例如,当您的Web应用程序的运行时类路径被多个不同版本的JSF库污染,或者在特定的Mojarra 2.x + Tomcat 8.x组合中被污染时,Web应用程序的web.xml中有不必要的ConfigureListener条目导致其被加载两次.

One more probable cause is that multiple JSF implementations have been loaded by your webapp, conflicting and corrupting each other. For example, when your webapp's runtime classpath is polluted with multiple different versioned JSF libraries, or in the specific Mojarra 2.x + Tomcat 8.x combination, when there's an unnecessary ConfigureListener entry in webapp's web.xml causing it to be loaded twice.

<!-- You MUST remove this one from web.xml! -->
<!-- This is actually a workaround for buggy GlassFish3 and Jetty servers. -->
<!-- When leaving this in and you're targeting Tomcat, you'll run into trouble. -->
<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>

使用Maven时,请绝对确保以正确的方式声明依赖项,并了解依赖项作用域.重要的是,当目标服务器已经提供了依赖项时,请勿将它们捆绑在webapp中.

When using Maven, make absolutely sure that you declare the dependencies the right way and that you understand dependency scopes. Importantingly, do not bundle dependencies in webapp when those are already provided by the target server.

  • Configuration of com.sun.faces.config.ConfigureListener
  • How to properly install and configure JSF libraries via Maven?

对于不熟悉基本 HTML Servlet . Internet上有很多低质量的资源.请忽略由业余人员维护的代码片段抓取站点,这些站点主要侧重于广告收入而不是教学,例如roseindia,tutorialspoint,javabeat等.通过扰乱广告链接/横幅很容易识别它们.另外,请忽略处理侏罗纪JSF 1.x的资源.通过使用JSP文件而不是XHTML文件,可以轻松识别它们.自2009年JSF 2.0以来,已不推荐使用JSP作为视图技术.

JSF has a very steep learning curve for those unfamiliar with basic HTTP, HTML and Servlets. There are a lot of low quality resources on the Internet. Please ignore code snippet scraping sites maintained by amateurs with primary focus on advertisement income instead of on teaching, such as roseindia, tutorialspoint, javabeat, etc. They are easily recognizable by disturbing advertising links/banners. Also please ignore resources dealing with jurassic JSF 1.x. They are easily recognizable by using JSP files instead of XHTML files. JSP as view technology was deprecated since JSF 2.0 at 2009 already.

要以正确的方式开始使用,请从我们的JSF Wiki页面开始,并订购

To get started the right way, start at our JSF wiki page and order an authoritative book.

  • Java EE web development, where do I start and what skills do I need?
  • What is the need of JSF, when UI can be achieved with JavaScript libraries such as jQuery and AngularJS

这篇关于JSF返回带有纯/原始XHTML/XML/EL源的空白/未解析页面,而不是呈现的HTML输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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