-1

I developed a web in to login from a login.xhtml but I have issues navigating when it validates a user doesn't exist. Rather the page brings out an exception (EJB NoResultException ,getsingle Result()). The application navigates fine when I type the right username and password but it brings the exception page when the user doesn't exist.I am simply trying to achieve that when a user types a wrong password,he is been redirected to the login.xhtml.my code looks like This

//managed bean class
//session scoped

 private Admin ad = new Admin();
 private String username;
 private String password;
  //string setters getters

   // admin getters, setters

   public String login(){
   ad=  adminEJB.verifyUser(                                  
   username,password);
   If(ad!=null)
   return="welcome";
   else return ="login"; 


}

When I login in with the right username and password ,it navigates to the welcome.xhtml page perfectly but when I test it with a wrong password,it shows me the error page Ejb noresult exception. What could possibly be the problem, I want it to display the login page with a message to say wrong password or invalid account. Also is the idea of using a managedbean to login as sessionscoped the best practice?

4

1 に答える 1

1

ログインが正しくない場合、EJB が例外をスローするようです。この例外をキャッチして失敗を返す必要がある場合があります。

private UIComponent component;

    public UIComponent getComponent() {
        return component;
    }

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


           public String login(){

        try{
           ad=  adminEJB.verifyUser(                                  
           username,password);
           If(ad!=null)
              return="welcome";
           else {               
          FacesContext context = FacesContext.
               getCurrentInstance();
          context.addMessage(component.getClientId(),
              new FacesMessage("Login Failed")); 
          return "login";
         }

        }catch(EJBException e){
          FacesContext context = FacesContext.getCurrentInstance();
          context.addMessage(component.getClientId(),
            new FacesMessage(e.getMessage()));                                                                       return "login";
         }
      }

次に<h:message>、ログイン ページで を使用してログイン エラーを表示します。

    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets" 
        >

        <h:body>

            <h:form>
             <h:message for="login"/>
                <h:commandButton id="login" 
 value="click me" action="#{componentMsgBean.doAction}" 
binding="#{componentMsgBean.component}" />
            </h:form>

        </h:body>
    </html>

セッション Bean は、ユーザー セッションでユーザー名やロールなどのその他の関連データを保持し、任意の xhtml ページからアクセスできるため、ログインに適しています。また、ユーザー セッションごとに 1 回発生するため、セッション スコープ内に保持することは理にかなっています。

于 2013-03-02T16:05:04.677 に答える