JSF <p:inputtext>在验证后未更新</p:inputtext>

时间:2014-11-11 16:22:09

标签: java spring forms jsf primefaces

我尝试使用Spring Security创建登录页面,但验证后用户名和密码未正确更新。如果我在Bean中设置属性,它会在页面加载时出现,但是如果我在xhtml中设置字段然后我提交它,我就无法获得更新的值。

以下是代码:

login.xhtml:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./WEB-INF/templates/template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:ezcomp="http://java.sun.com/jsf/composite/ezcomp">

<ui:define name="contenido">

<div class="mainContent">    

<h:form id="formLogin" prependId="false">
       <div class="form">
       <p><p:message for="formLogin" /></p>
       <p>
          <label>User Name <span>(Required Field)</span></label>
          <h:inputText id="j_username" label="User Name" value="#{loginMB.userName}"  required="true" />
          <label><p:message  for="j_username" /></label>
       </p>
       <p>
          <label>Password <span>(Required Field)</span></label>
          <h:inputSecret id="j_password" label="Password" value="#{loginMB.password}" required="true" />
          <label><p:message  for="j_password" /></label>
       </p>

  </div>
    <div class="buttons">
    <h:commandButton id="login" actionListener="#{loginMB.login}" value="Login" icon="ui-icon-person" />
  </div>
  <h:inputHidden value="#{loginMB.logoutHidden}" />
</h:form>
</ui:define>
</ui:composition>

LoginMB.java

@ManagedBean(name="loginMB")
@SessionScoped
@Component
public class LoginMB  implements Serializable {

    private static final long serialVersionUID = 1L;

    @NotEmpty
    @Size(min = 1, max = 25)
    private String userName;

    @NotEmpty
    @Size(min = 1, max = 25)
    private String password;


    @ManagedProperty(value="#{authenticationManager}")
    private AuthenticationManager authenticationManager;

    public AuthenticationManager getAuthenticationManager() {
        return authenticationManager;
    }

    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    public LoginMB() {}

    public String login() throws java.io.IOException {
            try {
                Authentication request = new UsernamePasswordAuthenticationToken(userName, password);
                Authentication result = authenticationManager.authenticate(request);
                SecurityContextHolder.getContext().setAuthentication(result);
                System.out.println("Login Success! ..");
                return "/admin/index.html";
        } catch (AuthenticationException ex) {
                System.out.println("Login Failed");
                FacesContext.getCurrentInstance().addMessage("formLogin", new FacesMessage(FacesMessage.SEVERITY_WARN,"Login Failed", "User Name and Password Not Match!"));           
            return "/login";
        }
    }

    public String logout() {
            SecurityContextHolder.getContext().setAuthentication(null);
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
                            .clear();
            return "/login";
    }

    public String getLogoutHidden() {
            SecurityContextHolder.getContext().setAuthentication(null);
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
                            .clear();
            return "logout";
    }

    public void setLogoutHidden(String logoutHidden) {
    }


    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;
    }

3 个答案:

答案 0 :(得分:0)

尝试使用action代替actionListener属性。当您单击按钮时,我希望您的方法不会从视图中触发。这是因为:

  

动作侦听器方法必须是具有ActionEvent参数和void返回类型的公共方法。

请参阅此处h:commandButton reference

答案 1 :(得分:0)

似乎重新渲染匹配表单的过程失败了:

您是否尝试将update属性添加到commandButton,并且该值应与表单ID匹配。

<h:commandButton id="login" update ="formLogin" actionListener="#{loginMB.login}" value="Login" icon="ui-icon-person" />

希望它能解决问题。

答案 2 :(得分:0)

最后我在spring配置文件中发现了错误,我把范围放了两次:

<bean id="LoginMB" name="LoginMB" class="com....LoginMB" scope="prototype">
  <property name="authenticationManager" ref="authenticationManager"></property>
</bean>

并纠正了它:

<bean id="LoginMB" name="LoginMB" class="com.....LoginMB">
  <property name="authenticationManager" ref="authenticationManager"></property>
</bean>