JSF支持bean未通过H:CommandButton调用,页面刷新



我有一个登录页面,按钮提交我希望调用备用bean方法。单击"提交"按钮时调用@PostConstruct方法,但未调用BAIN BEAN(登录方法)中的方法。没有显示错误消息,并且日志中没有错误。

这是我的login.xhtml页面:

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Login</title>
</h:head>
<h:body>
    <p />
    <h:form id="login" prependId="false">
        <h:messages globalOnly="true" />
        <h:outputLabel for="username" value="Username" />
        <h:inputText id="username" value="#{user.username}" required="true" />
        <h:message for="username" />
        <br />
        <h:outputLabel for="password" value="Password" />
        <h:inputSecret id="password" value="#{user.password}" required="true" />
        <h:message for="password" />
        <br />
        <h:commandButton value="Sign in" actionListener="#{user.login}" action="#{user.login}" />
    </h:form>
</h:body>
</html>

这是我的背bean:

@ViewScoped
@Named(value = "user")
public class UserController implements Serializable {
    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
    private String originalURL;
    @PostConstruct
    public void init() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        originalURL = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);
        if (originalURL != null) {
            String originalQuery = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_QUERY_STRING);
            if (originalQuery != null) {
                originalURL += "?" + originalQuery;
            }
        }
    }
    public void login() throws IOException {
        HttpServletRequest request = WebContext.getRequest();
        try {
            request.login(username, password);
            if (originalURL != null) {
                WebContext.redirect(originalURL);
            } else {
                WebContext.redirect("/index.xhtml");
            }
        } catch (ServletException e) {
            WebContext.addMessage(null, new FacesMessage("Incorrect username or password!"));
        }
    }
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
}

我已将登录到跟踪,单击提交按钮后的输出如下:

