`
glacier3
  • 浏览: 376702 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

JSF中UI控件binding属性的用法

阅读更多

以下内容为我在CSDN上转载的,版权归属原作者!   

在.Net中,binding是个十分容易理解的东西,但最近学习JSF,总理解不了binding属性的用法,查了一些资料,都是简单的说明,对这个属性没有什么例子。今天星期天,没事来到公司,突然对这个属性领悟了,觉得特别高兴。等我理解后,觉得这个属性是十分简单的,没理解之前,隔层窗户纸,总那么朦朦胧胧的。现在把这个小例子写出来,以做备忘。

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsf/core" %>
<html>
<head>
<title>
Simple jsp page
</title>
</head>  

<body>
<c:view>
<c:loadBundle basename="jsfResource" var="msg"></c:loadBundle>
<h:form>
<h:outputText value="#{msg.username}"></h:outputText> :
<h:inputText value="" binding="#{Backing.txtComponentInput}"></h:inputText>
<br>
<h:commandButton value="#{msg.btnSubmit}" actionListener="#{Backing.listen}"></h:commandButton>
</h:form>
</c:view>
</body>
</html>

index.jsp文件说明,16行就是binding绑定,18行是action事件。其实binding就是把控件的实例以属性的形式存储到bean里面,便于其他函数或方法操作这个控件。在.Net中,每个服务器控件就相当于一个类,服务器控件的属性就是类中的属性。程序编写人员对控件value,color等属性的操作其实就是对类中属性的操作(理解不正确之处,望给予斧正)。这里,JSF的思想和.Net是一致的。把控件的以实例的形式放到bean中,这样就可以用程序随意操作这个控件的各个属性了。

BackingBean.java

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 
import javax.faces.component.UIInput;
import javax.faces.event.ActionEvent;
import javax.faces.context.FacesContext;  

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2006-1-15
* Time: 10:02:59
* To change this template use File | Settings | File Templates.
*/
public class BackingBean {
UIInput txtComponentInput;

public UIInput getTxtComponentInput() {
return txtComponentInput;
}

public void setTxtComponentInput(UIInput txtComponentInput) {
this.txtComponentInput = txtComponentInput;
}

public void listen(ActionEvent e) {
FacesContext context = FacesContext.getCurrentInstance();
txtComponentInput.setValue("@@@@@@");
txtComponentInput.setRendered(true);
System.out.println(e.getComponent().getClientId(context));
}
}


BackingBean.java文件说明:主要是27,28行,其意思是当按钮的action事件触发后,就控件txtComponentInput进行操作,这里,我只是对Value和Rendered两个属性进行了操作,当然可以对其他属性进行随意操纵。这里的txtComponentInput其实就是<h:inputText value="" binding="#{Backing.txtComponentInput}"></h:inputText>,把此控件绑定到bean中,便于用程序对其进行动态控制。

faces-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!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>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<managed-bean>
<managed-bean-name>Backing</managed-bean-name>
<managed-bean-class>BackingBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>

faces-config.xml文件大家一看就是知道意思,就不多说了。
下面是运行截图.(当点击Submit按钮时,input被负值@@@@@@)

分享到:
评论
12 楼 蒲公英的约定 2013-08-10  
其實也可以通過尋找id來得到組建,不過還是綁定方便得多。不知道jsf能否在後臺去執行js函數
11 楼 蒲公英的约定 2013-08-10  
10 楼 heihay 2011-03-01  
有茅塞顿开之感,也应用到了项目当中
为了表示感谢,特地完成了测验留言^_^
9 楼 glacier3 2010-07-22  
我很久没有研究jsf了,这个都是在学校的时候开发一个项目的时候转载的。
8 楼 lucky16 2010-07-22  
最近在学习JSF,在参数的传递上就是对BackingBean不明白, 在网上搜索了一下也没有发现有价值的文章,你这篇文章很好,虽然是转载的,但是对理解BackingBean的作用很有帮助,现在已经理解了BackingBean的作用和用法了,呵呵
谢谢了!
如果楼主有时间,可以写写JSF的生命周期的问题,比如managed-bean的,呵呵,个人之见,有错误的地方还指正!
7 楼 成溪先生 2009-10-30  
如果是outputText 绑定到后台,并且页面还添加了验证,点击按钮,不能通过验证,该outputText 的值还能显示么?
6 楼 Doris_Dong 2009-03-16  
很好
5 楼 yourkenyang 2009-01-08  
4 楼 liuzchao 2008-10-20  
 
3 楼 liveman8988 2008-04-23  
2 楼 tiana4002 2007-10-28  
好,好,非常的好!!!
1 楼 tiana4002 2007-10-28  
好,好,非常好!!

相关推荐

Global site tag (gtag.js) - Google Analytics