JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Web Framework » JSF  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Dynamically Navigate Your JavaServer Faces.
caterpillar

良葛格

版主

發文: 2613
積分: 70
於 2005-02-08 19:38 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
In the article of “Your First JavaServer Faces”, there’s only one navigation rule, from “pages/index.jsp” to “pages/welcome.jsp”. How do you do if you’d like to dynamically navigate your pages according to user’s inputted data? For example, if user enters the wrong username or password, they should be sent back to “pages/index.jsp”. Only if user provides the correct username and password, the “pages/welcome.jsp” could be shown.

To archive this requirement, we provide a verify() method on our UserBean first.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package onlyfun.caterpillar;
 
public class UserBean {
    private String name;
    private String password;
    private String errMessage;
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setErrMessage(String errMessage) {
        this.errMessage = errMessage;
    }
    
    public String getErrMessage() {
        return errMessage;
    }
    
    public String verify() {
        if(!name.equals("justin") || 
           !password.equals("123456")) {
            errMessage = "incorrect name or password";
            return "failure";
        }
        else {
            return "success";
        }
    }
}


The verify() method returns two possible String. The “failure” would be returned if user’s name and password isn’t correct. Otherwise, the “success” would be returned. The returned String would be used for looking up navigation rules defined in “faces-config.xml”. We’ll see them soon.

If user enters the wrong username or password, they should know what’s wrong while their browser were sent back to “pages/index.jsp”. For this reason, we provide the “errMessage” assigned the error message, they would be shown in the “pages/index.jsp”.

Now let’s see how to modify our “pages/index.jsp” first.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<html>
<head>
<title>JSF Demo</title>
</head>
<body>
    <f:view>
        <h:form>
            <h3>Please enter your name and password.</h3>
            <h:outputText value="#{user.errMessage}"/><p>
             username: <h:inputText value="#{user.name}"/><p>
             password: <h:inputSecret value="#{user.password}"/><p>
            <h:commandButton value="Login" action="#{user.verify}"/>
        </h:form>
    </f:view>
</body>
</html>


<h:inputSecret> would provide a password field. What you enter in it would be shown as ‘*’ character. See the <h:commandButton> now. Its “action” attribute is now assigned as “#{user.verify}”. It tells FacesServlet that we’d like to invoke “user” bean’s verify() method while user click the “Login” Button. As we defined previously, the verify() method would see if user’s submitted data is correct and return the “failure” or “success” String.

Once the verify() method return the result String. Your browser would be navigated according to navigation rules defined in “faces-config.xml”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 <?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
    "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
    "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
 
<faces-config>
    <navigation-rule>
        <from-view-id>/pages/index.jsp</from-view-id>
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/pages/welcome.jsp</to-view-id>
        </navigation-case>
        <navigation-case>
            <from-outcome>failure</from-outcome>
            <to-view-id>/pages/index.jsp</to-view-id>
        </navigation-case>
    </navigation-rule>
        
    <managed-bean>
        <managed-bean-name>user</managed-bean-name>
        <managed-bean-class>onlyfun.caterpillar.UserBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
</faces-config>


If your verify() method returns “failure”, you’ll be navigated to “pages/index.jsp” according to the below.
1
2
3
4
        <navigation-case>
            <from-outcome>failure</from-outcome>
            <to-view-id>/pages/index.jsp</to-view-id>
        </navigation-case>


If the returned String is “success”, you’ll be navigated to “pages/welcome.jsp”.
1
2
3
4
        <navigation-case>
            <from-outcome>success</from-outcome>
            <to-view-id>/pages/welcome.jsp</to-view-id>
        </navigation-case>


caterpillar edited on 2005-02-09 10:28
reply to postreply to post
良葛格學習筆記
話題樹型展開
人氣 標題 作者 字數 發文時間
1846 Dynamically Navigate Your JavaServer Faces. caterpillar 4779 2005-02-08 19:38
» JWorld@TW »  Web Framework » JSF

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8