第四课:JSF\Richfaces中使用javabean

转贴:http://www.ceapet.com/topic35.html

Richfaces有很多控 件,这些控件都都用于前台页面设计,一个xhtml网页设计好了,比如,我需要用户输入一个数值,然后我通过后台处理,将结果显示在页面上,这该怎么做 呢?

这就需要 javabean,通过java编写好java类,然后在faces-config.xml中配置好这个javabean。这样就可以处理数据了。 javabean就是后台,或者说是动态的编程语言,类似php、asp、jsp等。

我们先设计一个login.xhtml页面,如下

 1     <html xmlns="http://www.w3.org/1999/xhtml"
 2           xmlns:ui="http://java.sun.com/jsf/facelets"
 3           xmlns:h="http://java.sun.com/jsf/html"
 4           xmlns:f="http://java.sun.com/jsf/core"
 5           xmlns:a4j="http://richfaces.org/a4j"
 6           xmlns:rich="http://richfaces.org/rich">
 7     <ui:composition>   
 8        <h:form>
 9           <h:panelGrid columns="10">
10              <h:outputText value="帐号:" />
11              <h:inputText value="#{login.NAME_}"/>
12              <h:outputText value="密码:"/>
13              <h:inputSecret  value="#{login.PASSWORD_}"/>
14              <h:commandButton
15                 image="http://#{header['host']}/images/icons/login.png"
16                 action="#{login.mLogin}"
17                 style="height:19px;">
18              </h:commandButton>
19           </h:panelGrid>
20        <h:form>
21     </ui:composition>
22     </html>
23 


上面这个页面使用了名为 login的javabean,有NAME_,PASSWORD_两个属性,和一个mLogin方法。

login的内容如下:

 1     package ceapet.jbpm;
 2     import java.util.*;
 3     public class login
 4     {
 5        private String NAME_,PASSWORD_;
 6        
 7        public void setNAME_(String data)
 8        {
 9           this.NAME_ = data;
10        }
11        public String getNAME_()
12        {
13           return this.NAME_;
14        }
15        public void setPASSWORD_(String data)
16        {
17           this.PASSWORD_ = data;
18        }
19        public String getPASSWORD_()
20        {
21           return this.PASSWORD_;
22        }
23 
24        public String mLogin()
25        {
26           Systemout.println(this.NAME_);
27           Systemout.println(this.PASSWORD_);
28           this.NAME_ = "yourname";
29           this.PASSWORD_ = "yourpassword";
30        }
31 
32        public String mLogout()
33        {
34        
35        }
36     }
37 

在faces-config.xml进行如下配置


代码
     <managed-bean>
      
<managed-bean-name>login</managed-bean-name>
      
<managed-bean-class>ceapet.jbpm.login</managed-bean-class>
      
<managed-bean-scope>request</managed-bean-scope>
    
</managed-bean>



 

managed- bean-scope可以填request、session或applicantion测试一下吧,login.xhtml中使用的是jsf标签,还没有 使用任何richfaces的标签,如果要使用的话,看看richfaces的demo,现学现用就可以了。

原文地址:https://www.cnblogs.com/liuzhengdao/p/1646329.html