作者:江南白衣
本文来自SpringSide WIki,请留意Wiki上的最新版本。
Struts 1.2 其实已经大不同,只要大家有简约的态度,即使我们日常的Struts,同样可以写得非常精简,不一定要如传统观念认为的那么繁重,从而节省每天编码的时间。
Struts与Spring结合的基本知识请阅读Struts使用要点。
1.DispatchAction
以一个Controller响应一组动作绝对是Controller界的真理,Struts的DispatchAction同样可以做到这点。
<actionpath="/admin/user"name="userForm"scope="request"parameter="method"validate="false">
<forwardname="list"path="/admin/userList.jsp"/>
</action>
其中parameter="method" 设置了用来指定响应方法名的url参数名为method,即/admin/user.do?method=list 将调用UserAction的public ActionForward list(....)函数。
public ActionForward unspecified(....) 函数可以指定不带method方法时的默认方法。
2. struts-config.xml使用通配符
对一些CRUD的Action,可以使用通配符,如果这批action里某个action某个方法还存在特殊路径,可以在代码里直接new ActionForward("/foo.jsp");
<actionpath="/admin/*"name="{1}Form"parameter="method"scope="request"validate="false">
<forwardname="list"path="/WEB-INF/pages/admin/{1}List.jsp"/>
<forwardname="edit"path="/WEB-INF/pages/admin/{1}Form.jsp"/>
<forwardname="success"path="/admin/{1}.do?method=list"redirect="true"/>
</action>
3.No FormBean
Struts 1.2.7 之后一共有三种方法,不需要定义FormBean Java类也不需要在struts-config.xml里详细定义formBean的每个属性。
第一种是appfuse里使用的,定义DynaValidatorForm里,内嵌一个pojo属性.
<form-beanname="userForm"type="org.apache.struts.validator.DynaValidatorForm">
<form-propertyname="user"type="org.appfuse.model.User"/>
</form-bean>
//取得formDynaActionFormuserForm=(DynaActionForm)form;
Useruser=(User)userForm.get("user");
//设置form
userForm.set("user",user);
第二种是用BeanValidatorForm,直接把Pojo作FormBean,无需继承于FormBean接口。
<form-beanname="userForm"type="org.springside.helloworld.domain.User"/>
//取得formBean
ValidatorFormuserForm=(BeanValidatorForm)form;Useruser=(User)userForm.getInstance();
//设置form
BeanUtils.copyProperties(userForm,user);
第3种是用LazyValidatorForm ,特别适合于FormBean中并不包含POJO商业对象所有属性的情况,因为通常项目里都属于这种情况,所以springside默认使用lazyValidatorForm.
比如User对象 有 id,name,status三个属性,而form表单中只有id和name两个input框,如果使用方法1,2,直接把user 作为 form bean, user对象的status因为没设值,将为null, copy 到作为商业对象的user时,就会以null覆盖原值。而lazyBean就没有这个问题,如果form中没有status属性,它不会将它copy给商业对象。
另外,apache commons-beantuils 1.7.0的lazybean仍有小bug,需要使用commons-1.7.1 snapshot版,而且它直接提供下载的snapshot版缺少了几个class,应使用springside提供的版本。
<form-beanname="bookForm"type="org.apache.struts.validator.LazyValidatorForm"/>
BeanUtils.copyProperties(user,form);
注意User对象被自动绑定,默认Integer id 在空值时会被赋值为0,需要增加converter,让其默认为null,虽然也可以在web.xml里进行相关配置,但还是在基类里配置了比较安全。
static{
ConvertUtils.register(newIntegerConverter(null),Integer.class);
}
4. 一些不必受困于Struts机制的简化写法
一些简便直接的方法,大家可不必太受困于Struts的机制:
4.1 不在struts-config.xml配置jsp路径,直接在代码里跳转
returnnewActionForward("/foo/bar.jsp");
or
returnnewActionForward("/foo/bar.jsp",true);
4.2 不走jsp,直接输出字符串
ActionForwardexecute(....){
try{
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(text);
}catch(IOExceptione){
log.error(e);
}
returnnull;
}
4.3. 不依赖Struts Taglib,EL直接输出FormBean 属性
普通FormBean:${bookForm.image}
LazyValidatorForm:${bookForm.map.image}
其中bookForm 是formBean名。另一种输出属性的方式是使用jodd-form, 设<jodd:form bean="bookForm">
4.4 不用i18N地使用Message与Error
Struts的ActionMessages很常用,但只接收i18N的key作为输入参数,老定义i18n很麻烦,偏方之一就是设一个message= {0},然后直接new ActionMessage("message", message);
5. 终极化简--StrutsEntityAction
SpringSide封装的EnttiyCRUDAction,,每个包含了CRUD操作的Action仅需如下代码,详细请阅读SpringSide的Struts封装
publicclassUserActionextendsStrutsEntityAction<User,UserManager>{
privateUserManageruserManager;
publicvoidsetUserManager(UserManageruserManager){
this.userManager=userManager;
}
}
分享到:
相关推荐
Pragmatic Programmers - Pragmatic Project Automation(2004).pdf
Straight from the programming trenches, The Pragmatic Programmer cuts through the increasing specialization and technicalities of modern software development to examine the core process--taking a ...
The Pragmatic Programmer 是我转的 txt的
"Pragmatic Version Control Using Subversion" 一书作者的最新力作,出版还不超过半年。与其他又长又臭的砖头书相比,这本只有一百多页的“实用书”非常有诱惑力。 强烈推荐!
pragmatic.software.testing a new book for program testing
/root/download/GWT Pragmatic book.pdf
该资源主要是关于PRAGMATIC方面的技术,无论是入门级别的还是大师级别的各个过程的图书都有希望能帮助到大家!
The Pragmatic Programmer,程序员最应该阅读的。可以借鉴
Pragmatic Guide to Sass 3
pragmatic-java-engineer,Java Java.zip
Pragmatic Ajax - A Web 2.0 Primer
Pragmatic_Version_Control_-_Using_Git
Programming Ruby 1.9 The Pragmatic Programmers' Guide -- Ruby 编程的圣经教程。此版本涵盖最新的Ruby 1.9
Pragmatic Unit Testing In C# With Nunit pdf版
javascript编程指南 Pragmatic_Guide_to_JavaScript
Pragmatic Software Co., Inc. 介绍 公司主要产品是Software Planner(需求管理、项目发布管理、测试用例管理、缺陷管理等)。2009年9月被AutomatedQA收购,为AutomatedQA进入ALM领域打下铺垫。