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

» JWorld@TW » Web Framework » JSF  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Simple Event-Driven JavaServer Faces. [精華]
caterpillar

良葛格

版主

發文: 2613
積分: 70
於 2005-02-09 10:24 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 “Dynamically Navigate Your JavaServer Faces”, you know how to dynamically navigate your browser to the destination page. It’s enough for you to write your own web application by using JSF. But do you know? JSF has some features that let you to write your web application just like writing a desktop application. One of these features is event-driven model.

For example, sometimes it’s not a good idea that you mix your verifying logic into your data bean object. I’d like to modify our previous login application by using event-driven model to separate the verify() method from the UserBean. First, the UserBean is rewritten as the following.
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
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;
    }
}


There’s no verify() method in the UserBean right now and then I write a LoginListener class.
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
 package onlyfun.caterpillar;
 
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.event.ActionEvent;
 
public class LoginListener {
    private String outcome;
    
    public void verify(ActionEvent e) {
        FacesContext context = FacesContext.getCurrentInstance();
        ValueBinding binding = context.getApplication().createValueBinding("#{user}");
        UserBean user = (UserBean) binding.getValue(context);
        
        if(!user.getName().equals("justin") || 
           !user.getPassword().equals("123456")) {
           user.setErrMessage("incorrect name or password");
           outcome = "failure";
        }
        else {
           outcome = "success";
        }
    }
    
    public String outcome() {
        return outcome;
    }
}


In the LoginListener, your verify() method receive an ActionEvent. It’s like your event object of desktop application. I’d like to verify user’s name and password so I should get the user bean.You could get some information about the event. I get user bean from the code.
1
2
3
FacesContext context = FacesContext.getCurrentInstance();
ValueBinding binding = context.getApplication().createValueBinding("#{user}");
UserBean user = (UserBean) binding.getValue(context);


We create value binding to user bean by using expression language “#{user}”. It’s defined in the “faces-config.xml”. Then we could get the property of user bean and verify user’s login information.

The following is our content of “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
25
26
27
28
29
30
<?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>
 
    <managed-bean>
        <managed-bean-name>loginListener</managed-bean-name>
        <managed-bean-class>onlyfun.caterpillar.LoginListener</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>


Now let’s modify our “pages/index.jsp”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ 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="登入" 
                             actionListener="#{loginListener.verify}"
                             action="#{loginListener.outcome}"/>
        </h:form>
    </f:view>
</body>


In the tag of <h:commandButton>, we define our actionListener to “#{loginListener.verify}”. While user clicks the button, the loginListener’s verify() method would be invoked. The action attribute is assigned to “#{loginListener.outcome}”. The returned value of outcome() method would be used to navigate your browser.


caterpillar edited on 2005-02-09 10:29
reply to postreply to post
良葛格學習筆記
» JWorld@TW »  Web Framework » JSF

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8