1.Build Tool
对于很多开发者来说,build tool不是特别重要,但是这是值得考虑的因素之一。Wicket 1.5和Tapestry 5都使用maven作为build tool,这个它们没有区别。
2.Configuration 配置
Wicket 1.5和Tapestry 5都是采用xml,必须要配置的文件是web.xml。其他的,还需要配置xml设置页面调用的action等。这两个框架都认为开发框架应该负责生成URL和页面渲染的顺序,而不是让开发者在xml配置告诉框架如何做。
Wicket的web.xml
<web-app>
<display-name>wicket</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>wicket</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>agilist.lab.WicketApplication</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>wicket</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
如上显示,大多数配置在Java class:WicketApplication中,WicketApplication是一个用来定义你的web应用的java class。
下面是WicketApplication的部分代码:
public class WicketApplication extends WebApplication{
public WicketApplication(){}
public void init(){
super.init();
}
public Class<HomePage> getHomePage() {
return HomePage.class;
}
}
WicketApplication扩展了WebApplication,后者用来通过HTTP协议调用页面。在getHomePage()中返回index.html
Tapestry 5
下面看看Tapestry 5的配置,和Wicket类似,唯一必须要配置的文件是web.xml。
<web-app>
<display-name>tapestry5</display-name>
<context-param>
<param-name>tapestry.app-package</param-name>
<param-value>agilist.lab</param-value>
</context-param>
<filter>
<filter-name>app</filter-name>
<filter-class>org.apache.tapestry5.TapestryFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>app</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
在web.xml中,你告诉tapestry到哪里去找到你的页面,组件和mixins。在配置中,你的页面,组件和mixins应该在以下目录中:
* Components: agilist.lab.components
* Pages: agilist.lab.pages
* Mixins: agilist.lab.mixins
tapestry 5也拥有配置java class的能力,也类似Wicket。
public class AppModule
{
public static void bind(ServiceBinder binder)
{
binder.bind(Member.class);
}
public static void contributeApplicationDefaults(
MappedConfiguration<String, String> configuration)
{
configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en");
configuration.add(SymbolConstants.PRODUCTION_MODE, "false");
}
}
这两个框架在配置方面做的都很棒。
3.Controller/Page Class
Wicket 和 Tapestry,都是被称为基于 controller in action的框架,两者都是同一的方式来匹配page和class。
Tapestry中,比如我有一个AddMemberPage.java,我的模板名字就是AddMemberPage.html,在Wicket中是用AddMemberPage.tml作为模板。
Wicket:
wicket page class:
public class AddMemberPage extends BasePage {
private static final Logger logger = LoggerFactory.getLogger(AddMemberPage.class);
private Member member;
public AddMemberPage() {
add(new AddMemberForm("addMemberForm", new CompoundPropertyModel( new Member() )));
}
public class AddMemberForm extends Form {
public AddMemberForm(String id, final CompoundPropertyModel model) {
super(id, model);
member = (Member)model.getObject();
add(new TextField("name"));
add(new Button("save"){
public void onSubmit(){
logger.info("Member name: {}", member.getName());
}
});
}
}
}
这是作为父class的BasePage class:
public class BasePage extends WebPage{
public BasePage() {
add(new PageLink("homeLink", HomePage.class)
.add(new Label("homeLabel", new ResourceModel("home"))));
add(new BookmarkablePageLink("addMemberLink", AddMemberPage.class)
.add( new Label("addMemberLabel", new ResourceModel( "member.add" ) ) )
);
}
}
Tapestry 5:
Tapestry 5 Page class:
public class Add {
@Inject private Logger logger;
@Inject @Property @Parameter private Member member;
void onSelectedFromSave(){
logger.info("Member name: {}", member.getName());
}
}
和wicket相同。你必须定义layout class给模板使用。和wicket不同点在于 tapestry 5 模板中使用组件而不是继承。下面是layout class:
public class Layout {
}
因为layout将作为一个组件,所以必须处于组件包下面。
wicket page class比tapestry 5长很多,不过很有趣,你会发觉wicket中的page class类似swing controller,而tapestry 5和JSF方式更象。
4.模板
Wicket 1.5和Tapestry 5的模板都是使用普通HTML,你不需要调用任何特殊的taglib。
wicket:
wicket通过继承方式使用模板,有一个parent父page,作为主要和涉及所有layout的子片断的内容。
<html>
<head>
</head>
<body>
<div id="wrap">
<div id="header">
<a href="#" wicket:id="homeLink"><span wicket:id="homeLabel" /></a>
<a href="#" wicket:id="addMemberLink"><span wicket:id="addMemberLabel" /></a></div>
<div id="content">
<wicket:child /></div>
<div id="footer">
Copyright</div>
</div>
</body>
</html>
这和之前的BasePage html layout一致。使用wicket:child标签,就可以让其他子页面继承模板。
<html>
<head></head>
<body>
<wicket:extend>
<form wicket:id="addMemberForm">
<table>
<tr>
<td>Name</td>
<td><input type="text" wicket:id="name"/></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" wicket:id="save" /></td>
</tr>
</table>
</form>
</wicket:extend>
</body>
</html>
Tapestry
Tapestry5 模板使用的是组件方式:
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<head>
</head>
<body>
<div id="wrap">
<div id="header">
<a t:type="PageLink" page="home">${message:home}</a>
<a t:type="PageLink" page="member/Add">${message:member.add}</a></div>
<div id="content">
<t:body /></div>
<div id="footer">
Copyright</div>
</div>
</body>
</html>
t:body用来定义什么地方来放置一个组件模板。
使用模板:
<t:layout xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<table>
<t:form>
<tr>
<td>Name</td>
<td><input t:type="TextField" t:id="name" t:size="30" t:value="prop:member.name"/></td>
</tr>
<tr>
<td></td>
<td><input t:type="Submit" t:id="save" value="save" /></td>
</tr>
</t:form></table>
</t:layout>
5.Spring integration 和Spring 集成
Wicket and Tapestry中集成spring都很简单,无缝集成。
wicket:
在init()加一行代码:
Java代码
addComponentInstantiationListener(new SpringComponentInjector(this));
类似这样:
public class WicketApplication extends WebApplication{
public WicketApplication(){}
public void init(){
super.init();
addComponentInstantiationListener(new SpringComponentInjector(this));
}
public Class<HomePage> getHomePage() {
return HomePage.class;
}
}
接下来就可以使用spring bean从page class中使用annotating @SpringBean调用:
public class AddMemberPage extends BasePage {
private static final Logger logger = LoggerFactory.getLogger(AddMemberPage.class);
private Member member;
private @SpringBean MemberService service;
public AddMemberPage() {
add(new AddMemberForm("addMemberForm", new CompoundPropertyModel( new Member() )));
}
public class AddMemberForm extends Form {
public AddMemberForm(String id, final CompoundPropertyModel model) {
super(id, model);
member = (Member)model.getObject();
add(new TextField("name"));
add(new Button("save"){
public void onSubmit(){
logger.info("Member name: {}", member.getName());
service.add(member);
}
});
}
}
}
Tapestry 5
在Tapestry 5中,spring bean被看作tapestry 5的组件,无缝调用和通过tapestry 5 IoC注入。为了集成spring,你需要修改web.xml中一行:
<filter>
<filter-name>app</filter-name>
<filter-class>org.apache.tapestry5.spring.TapestrySpringFilter</filter-class>
</filter>
接下来就能注入spring bean进入page class通过简单的@Inject annotation:
public class Add {
@Inject private Logger logger;
@Inject private MemberService service;
@Inject @Property @Parameter private Member member;
void onSelectedFromSave(){
logger.info("Member name: {}", member.getName());
}
}
6.Page unit testing
Wicket and Tapestry5的Page unit testing都不需要启动一个servlet容器。
wicket:
不需要锁定一个特殊测试框架,你可以使用JUnit或者TestNG都没问题,因为wicket提供helper class:
WicketTester来做page class的Unit testing:
public class TestHomePage extends TestCase
{
private WicketTester tester;
@Override
public void setUp()
{
tester = new WicketTester(new WicketApplication());
}
public void testRenderMyPage()
{
//start and render the test page
tester.startPage(HomePage.class);
//assert rendered page class
tester.assertRenderedPage(HomePage.class);
}
}
Tapestry 5
也不需要锁定一个特殊测试框架:
public class MyTest extends Assert
{
@Test
public void test1()
{
String appPackage = "org.example.app";
String appName = "LocaleApp";
PageTester tester = new PageTester(appPackage, appName, "src/main/webapp");
Document doc = tester.renderPage("MyPage");
assertEquals(doc.getElementById("id1").getChildText(), "hello");
}
}