| EasyJWeb vs Struts2 |
|
| 作者是 简易java框架 | |
| 2007-12-26 05:27:13 | |
|
上周EasyJWeb-1.0m3版本发布,收到几封来信,其中有两封提到EasyJWeb相对于struts2这个web MVC框架有哪些优势。这两天在编《EasyJWeb实用开发指南》,因此,整理了一下其中的一些内容,对其中的一些关键作了一个简单的比较,希望对于想了解EasyJWeb及Struts2这两个框架的朋友有所帮助。 宏观的东西不多说,大的用法大家可以从两个框架与JPA+Spring结合实现的添删改查实例进行简单的对比分析。你可以直接通过下面的链接了解更多的内容: Easyjweb+Spring2+JPA 实现一个基本CRUD应用示例 英文版 Struts 2 + Spring 2 + JPA + AJAX Migrating Struts Apps to Struts 2
<context-param> <param-name>defaultActionPackages</param-name> <param-value>myapp.demo,easyjweb.demo</param-value> </context-param> <servlet> <servlet-name>easyjf</servlet-name> <servlet-class>com.easyjf.web.ActionServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>easyjf</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
Struts2的web.xml <init-param> <param-name>actionPackages</param-name> <param-value>com.foo.bar,com.baz.quux</param-value> </init-param> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2、惯例代替配置 <bean name="CommonTemplatePage" class="com.easyjf.web.core.support.CommonTemplatePageVender"> <property name="suffix" value="ct"/> </bean>![]() Struts2配置文件中配置Bean <bean type="com.opensymphony.xwork2.ObjectFactory" name="myfactory" class="com.company.myapp.MyObjectFactory" /> public class HelloAction implements IWebAction{ public Page execute(WebForm form, Module module) throws Exception { form.addResult("msg", "您好,这是EasyJWeb的第一个程序!"); form.addResult("date", new Date()); return new Page("/hello/index.html"); } }Struts2中的Action public class HelloAction { public Page execute() throws Exception { return "success"; } }![]() 当然,这个祼体Action基本上没任何作为,真正的Struts2版本的HelloAction可以写成如下: public class HelloAction implements ServletRequestAware { private HttpServletRequest request; public void setServletRequest(HttpServletRequest request) { this.request = request; } public Page execute() throws Exception { request.setAttribute("msg", "您好,这是EasyJWeb的第一个程序!"); request.setAttribute ("date", new Date()); return Action.SUCCESS; } }![]() 另外,还需要在struts2的配置文件中增加大致如下的配置来指明这个success字符对应的文件路径: <action name="HelloWorld" class="tutorial.HelloAction"> <result>/HelloWorld.jsp</result> </action> 但实际应用中,EasyJWeb的Action大多数都直接继承AbstractPageCmdAction,因为这个基础控制器提供了很多非常强大的功能,比如按惯例查找页面、灵活页面跳转及视图切换,动态特性的命令方法。同样为了使用Struts2中的很多功能,比如拦截器、依赖注入等都需要Action实现指定的接口,更多的时候直接继承ActionSuport类。
<!-- 申明Spring为easyjweb 应用容器开始 --> <bean name="springContainer" class="org.springframework.web.context.support.XmlWebApplicationContext"> <property name="configLocations"> <list> <value>WEB-INF/classes/application.xml</value> </list> </property> </bean> <bean name="innerSpringContainer" class="com.easyjf.container.impl.SpringContainer"> <property name="factory" ref="springContainer" /> </bean> <!-- 申明Spring为easyjweb 应用容器结束 --> </beans>![]()
Struts2可以使用代理的方式,先在web.xml文件中通过下面的配置启动spring容器: 另外一点区别是,EasyJWeb的Action都是由EasyJWeb的IoC容器管理,不需要用其它容器来管理,因为EasyJWeb的IoC容器会把其它容器中的业务组件注入到Action的相应属性中,你可以把业务组件同时放在EJB容器、Spring容器、或者Guice容器中。 五、数据的处理 public class PersonAction extends AbstractPageCmdAction { private PersonService service; public void setService(PersonService service) { this.service=service; } public void save(WebForm form) { Person person=form.toPo(Person.class); this.service.save(person); page("list"); } }![]() 对应的表单是普通的html表示,如下所示: <form action="save" validate="true"> <input type="text" id="id" name="id" cssStyle="display:none"/> <input type="text" id="firstName" label="First Name" name="firstName"/> <input type="text" id="lastName" label="Last Name" name="lastName"/> </form>Struts2中则不需要写代码,只需要把要注入的数据在Action中准备好即可,如: public class PersonAction { private PersonService service; private Person person;![]() public PersonAction(PersonService service) { this.service = service; }![]() public String execute() { //this.persons = service.findAll(); return Action.SUCCESS; } public String save() { this.service.save(person); this.person = new Person(); return execute(); } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }![]() ![]()
对应的表单如下所示: <s:form action="save" validate="true"> <s:textfield id="id" name="person.id" cssStyle="display:none"/> <s:textfield id="firstName" label="First Name" name="person.firstName"/> <s:textfield id="lastName" label="Last Name" name="person.lastName"/> </s:form>当然,Struts2关于数据转换方面提供了很多可供选择,比如也可以实现ModelDriven接口提供getModel来按指定的规则注入模型数据。 对于关联对像的加载及注入,比如Person中的Department属性,EasyJWeb只需要在属性上加上一个@POLoad标签即可,而struts2则不能。 Struts2的验证配置, <field name="stringLengthValidatorField"> <field-validator type="stringlength"> <param name="maxLength">4</param> <param name="minLength">2</param> <param name="trim">true</param> <message><![CDATA[ must be a String of a specific greater than 1 less than 5 if specified ]]></message> </field-validator> </field>
另外,EasyJWeb及Struts2都还具有其它自己很多独特的优点。总的来说,对于框架的选择应该是多方面考虑的,没有绝对的谁好或者谁坏,更应该是顺手适用才行,如果你愿意,继续使用Struts1.x两三年也没问题。由于本人对Struts2应用得不够多,所以文中一些不足或偏颇之处,还请各位朋友不吝赐教。 |
|
| 最近更新 ( 2007-12-26 05:27:13 ) |





form.addResult(
}
}