문제

The form doesn't submit to the bean.

login.xhtml

<h:form>
    <p:panel header="Login">  
        <p:messages id="msgs" showDetail="true"/>
        <h:panelGrid columns="2" columnClasses="column" cellpadding="5">
            <h:outputLabel for="user" value="Username" />
            <h:inputText id="user" value="#{login.username}" />

            <h:outputLabel for="pw" value="Passwort" />
            <h:inputSecret id="pw" redisplay="false" value="#{login.password}" />
        </h:panelGrid>
        <p:button value="Anmelden" action="#{login.login}" type="submit" />
    </p:panel>
</h:form>

Login.java

@ManagedBean
@ViewScoped
public class Login {

    private FacesContext fCtx;
    private String username;
    private String password;

    public Login() {
        fCtx = FacesContext.getCurrentInstance();
    }

    public String login(){
        HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
        String sessionId = session.getId();
        fCtx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Info: ", getUsername()+", "+getPassword()+", "+sessionId));  

        return "login.xhtml";
    }


    public void setUsername(String username) {
        this.username = username;
    }
    public String getUsername() {
        return username;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getPassword() {
        return password;
        }
    }

There are even no exceptions in the logs. What's the problem and how can I fix it?

도움이 되었습니까?

해결책

The <p:button> doesn't submit the parent form, it's just a hook to be able to execute some JS code on button press without the intent to submit any POST form. Replace it by <p:commandButton>. Also, you'd like to add update="msgs" to refresh the component with id="msgs" after submit.


Unrelated to the concrete problem, you don't want to use getSession(false) since it may return null. Just use getSession(true).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top