容器管理的登录页面未呈现JSF组件

容器管理的登录页面未呈现JSF组件

本文介绍了容器管理的登录页面未呈现JSF组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了一个解决方案,但找不到针对我特定问题的答案.我有一个login.xhtml页面,但未呈现JSF标记.成功登录后,标记将正确呈现.因此,login.xhtml不会以某种方式通过faces servlet.这似乎很奇怪,因为所有配置均正确.如何强制login.xhtml正确呈现?

I searched for a solution but could not find an answer for my specific issue. I have a login.xhtml page but the JSF tags are not rendered. When I successfully log in, the tags are renderen correctly. so somehow the login.xhtml does not pass through the faces servlet. This seems strange because all is configured correctly. How can I force the login.xhtml to be rendered correctly?

这是我的web.xml部分

Here is my web.xml portion

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>file</realm-name>
    <form-login-config>
        <form-login-page>/login.xhtml</form-login-page>
        <form-error-page>/error.xhtml</form-error-page>
    </form-login-config>
</login-config>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

Login.xhtml

Login.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/templates/eps_template.xhtml">
<ui:define name="body">

    <h2>Hello, please log in:</h2>
    <form name="loginForm" method="post" action="j_security_check">
        <p>
            <strong><label for="username">Please type your user
                    name: </label> </strong> <input id="username" type="text" name="j_username"
                size="25" />
        </p>
        <p>
            <strong><label for="password">Please type your
                    password: </label> </strong> <input id="password" type="password" size="15"
                name="j_password" />
        </p>
        <p>
            <input type="submit" value="Submit" /> <input type="reset"
                value="Reset" />
        </p>
    </form>

</ui:define>
</ui:composition>

因此html表单正确显示,但模板组件未正确显示.输出为:

So the html form is shown correctly, but the template components are not. The output is:

<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/templates/eps_template.xhtml">
<ui:define name="body">

    <h2>Hello, please log in:</h2>
    <form name="loginForm" method="post" action="j_security_check">
        <p>
            <strong><label for="username">Please type your user
                    name: </label> </strong> <input id="username" type="text" name="j_username"
                size="25" />

        </p>
        <p>
            <strong><label for="password">Please type your
                    password: </label> </strong> <input id="password" type="password" size="15"
                name="j_password" />
        </p>
        <p>
            <input type="submit" value="Submit" /> <input type="reset"
                value="Reset" />
        </p>

    </form>

</ui:define>
</ui:composition>

2012年1月31日更新

Update 31 January 2012

配置:玻璃鱼3.1Primefaces 3.1JSF 2.1

Configuration:Glassfish 3.1Primefaces 3.1JSF 2.1

我有一个index.html页面,该页面转发到home.jsf.但是容器会注意到用户尚未登录,因此将其重定向到login.xhtml.

I have a index.html page which forwards to home.jsf. But the container notices the user is not logged in so it redirects to login.xhtml.

如果仅将* .jsf添加到web.xml中,则一切正常,但是login.jsf不会呈现JSF标记.

If I add only the *.jsf to the web.xml, all is working fine but the login.jsf does not render JSF tags.

<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>

如果我还添加* .xhtml,login.jsf页会渲染标签,但根据Primefaces外观,它不能正确显示.当我单击提交时,将jquery.js打印到屏幕上.

If I also add *.xhtml the login.jsf page does render tags but not properly according to the Primefaces skin. When I click submit the jquery.js is printed to the screen.

这是完整的web.xml

Here is the complete web.xml

<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>*.jsf</url-pattern>
    <url-pattern>*.xhtml</url-pattern>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>

<!-- Primefaces -->
<servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
    <url-pattern>/primefaces_resource/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Production</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>
