Tapestry的Form内组件组件运用总结

本文详细介绍了Tapestry框架中的多种组件,包括Insert、TextField、TextArea等,解释了它们的工作原理及应用场景,如数据绑定、表单处理等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A) Insert 组件
e.g.
    <input type="text" jwcid="name @Insert" value=" ognl:user.name"/>
    页面表现时,将会到页面类中寻找getUser().getName()方法获取初值并输出
    相当于在页面上显示数据.

B) TextField 组件
e.g.
    <input type="text" jwcid="username @TextField" value=" ognl:username"/>
    页面表现时,将会到页面类中寻找getUsername()方法获取初值
    *如果是修改信息页面,通常初始值要在页面表现之前由setUsername()手动设置从数据库中读取出来的值
    表单提交时,通过setUsername()写入新值(即用户输入值),在类中通过getUsername()获取新值
    相当于在修改个人信息时,首先读出用户名赋予文本框(用户名)初值,用户修改时填入新值,后台获取之
    *Hidden属性区分是普通文本输入框(默认false)和密码输入框(hidden="ognl:true")
    readonly属性设置只读 readonly="true"为只读(后台可读取)
    *disabled属性设置是否可写 diabled="true"为不可写(后台也不可读取)

C) TextArea 组件
e.g.
    <textarea jwcid="content @TextArea" value=" ognl:content" cols="40" rows="10"></textarea>
    页面表现时,将会到页面类中寻找getContent()方法获取初值
    工作原理同TextField

D) RadioGroup/Radio 组件
e.g.
    <span jwcid="headImage @RadioGroup" selected=" ognl:headImage">
      <input jwcid=" @Radio" type="radio" value="1"/>头像1
      <input jwcid=" @Radio" type="radio" value="2"/>头像2
      <input jwcid=" @Radio" type="radio" value="3"/>头像3
      <input jwcid=" @Radio" type="radio" value="4"/>头像4
      <input jwcid=" @Radio" type="radio" value="5"/>头像5
      <input jwcid=" @Radio" type="radio" value="6"/>头像6
    </span>
    RadioGroup为每一个Radio提供一个唯一的ID。RadioGroup跟踪当前被选中的属性值,并且只有一个Radio能够被选中.
    页面提交时,RadioGroup组件就利用OGNL表达式向headImage字段写入被选中的Radio组件的value参数值.
    页面表现时(修改页面),将会到页面类中寻找getHeadImage()方法获取初值,然后寻找 @Radio组件中与其相同的组件并勾选上.

E) PropertySelection 组件
    使用PropertySelection组件必须要构造一个类来实现IPropertySelectionModel接口,并且重写该接口的5个方法.
    public int getOptionCount() //提供下拉菜单的长度
    public Object getOption(int index) //提供select标签的option
    public String getLabel(int index) //提供select标签的Label值,也就是下拉菜单显示的内容
    public String getValue(int index) //提供select标签的value值
    public Object translateValue(String value) //selected后的返回值,value值未必就是我们需要的返回值,可以在这个方法里面对返回的value做对应的转换或修改.
e.g.1. 性别下拉框
    <select jwcid="gender @ProPertySelection" name="genderSelect" value=" ognl:gender" model=" supportedGender">
      <option selected>先生</option>
      <option>女士</option>
    </select>
Java代码
  1. GenderSelectionModel.java   
  2. public class GenderSelectionModel implements IPropertySelectionModel {   
  3.   
  4.     public static final String male = "先生";   
  5.   
  6.     public static final String female = "女士";   
  7.   
  8.     public static final String[] genderOptions = { male, female };   
  9.   
  10.     public int getOptionCount() {   
  11.         return genderOptions.length;   
  12.     }   
  13.   
  14.     public Object getOption(int index) {   
  15.         return this.translateValue(genderOptions[index]);   
  16.     }   
  17.   
  18.     public String getLabel(int index) {   
  19.         return genderOptions[index].toString();   
  20.     }   
  21.   
  22.     public String getValue(int index) {   
  23.         return genderOptions[index];   
  24.     }   
  25.   
  26.     public Object translateValue(String value) {   
  27.         if (value.equals("先生")) {   
  28.             return "1";   
  29.         } else {   
  30.             return "0";   
  31.         }   
  32.     }   
  33. }  
GenderSelectionModel.java
public class GenderSelectionModel implements IPropertySelectionModel {

	public static final String male = "先生";

	public static final String female = "女士";

	public static final String[] genderOptions = { male, female };

	public int getOptionCount() {
		return genderOptions.length;
	}

	public Object getOption(int index) {
		return this.translateValue(genderOptions[index]);
	}

	public String getLabel(int index) {
		return genderOptions[index].toString();
	}

	public String getValue(int index) {
		return genderOptions[index];
	}

	public Object translateValue(String value) {
		if (value.equals("先生")) {
			return "1";
		} else {
			return "0";
		}
	}
}

Java代码
  1. ModUserInfo.java   
  2. public IPropertySelectionModel getSupportedGender() {   
  3.     return new GenderSelectionModel();   
  4. }  
ModUserInfo.java
public IPropertySelectionModel getSupportedGender() {
	return new GenderSelectionModel();
}


    存入数据库中"1"代表先生,"0"代表女士,通过translateValue(String value)方法转换
    页面表现时,通过model属性给出的IPropertySelectionModel获取下拉选项,即getSupportedGender().
    然后通过getGender()方法获取初值,比如获取"0",则在页面显示时寻找value值为"0"的选项即为"女士",并选择之作为初始选择项.


e.g.2. 日志类型下拉框
    <select jwcid="logType @PropertySelection" name="typeSelect" value=" ognl:logType" model=" supportedType">
      <option>心情日记</option>
      <option>情感天地</option>
      <option>生活感触</option>
    </select>
Java代码
  1. TypeSelectionModel.java   
  2. public class TypeSelectionModel implements IPropertySelectionModel {   
  3.        
  4.     private List typeList = new ArrayList();   
  5.   
  6.     public TypeSelectionModel(List typeList) {   
  7.         this.typeList = typeList;   
  8.     }   
  9.   
  10.     public int getOptionCount() {   
  11.         return typeList.size();   
  12.     }   
  13.   
  14.     public Object getOption(int index) {   
  15.         return ((LogType)typeList.get(index)).getValue();   
  16.     }   
  17.   
  18.     public String getLabel(int index) {   
  19.         return ((LogType) typeList.get(index)).getName();   
  20.     }   
  21.   
  22.     public String getValue(int index) {   
  23.         return ((LogType) typeList.get(index)).getValue();   
  24.     }   
  25.   
  26.     public Object translateValue(String value) {   
  27.         return value;   
  28.     }   
  29. }  
TypeSelectionModel.java
public class TypeSelectionModel implements IPropertySelectionModel {
	
	private List typeList = new ArrayList();

	public TypeSelectionModel(List typeList) {
		this.typeList = typeList;
	}

	public int getOptionCount() {
		return typeList.size();
	}

	public Object getOption(int index) {
		return ((LogType)typeList.get(index)).getValue();
	}

	public String getLabel(int index) {
		return ((LogType) typeList.get(index)).getName();
	}

	public String getValue(int index) {
		return ((LogType) typeList.get(index)).getValue();
	}

	public Object translateValue(String value) {
		return value;
	}
}

Java代码
  1. ModLog.java   
  2. public IPropertySelectionModel getSupportedType() {   
  3.     TypeSelectionModel typeSelectionModel =    
  4.                            new TypeSelectionModel(loadType(getUser().getUserId()));   
  5.     return typeSelectionModel;   
  6. }   
  7.   
  8. private List loadType(int userid) {   
  9.     ...//从数据库载入该用户的日志类型列表   
  10. }  
ModLog.java
public IPropertySelectionModel getSupportedType() {
	TypeSelectionModel typeSelectionModel = 
                           new TypeSelectionModel(loadType(getUser().getUserId()));
	return typeSelectionModel;
}

private List loadType(int userid) {
	...//从数据库载入该用户的日志类型列表
}

    页面表现时,通过model属性给出的IPropertySelectionModel获取下拉选项,即getSupportedType().
    然后通过value属性给出的初始值即,getLogType()方法获取初值,比如获取"2",则在页面显示时寻找value值为"2"的选项即为"生活感触",并选择之作为初始选择项.


F) Form组件
e.g.
    <form jwcid="logForm @Form">
      ...
    </form>
    Form的监听(listener)方法可以有两种方式:
      1. 在Form组件中声明.
        <form jwcid="logForm @Form" listener=" ognl:listener:onLogin">
          ...
        </form>
      2. 在submit类型组件中声明.
        <input type="submit" jwcid="onLogin @Submit" listener=" listener:onLogin" value="发表"/>或者
        <span jwcid=" @ImageSubmit" image="..." listener=" listener:onLogin"><img src="..." width="" height=""/></span>
      前一种方式当Form中只要有submit就会触发监听方法,后一种方式是Form中有多个submit,各自实现不同的监听方法.

G) Foreach 组件
e.g.
    <span jwcid=" @Foreach" source=" ognl:logList" value=" ognl:item">
    循环组件,遍历source参数,在表现其内容前更新value参数,将Foreach组件所包含的内容重复表现,其中可以通过value参数获取所需显示内容.
    本例中,页面表现时通过getLogList()方法获取日志列表,循环取出其中数据更新item(日志对象)并予以显示.其中item需要在页面规范(.page)文件中声明:
    <property name="item"/>
    *class参数用来寻找类似CSS的文件对Foreach进行修饰.
    Foreach组件: class="ognl:beans.evenOdd.next"
    Page文件: <bean name="evenOdd" class="org.apache. tapestry.bean.EvenOdd"/>
    CSS文件: tr.odd{background-color: #ffffff;}tr.even{background-color: #eeeeee;}

H) Conditional 组件
e.g.
    <span jwcid=" @Conditional" condition=' ognl:item.sex.equals("1")'>先生</span>
    <span jwcid=" @Conditional" condition=' ognl:item.sex.equals("0")'>女士</span>
    conditional参数为true时运行Conditional组件中的HTML模板内容.
    在 Tapestry4.0以后就不支持该组件了, 可以使用其他组件来实现:
    1. Contrib:Choose和Contrib:When
    <library id="contrib" specification-path="classpath:/org/apache/ tapestry/contrib/Contrib.library"/>(.application文件中引入Contrib类包)
    <span jwcid=" @contrib:Choose">
      <span jwcid=" @contrib:When" condition='ognl:user.gender.equals("1")'>先生</span>
      <span jwcid=" @contrib:When" condition='ognl:user.gender.equals("0")'>女士</span>
    </span>
    2. If组件
    <span jwcid=" @If" condition=' ognl:item.sex.equals("1")'>先生</span>
    <span jwcid=" @If" condition=' ognl:item.sex.equals("0")'>女士</span>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值