javax.faces.DEFAULT_SUFFIX not working

前端 未结 1 927
粉色の甜心
粉色の甜心 2021-02-06 13:38

Ive been reading some posts about javax.faces.default_suffix but without success when trying to implement it.

Using : jsf 2.0, jboss 7.1, Mojarra 2.1.5

  • I n
相关标签:
1条回答
  • 2021-02-06 13:54

    You're mixing the meaning of the default suffix and the URL pattern.

    The javax.faces.DEFAULT_SUFFIX represents the default suffix of the physical file you've in your webapplication which represents a JSF file. This defaults in JSF 2.0 to .xhtml. If you change it to .jsf, then you should rename all physical files from some.xhtml to some.jsf. This makes generally no utter sense. You should not do that, just get rid of that context param altogether.

    The <url-pattern> represents the default URL pattern which the enduser has to use in request URL in order to invoke the FacesServlet (which in turn uses the default suffix configuration to locate the physical file based on the URL). You said that you want to use *.jsf in URLs, however you have set it to *.xhtml. This is not right and changing the default suffix is not the right solution.

    You should just set the URL pattern alone, not the default suffix.

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    

    This way http://localhost:8080/myproject/index.jsf will work.

    Then there's a third problem: you're completely misunderstanding the purpose of the welcome file. It should not represent the path to the homepage. It should represent the filename of the physical file which you'd like to serve up as default file when a folder like /, /foo/, /foo/bar/, etc is requested. Just set it to index.jsf.

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

    However, you should keep in mind that the container will verify the existence of the physical file before continuing the request, so that it can properly show a 404 error if absent. As *.jsf is actually a virtual URL, that step will fail. You can solve that by fooling the container by placing a physically existing but empty index.jsf file next to the index.xhtml file in the desired folder.

    This way http://localhost:8080/myproject/ will work, provided that you have a real index.xhtml file and empty index.jsf file in the root folder.

    Much easier is to just get rid of virtual URLs and stick to *.xhtml all the time.

    See also:

    • JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
    • JSF welcome file is not recognized
    • richfaces + index.xhtml having errors
    0 讨论(0)
提交回复
热议问题