繁体   English   中英

JSF <p:inputText> 验证后不更新

[英]JSF <p:inputText> not updating after validation

我试图使用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;
    }

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

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

参见这里h:commandButton参考

似乎重新呈现匹配表单的过程失败了:

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

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

希望它能解决问题。

最后,我在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>

暂无
暂无

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

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