1

I am new to jsf and trying to build app. Problem is when i click Login commandButton nothing happens. So i input some print lines and it show's me that connection with database exist and arguments are valid but it wont redirect.

Here is index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Dezurstvo</title>
    </h:head>
    <h:body>
        Login
        <br />
        <h:form>
            <p:growl id="msgs" showDetail="true"/>
            <h:panelGrid columns="2" cellpadding="5">
                <h:outputText value="Username"/>
                <p:inputText value="#{logIn.username}" required="true"/>
                <h:outputText value="Password"/>
                <p:password id="pass" value="#{logIn.password}" feedback="false" required="true"/>
            </h:panelGrid>
            <p:commandButton value="Login" action="#{logIn.loadUser()}" update="msgs" />
            <p:commandButton value="Regisracija" />
        </h:form>
        <h:link outcome="adminMain" value="Primefaces welcome page" />
    </h:body>
</html>

NavigationBean class

package beans;

import dataBeans.Korisnik;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;


@ManagedBean(name = "navigation")
@RequestScoped
class NavigationBean {
        public String getHomepage(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        Korisnik kor = (Korisnik)session.getAttribute("username");
        if(!"A".equals(kor.getStatus()) || !"D".equals(kor.getStatus()) || !"N".equals(kor.getStatus()))
            return "index";
        else if("A".equals(kor.getStatus()))
            return "adminMain";
        else if("D".equals(kor.getStatus()))
            return "demonstratorMain";
        else if("N".equals(kor.getStatus()))
            return "nastavnikMain";
        return "";
    }
    public static String redirect(String status){
        if(!"A".equals(status) || !"D".equals(status) || !"N".equals(status))
            return "index";
        else if("A".equals(status))
            return "adminMain";
        else if("D".equals(status))
            return "demonstratorMain";
        else if("N".equals(status))
            return "nastavnikMain";
        return "error";
    }
}

Login Bean

  package beans;

import dataBeans.Korisnik;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

@ManagedBean(name="logIn")
@RequestScoped
public class LoginBean {
    private String user;
    private String pass;

    private UIComponent component;
    public UIComponent getComponent() {
        return component;
    }
    public void setComponent(UIComponent component) {
        this.component = component;
    }

    public LoginBean()  {}

    /**
     * @return the user
     */
    public String getUsername() {
        return user;
    }

    /**
     * @param username the user to set
     */
    public void setUsername(String username) {
        this.user = username;
    }

    /**
     * @return the pass
     */
    public String getPassword() {
        return pass;
    }

    /**
     * @param password the pass to set
     */
    public void setPassword(String password) {
        this.pass = password;
    }


    //Loading user into session
    public String loadUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        FacesMessage msg;
        Korisnik kor = Korisnik.getUser(user);

        if (user.equals(kor.getUsername())) {
            System.out.println( "user je = " + kor.getUsername());
            //User exists in database
            if (pass.equals(kor.getPassword())) {
                System.out.println( "Sifra je = " + kor.getPassword());
                System.out.println( "Status = " + kor.getStatus());
                //Pasword is OK              
                session.setAttribute(user, kor);
                return NavigationBean.redirect(kor.getStatus());
            } else {
                //Wrong pass
              //  System.out.println( "Sifra je = " + kor.getPassword());
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Pogrešna šifra.");
                return msg.toString();
            }
        } else {
            //Wrog user
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Korisnik sa datim korisničkim imenom ne postoji.");
            return msg.toString();
        }
    }

    public String signOut(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        session.invalidate();
        return "logIn";
    }
}

faces config

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">

    <navigation-rule>
        <from-view-id>/index.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>adminMain</from-outcome>
            <to-view-id>admin/adminMain.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>demonstratorMain</from-outcome>
            <to-view-id>demonstrator/demonstratorMain.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>nastavnikMain</from-outcome>
            <to-view-id>nastavnik/nastavnikMain.xhtml</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>error</from-outcome>
            <to-view-id>error.xhtml</to-view-id>
        </navigation-case>
    </navigation-rule>
</faces-config>

and web

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>

</web-app>

when button is click it call function loadUser from LoginBean and he call redirect from NavigationBean and there should redirect me to specific page depending on status of user, but nothing happens. Also when i input wrong data it show me

Warning:   JSF1091: No mime type could be found for file /javax.faces.application.FacesMessage@67cc4160.  To resolve this, add a mime-type mapping to the applications web.xml.
Warning:   JSF1064: Unable to find or serve resource, /javax.faces.application.FacesMessage@67cc4160.

don't know is this somehow connected to this.

I'm using glassfish server 4.1, primefaces 5.0, wamp, netbeans 8.0.2, jsf 2.2

Here is tree view if needed

tree view

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
DarkTemplar
  • 31
  • 11

1 Answers1

5

The string returned from a JSF action method must represent a JSF view ID like so:

public String submit() {
    // ...
    return "/some.xhtml";
}

Or a real redirect (all your cases were simple forwards, not redirects, get this terminology right too):

public String submit() {
    // ...
    return "/some.xhtml?faces-redirect=true";
}

or just null which means the same as "return to current page":

public String submit() {
    // ...
    return null;
}

If you're using navigation cases in faces-config.xml the old JSF 1.x style, then the returned string can also represent a navigation case outcome.

As to your concrete problem, you got the below warning in server log:

Warning:   JSF1064: Unable to find or serve resource, /javax.faces.application.FacesMessage@67cc4160.

This means that JSF didn't recognize a string value of /javax.faces.application.FacesMessage@67cc4160 as a valid JSF view ID. This basically means that you did like below in your action method:

public String submit() {
    // ...
    return "/javax.faces.application.FacesMessage@67cc4160";
}

And indeed, you're doing exactly this in the below lines!

    } else {
        //Wrong pass
        //  System.out.println( "Sifra je = " + kor.getPassword());
        msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Pogrešna šifra.");
        return msg.toString();
    }
} else {
    //Wrog user
    msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "", "Korisnik sa datim korisničkim imenom ne postoji.");
    return msg.toString();
}

This is not the right way to return to the current view with a faces message added to the context. Replace the return lines as below:

context.addMessage(null, msg);
return null;

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Thx fro you answer, but I'm not sure that i understand you completely. In NavigationBean, redirect function i change return to if(!"A".equals(status) || !"D".equals(status) || !"N".equals(status)) return "/index.xhtml?faces-redirect=true"; else if("A".equals(status)) return "admin/adminMain.xhtm?faces-redirect=true";; but the only thing that change is in url instead of localhost8080 now it writing http://localhost:8080/faces/index.xhtml after button clicked. Should i remove faceconfig or something else. really appreciate your help. – DarkTemplar Jul 05 '15 at 20:12
  • If you want to load a fresh new page (like as if you open a link/bookmark in browser), use a redirect. If you want to keep the current page open in order to display faces messages, return null. If you want to open a different page with therein the faces messages after submitting the form, then you've a potential design problem because nothing in your question indicates that you need that solution. – BalusC Jul 06 '15 at 05:50
  • Thx, I change the function loadUser and remove navigationBean and it worked. And about the warning, i change on bout places and it work for wrong password but when username is wrong it trows null.pointer exception on this line if (user.equals(kor.getUsername())). I don't understand, it should return null if user not exist in database compare it with user and jump to else branch. – DarkTemplar Jul 06 '15 at 20:00