简体   繁体   English

JSF 2使用HTML页面而不是XHTML

[英]JSF 2 with HTML pages instead of XHTML

I am trying to get JSF setup to use files with a html extension. 我试图让JSF设置使用具有html扩展名的文件。

If I use a .xhtml pattern ( <url-pattern>*.xhtml</url-pattern> ) and name my files with a xhtml extension, the pages load correctly and get parsed. 如果我使用.xhtml模式( <url-pattern>*.xhtml</url-pattern> 。xhtml <url-pattern>*.xhtml</url-pattern> )并使用xhtml扩展名命名我的文件,则页面会正确加载并进行解析。

However, if I change this to <url-pattern>*.html</url-pattern> and naming the files with a .html extension, I get 500 an error: 但是,如果我将其更改为<url-pattern>*.html</url-pattern>并命名扩展名为.html的文件,则会出现500错误:

The server encountered an internal error () that prevented it from fulfilling this request.
Exception: java.lang.NullPointerException

Server logs: 服务器日志:

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
    at com.sun.faces.context.flash.ELFlash.getPhaseMapForReading(ELFlash.java:748)
    at com.sun.faces.context.flash.ELFlash.isEmpty(ELFlash.java:466)
    at com.sun.faces.facelets.util.DevTools.writeVariables(DevTools.java:318)
    at com.sun.faces.facelets.util.DevTools.writeVariables(DevTools.java:208)
    at com.sun.faces.facelets.util.DevTools.debugHtml(DevTools.java:123)
    at com.sun.faces.renderkit.RenderKitUtils.renderHtmlErrorPage(RenderKitUtils.java:1139)
    at com.sun.faces.context.ExceptionHandlerImpl.throwIt(ExceptionHandlerImpl.java:249)
    at com.sun.faces.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:136)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:119)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
    at java.lang.Thread.run(Thread.java:680)

Here is my web.xml file: 这是我的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">   
  <display-name>com.test.cms</display-name>

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

  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <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>*.html</url-pattern>
  </servlet-mapping>

</web-app>

However, if I change this to *.html and naming the files with a .html extension , I get 500 an error 但是,如果我将其更改为* .html 并命名扩展名为.html的文件,则会出现500错误

Did you mean that you renamed the physical .xhtml files to .html ? 你的意思是你将物理的.xhtml文件重命名为.html吗? You should not have the need to do that. 你不应该这样做。 Rename them back to .xhtml and keep using the .html in URLs. 将它们重命名为.xhtml并继续在URL中使用.html The FacesServlet will automatically load the right XHTML file associated with the URL. FacesServlet将自动加载与URL关联的正确XHTML文件。

If you really need to give them the .html extension, then you'd need to change the default suffix to .html as well. 如果你真的需要给它们.html扩展名,那么你也需要将默认后缀更改为.html Add the following entry to web.xml to achieve that: 将以下条目添加到web.xml以实现:

<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.html</param-value>
</context-param>

See also: 也可以看看:

As BalusC said you don't need to change the physical file names. 正如BalusC所说,您不需要更改物理文件名。

But anyway I spotted odd values in your web.xml schema. 但无论如何,我在你的web.xml架构中发现了奇怪的值。 There are mixed version 2_5 and 3_0 values. 混合版本2_5和3_0值。

With the settings below you should be able to keep files with .xhtml extension and access them as .html . 使用下面的设置,您应该能够保留扩展名为.xhtml文件并将其作为.html访问。

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <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>*.html</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

I tested it in GlassFish 3.1.2 with Mojarra 2.1.6 and it works fine. 我使用Mojarra 2.1.6在GlassFish 3.1.2中测试它,它工作正常。

I hope it helps. 我希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM