在这个文件中,用到了另外一些常用的组件,先看一下EditStudent.jwc中的这些组件的描述:
<specification class="test.EditStudent">
<component id="form" type="Form">
<binding name="listener" property-path="listeners.formSubmit"/>
</component>
<component id="gender" type="RadioGroup">
<binding name="selected" property-path="gender"/>
</component>
<component id="ifError" type="Conditional">
<binding name="condition" property-path="error"/>
</component>
<component id="insertError" type="Insert">
<binding name="value" property-path="error"/>
</component>
<component id="id" type="TextField">
<binding name="value" property-path="id"/>
</component>
<component id="male" type="Radio">
<field-binding name="value" field-name="test.EditStudent.MALE"/>
</component>
</specification>
form是一个Form组件,它的参数listener指定submit这个form时有那个函数处理。ifError是一个Conditional组件,这个组件指定当condition满足时才会显示,在本例中,如果error不为空,则condition满足。在这个组件中,有嵌套了一个Insert类型的组件,用于将错误显示。这是Tapestry中经常用到的处理错误的方式。gender是一个RadioGroup组件,它绑定了javabean中的gender属性,selected参数指定那个radio被选中,在这个组件中,又嵌套了两个Radio组件,分别用来表示男,女。Radio的value参数指定当用户选定这个radio时,RadioGroup绑定的属性值将会等于field-name中指定的值(这个值必须是static的),在本例中,gender=test.EditStudent.MALE。id是一个TextField组件,其参数value绑定到javabean中的id属性。
下面是相应的EditStudent类:
package test;
import com.primix.tapestry.*;
public class EditStudent extends BasePage
{
public static final int MALE = 1;
public static final int FEMALE = 2;
private int gender;
private String error;
private String id;
private String sname;
private String department;
public void detach()
{
error = null;
id=null;
sname=null;
gender=0;
department=null;
super.detach();
}
public int getGender()
{
return gender;
}
public String getId()
{
return id;
}
public String getSname()
{
return sname;
}
public String getDepartment()
{
return department;
}
public void setGender(int value)
{
gender = value;
fireObservedChange("gender", value);
}
public void setId(String value)
{
id = value;
fireObservedChange("id", value);
}
public String getError()
{
return error;
}
public void setSname(String value)
{
sname = value;
fireObservedChange("sname", value);
}
public void setDepartment(String value)
{
department = value;
fireObservedChange("department", value);
}
public void formSubmit(IRequestCycle cycle)
{
//判断用户是否添完了所有数据
if (gender== 0||id==null||id.equals("")||sname==null||sname.equals("")||
department==null||department.equals(""))
{
error = "请填充完所有选项";
return;
}
//将学生保存
try{
Student student=new Student();
student.setId(id);
student.setName(sname);
if(gender==1)
student.setGender("男");
else
student.setGender("女");
student.setDepartment(department);
student.save(null);
}catch(Exception e){
e.printStackTrace();
}
//清空当前的各个属性,以免再次进入此页面时,各属性仍旧保留原来的值
setSname(null);
setDepartment(null);
setId(null);
setGender(0);
//重定向到Home页面
cycle.setPage("Home");
}
}
在本类的一些设置属性的函数中使用了fireObservedChange这个函数,这个函数激发一个改变事件,通知当前的属性的值已经改变。
其他应用
Tapestry中自带的例子中的Workbench中的localization例子演示了怎样使用本地化,你只需要创建不同语言的html模板,还有图形等其它一些html中用到的资源。例如创建一个法语版的EditStudent.html,则相应的html文件名为EditStudent_fr.html,而jwc中定义的组件的描述不用有多个版本。这里要介绍一下Tapestry本地化中经常用到的一个概念:assets。assets是一些web应用中用到的资源,如图象,视频。assets有三种:external, internal 和private。External类型的assets来源于任意的URL。Internal类型的assets来源于和Tapestry应用在同一个服务器上的URL。Private 类型的assets允许部署在WAR的WEB-INF/classes目录下(同上面的html模板,jwc文件一样),这个目录对于web服务器来说是不可见的。
看一下Workbench中localization例子中的localization.jwc文件的片断:
<component id="changeButton" type="ImageSubmit">
<binding name="image" property-path="assets.change-button"/>
</component>
<private-asset name="change-button" resource-path="/tutorial/workbench/localization/Change.gif"/>
在changeButton组件中就使用了private assets,而这些图像文件就放在WAR的WEB-INF/classes下,注意图像跟html一样也有多个语言的版本。
注意jwc文件中的inputLocale这个组件,其实localization应用就是通过这个组件来实现本地化。具体参数请看其Developer guide。
<component id="inputLocale" type="PropertySelection">
<binding name="value" property-path="page.engine.locale"/>
<binding name="model" property-path="localeModel"/>
</component>
Tapestry还支持创建自己的可重用组件,其自身带了一个这样的例子:Border。同时它还有其它一些例子:Inspector展示了怎样监视你的应用程序。vlib是一个用tapestry作表示层的j2ee应用程序(用jboss作为应用服务器)。
Tapestry的功能非常强大,本文只是介绍了其一小部分,还有很多方面没有涉及到,例如javascript在Tapestry中的应用。具体可以看其文档,相信如果你用一下这个框架,你就会被它深深吸引。Tapestry的文档做的不是很全,不过经过不断的摸索,相信你会很快掌握它。