2014-02-02 13:35:01,881 DEBUG [org.jboss.weld.Conversation] (default task-6) WELD-000327: Resuming conversation with id null
2014-02-02 13:35:01,882 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) servletPath /login.xhtml
2014-02-02 13:35:01,882 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) pathInfo null
2014-02-02 13:35:01,882 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) URL pattern of the FacesServlet executing the current request .xhtml
2014-02-02 13:35:01,882 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-6) execute(com.sun.faces.context.FacesContextImpl@115359ad)
2014-02-02 13:35:01,883 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-6) Entering RestoreViewPhase
2014-02-02 13:35:01,883 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-6) New request: creating a view for /login.xhtml
2014-02-02 13:35:01,883 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) URL pattern of the FacesServlet executing the current request .xhtml
2014-02-02 13:35:01,883 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) URL pattern of the FacesServlet executing the current request .xhtml
2014-02-02 13:35:01,883 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.ViewRoot'
2014-02-02 13:35:01,884 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created new view for /login.xhtml
2014-02-02 13:35:01,884 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Locale for this view as determined by calculateLocale en_US
2014-02-02 13:35:01,884 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) RenderKitId for this view as determined by calculateRenderKitId HTML_BASIC
2014-02-02 13:35:01,884 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.factory] (default task-6) Resource-Url from external context: file:/opt/jboss/standalone/tmp/vfs/temp/temp95c150145d0990a0/content-200a83a1e489dda9/login.xhtml
2014-02-02 13:35:01,885 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-6) Exiting RestoreViewPhase
2014-02-02 13:35:01,885 FINE  [javax.enterprise.resource.webcontainer.jsf.timing] (default task-6)  [TIMING] - [2ms] : Execution time for phase (including any PhaseListeners) -> RESTORE_VIEW 1
2014-02-02 13:35:01,885 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-6) render(com.sun.faces.context.FacesContextImpl@115359ad)
2014-02-02 13:35:01,885 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-6) Entering RenderResponsePhase
2014-02-02 13:35:01,885 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-6) About to render view /login.xhtml
2014-02-02 13:35:01,885 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Building View: /login.xhtml
2014-02-02 13:35:01,885 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.factory] (default task-6) Resource-Url from external context: file:/opt/jboss/standalone/tmp/vfs/temp/temp95c150145d0990a0/content-200a83a1e489dda9/login.xhtml
2014-02-02 13:35:01,886 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.Output'
2014-02-02 13:35:01,886 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @5,9 <h:head> Component[2030916047_790d51c2] Created: javax.faces.component.UIOutput
2014-02-02 13:35:01,887 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.OutputBody'
2014-02-02 13:35:01,887 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @9,9 <h:body> Component[2030916047_790d51e8] Created: javax.faces.component.html.HtmlBody
2014-02-02 13:35:01,887 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.HtmlForm'
2014-02-02 13:35:01,887 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @14,22 <h:form> Component[2030916047_790d518e] Created: javax.faces.component.html.HtmlForm
2014-02-02 13:35:01,888 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.HtmlMessages'
2014-02-02 13:35:01,888 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @15,36 <h:messages> Component[2030916047_790d5194] Created: javax.faces.component.html.HtmlMessages
2014-02-02 13:35:01,888 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.HtmlOutputLabel'
2014-02-02 13:35:01,888 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @16,52 <h:outputLabel> Component[2030916047_790d51a7] Created: javax.faces.component.html.HtmlOutputLabel
2014-02-02 13:35:01,889 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.HtmlInputText'
2014-02-02 13:35:01,889 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @17,73 <h:inputText> Component[2030916047_790d51ba] Created: javax.faces.component.html.HtmlInputText
2014-02-02 13:35:01,889 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) created validator of type 'javax.faces.Bean'
2014-02-02 13:35:01,889 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.HtmlMessage'
2014-02-02 13:35:01,889 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @18,31 <h:message> Component[2030916047_790d514d] Created: javax.faces.component.html.HtmlMessage
2014-02-02 13:35:01,890 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.HtmlOutputLabel'
2014-02-02 13:35:01,890 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @20,81 <h:outputLabel> Component[2030916047_790d5140] Created: javax.faces.component.html.HtmlOutputLabel
2014-02-02 13:35:01,890 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.HtmlInputSecret'
2014-02-02 13:35:01,891 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @21,75 <h:inputSecret> Component[2030916047_790d5153] Created: javax.faces.component.html.HtmlInputSecret
2014-02-02 13:35:01,891 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) created validator of type 'javax.faces.Bean'
2014-02-02 13:35:01,891 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.HtmlMessage'
2014-02-02 13:35:01,891 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @22,31 <h:message> Component[2030916047_790d5166] Created: javax.faces.component.html.HtmlMessage
2014-02-02 13:35:01,891 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.HtmlCommandButton'
2014-02-02 13:35:01,891 FINE  [javax.enterprise.resource.webcontainer.jsf.facelets.tag.component] (default task-6) /login.xhtml @24,92 <h:commandButton> Component[2030916047_790d5179] Created: javax.faces.component.html.HtmlCommandButton
2014-02-02 13:35:01,892 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Rendering View: /login.xhtml
2014-02-02 13:35:01,894 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) URL pattern of the FacesServlet executing the current request .xhtml
2014-02-02 13:35:01,894 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) URL pattern of the FacesServlet executing the current request .xhtml
2014-02-02 13:35:01,895 FINE  [javax.enterprise.resource.webcontainer.jsf.renderkit] (default task-6) component.getValue() returned Username
2014-02-02 13:35:01,895 FINE  [javax.enterprise.resource.webcontainer.jsf.renderkit] (default task-6) Value to be rendered Username
2014-02-02 13:35:01,895 FINE  [javax.enterprise.resource.webcontainer.jsf.renderkit] (default task-6) component.getValue() returned null
2014-02-02 13:35:01,895 FINE  [javax.enterprise.resource.webcontainer.jsf.renderkit] (default task-6) Value to be rendered null
2014-02-02 13:35:01,895 FINE  [javax.enterprise.resource.webcontainer.jsf.renderkit] (default task-6) component.getValue() returned Password
2014-02-02 13:35:01,895 FINE  [javax.enterprise.resource.webcontainer.jsf.renderkit] (default task-6) Value to be rendered Password
2014-02-02 13:35:01,896 FINE  [javax.enterprise.resource.webcontainer.jsf.renderkit] (default task-6) component.getValue() returned null
2014-02-02 13:35:01,896 FINE  [javax.enterprise.resource.webcontainer.jsf.renderkit] (default task-6) Value to be rendered null
2014-02-02 13:35:01,896 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Begin writing marker for viewId /login.xhtml
2014-02-02 13:35:01,896 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) End writing marker for viewId /login.xhtml
2014-02-02 13:35:01,896 FINE  [javax.enterprise.resource.webcontainer.jsf.application] (default task-6) Created component with component type of 'javax.faces.HtmlMessages'
2014-02-02 13:35:01,897 FINE  [javax.faces.component] (default task-6) No renderer-type for component j_id1
2014-02-02 13:35:01,897 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-6) Exiting RenderResponsePhase
2014-02-02 13:35:01,897 FINE  [javax.enterprise.resource.webcontainer.jsf.timing] (default task-6)  [TIMING] - [12ms] : Execution time for phase (including any PhaseListeners) -> RENDER_RESPONSE 6
2014-02-02 13:35:01,898 TRACE [org.jboss.security] (default task-6) PBOX000354: Setting security roles ThreadLocal: null
2014-02-02 13:35:11,597 DEBUG [org.jboss.ejb.client.txn] (Periodic Recovery) Send recover request for transaction origin node identifier 1 to EJB receiver with node name localhost

任何帮助都将不胜感激,因为我现在花了很多时间试图解决这个问题。谢谢

问题是我在Web.xml中的config正在引起误差并阻止beans方法被调用。

我从我的web.xml中删除了以下内容,而背bean方法被调用:

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>mysqldomain</realm-name>
    <form-login-config>
        <form-login-page>/login.xhtml</form-login-page>
        <form-error-page>/index.xhtml?error=true</form-error-page>
    </form-login-config>
</login-config>

最新更新