2

I need my JSF page to show the return value of the invoked method without redirecting to another page or refreshing existing. Or maybe I don't understand completely the problem, so I ask to guide me, how to show what bean returns (it returns some String). If it returns "home", then home.xhtml is displayed - this part works. But if it shows something else, I need it to wrote on the page. My page is below, how to change it to attain needed result?

<!DOCTYPE html>
<html 
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>login</title>
</h:head>
<h:body>
    <h:form id="loginForm">
        <p:growl id="msg" showDetail="true" life="3000" />
        <p:panel header="Login" style="width: 360px;">
            <h:panelGrid id="loginPanel" columns="2">
                <h:outputText value="Username" />
                <p:inputText id="username" value="#{loginBean.userName}"></p:inputText>
                <p:spacer></p:spacer>
                <p:message for="username"></p:message>
                <h:outputText value="Password" />
                <p:password id="password" value="#{loginBean.password}"
                    feedback="false"></p:password>
                <p:spacer></p:spacer>
                <p:message for="password"></p:message>
                <p:spacer></p:spacer>
                <p:commandButton action="#{loginBean.login()}" value="Login"
                    update="loginForm" ajax="true"></p:commandButton>
            </h:panelGrid>
        </p:panel>
    </h:form>
</h:body>
</html>

My LoginBean class:

@ManagedBean(name = "loginBean")
@SessionScoped

public class LoginBean implements Serializable {
    private String userName;
    private String password;
    private String role;
    private String msg;

setters and getters for all fields are omitted

    public String login() {

        UserBean result = UserDao.login(userName, password);

        if (!result.isWrongUserName() && !result.isWrongPassword()) {
            HttpSession session = Util.getSession();
            session.setAttribute("username", userName);
            session.setAttribute("role", role);
            return "home";
        } else if (result.isWrongUserName()) {
            ???????????
        } else {
            ???????????
        }
    }

    public String logout() {
        HttpSession session = Util.getSession();
        session.invalidate();
        return "login";
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Battle_Slug
  • 2,055
  • 1
  • 34
  • 60
  • That text is not understandable in JSF terminology. A "*return value*" of an `action` method (associated with a ``) represents a navigation case outcome that can be used to navigate to a different page - it could be the same page too (thus, a post back) that you are doing already. – Tiny Jan 24 '15 at 06:54
  • Ok, I did some experiments and understood that too, thank you for confirming my thought. I will amend the question. What I need is to tell the user explicitly: whether the username is wrong or password is wrong. How to do this then? – Battle_Slug Jan 24 '15 at 07:01
  • These return statements `return "Wrong UserName";` and `return "Wrong Password";` attempt to imply that you want to show error messages implied by the return values. It is just a validation thing and should be handled that way. (as an off side note, I would only display an (error) message representing a combination of user name and/or password is wrong hereby not specifically implying/telling either a user name or a password is wrong that can make a malicious user's life easy). JSF supports a validation scheme as a separation of concerns. It could be done using a custom validator of your need. – Tiny Jan 24 '15 at 07:10
  • The point is to make a malicious user's life easy like this done, for example, on Facebook... I cannot do that another way. Could you elaborate how to implement custom validator then? – Battle_Slug Jan 24 '15 at 07:17
  • And still would be good to know how to output here what the method returns given that the method is not login thing... – Battle_Slug Jan 24 '15 at 07:18
  • 2
    Given [here](http://stackoverflow.com/a/6047913/1391249) an example of a custom validator. All you need to do is annotate a class of your choice using a `@FacesValidator` annotation and implement a `Validator` interface (from `javax.faces.validator`) and override its `public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException{...}` method. – Tiny Jan 24 '15 at 15:31
  • You can put any logic of your choice in that method. If you however, need to inject an EJB as seems to be a mandatory need in this case, for example, into a validator, `@FacesValidator` will not work. You will need to turn the validator to a managed bean to inject an EJB(s) into the validator. These are such examples, [1](http://stackoverflow.com/a/7572413/1391249), [2](http://stackoverflow.com/a/20146283/1391249). [This](http://stackoverflow.com/a/6282509/1391249) is about cross-field validation in JSF, if need somewhere else in your application. – Tiny Jan 24 '15 at 15:32
  • The return value of an `action` method represents a navigation case outcome as already stated. [This answer](http://stackoverflow.com/a/3909382/1391249) is worth reading about action and actionListener. – Tiny Jan 24 '15 at 15:35
  • Thank you for all that things, I will read carefully and try – Battle_Slug Jan 24 '15 at 21:55
  • You're basically asking how to show a validation message on the page, right? Indeed, use a normal validator. Is this question then acceptable as dupe? http://stackoverflow.com/questions/6047866/how-to-perform-validation-in-jsf-how-to-create-a-custom-validator-in-jsf/ – BalusC Jan 25 '15 at 22:40
  • I don't think it's a dupe, let's leave it as is. Anyway, thank you. – Battle_Slug Jan 26 '15 at 03:18

0 Answers0