<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>bluesky</param-value>
</context-param>
<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    <init-param>
        <param-name>thresholdSize</param-name>
        <param-value>5120</param-value>
    </init-param>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>/****</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>


<!-- security -->

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>file</realm-name>
    <form-login-config>
        <form-login-page>/login.xhtml</form-login-page>
        <form-error-page>/error.xhtml</form-error-page>
    </form-login-config>
</login-config>

<security-role>
    <role-name>epsadmin</role-name>
</security-role>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Admin</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

<security-constraint>
    <display-name>Restrict raw XHTML documents</display-name>
    <web-resource-collection>
        <web-resource-name>XHTML</web-resource-name>
        <url-pattern>*.xhtml</url-pattern>
    </web-resource-collection>
    <auth-constraint />
</security-constraint>

在BalusC回答2之后更新.现在是完整的web.xml

Update after answer 2 by BalusC. This is now the complete web.xml

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

<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Production</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>server</param-value>
</context-param>
<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>bluesky</param-value>
</context-param>
<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    <init-param>
        <param-name>thresholdSize</param-name>
        <param-value>5120</param-value>
    </init-param>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>/tempupload</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>


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


<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>file</realm-name>
    <form-login-config>
        <form-login-page>/login.jsf</form-login-page>
        <form-error-page>/error.jsf</form-error-page>
    </form-login-config>
</login-config>

<security-role>
    <role-name>admin</role-name>
</security-role>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Admin</web-resource-name>
        <url-pattern>*.jsf</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

<error-page>
    <error-code>403</error-code>
    <location>/errorpages/error403.jsf</location>
</error-page>
<error-page>
    <error-code>404</error-code>
    <location>/errorpages/error404.jsf</location>
</error-page>
<error-page>
    <error-code>500</error-code>
    <location>/errorpages/error500.jsf</location>
</error-page>
<error-page>
    <exception-type>com.*******.ApplicationException</exception-type>
    <location>/errorpages/error500.jsf</location>
</error-page>

这是我按下提交按钮时获得的网址

This is the url I get when pressing the submit button

csman/javax.faces.resource/primefaces.js.jsf?ln=primefaces&v=3.1-SNAPSHOT

这是输出.

PrimeFaces={escapeClientId:function(a){return"#"+a ..........etc

但是,当我再次按下connect到开始URL时,我被成功定向到home.jsf页面.这样登录过程就可以了.

However, when I press connect to the start URL again, I am successfully directed to the home.jsf page. So the login procedure went OK.

并非login.jsf页面显示JSF标记,但是它们没有皮肤.

Not that the login.jsf page displays JSF tags but they are not skinned.

很奇怪?!

推荐答案

您已将FacesServlet配置为侦听*.jsf URL.因此,您所需要做的就是更改登录页面和错误页面以完全指向该URL.

You have configured your FacesServlet to listen on *.jsf URLs. So all you need to do is to change the login and error pages to point to exactly that URL.

所以,改变

<form-login-page>/login.xhtml</form-login-page>
<form-error-page>/error.xhtml</form-error-page>

<form-login-page>/login.jsf</form-login-page>
<form-error-page>/error.jsf</form-error-page>

别忘了按照其他答案的建议删除 FacesServlet上的其他*.xhtml URL模式.您在*.xhtml上有一个<security-constraint>,以阻止最终用户看到原始资源,这将无法很好地协同工作.因此,您的FacesServlet映射应该只具有 :

Don't forget to remove the additional *.xhtml URL pattern on the FacesServlet as suggested by the other answer. You have a <security-constraint> on *.xhtml to block the endusers seeing the raw source and this won't work well together. So your FacesServlet mapping should have only this:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>


作为另一种选择,在*.jsf上用*.xhtml


As a different alternative, replace the mapping on *.jsf by *.xhtml

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

并坚持在整个网站的所有URL/链接中使用*.xhtml,并在*.xhtml删除 <security-constraint>.通过*.xhtml上的映射,最终用户无论如何也永远看不到原始源.每个单个XHTML文件都将通过FacesServlet传递.

and stick to using *.xhtml in all URLs/links throughout your website and remove the <security-constraint> on *.xhtml. With a mapping on *.xhtml the endusers will never see the raw source anyway. Every single XHTML file will be passed through the FacesServlet.

无关与具体问题相关,根据您对另一个问题的评论,您似乎在索引页面上有了元刷新.这没有什么意义.只需将<welcome-file>home.xhtml</welcome-file>添加到<welcome-file-list>.如果您已将FacesServlet映射到*.xhtml上,则效果很好.

Unrelated to the concrete problem, as per your comment on the other question you seem to have a meta refresh on the index page. This makes little sense. Just add <welcome-file>home.xhtml</welcome-file> to the <welcome-file-list>. This works fine if you've mapped FacesServlet on *.xhtml.

这篇关于容器管理的登录页面未呈现JSF组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:03