Struts2小结

标签 : ,

天乙社区8.0(http://www.laoer.com)已经完全用Struts2实现,在使用Struts的过程中也遇到了一些问题,现在总结一下。

1、在web.xml中EncodingFilter的位置应该在Struts2的FilterDispatcher之前,道理很简单,要先调整字符集,再进入Action。

2、如果使用Urlrewrite,要指定filter-mapping的dispatcher方式,如下
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

3、在做上传文件的时候,要在web.xml中增加ActionContextCleanUp这个filter,如果不增加,会发生第一次上传取不到文件的情况
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
按照Struts2的API,filter的顺序是
struts-cleanup filter
SiteMesh filter
FilterDispatcher

4、在Apache+Resin的情况下,要在WEB-INF下增加resin-web.xml,该文件只针对Resin有效,作用是指定后缀与 Resin的Servlet引擎匹配,要不然从Apache转发过去的请求到Resin后会出现404的情况,resin-web.xml举例如下:
<web-app xmlns=”http://caucho.com/ns/resin”>
<servlet-mapping url-pattern=’*.bbscs’ servlet-name=’plugin_match’/>
</web-app>

5、在使用<s:url/>标签的时候,会出现将get或post数值带入url参数的情况,如果不需要这些参数,可以在struts.properties文件中设置
struts.url.includeParams=none
或是在<s:url/>标记中将includeParams属性设为none
另外还有两个值
all,是把get和post中的参数加入到url参数中
get,是只把get中的参数加入到url参数中

6、与webwork基本相同,Struts2提供了几种ui.theme,有xhtml、css_xhtml、simple等等,在 struts.properties中可以设置使用何种theme,这一点很关键,不同的theme,struts的tag会生成不同的html代码,而且在某些情况下这些theme不能满足页面要求,则需要自己进行扩展了,这些theme都是由freemarker写的,仿照这写就可以。

7、单个checkbox的标记库好像只能返回boolean的值,如果在数据库中设计为int型,则需要做一些转换,这一点我觉得不如Struts1.x的方便。

8、总体来说Struts2的标记库使用上比Struts1.x的方便,页面整体也比较简洁,Struts2采用stack的方式存取数据,与Struts1相比各有千秋吧。

Struts2主要延续自webwork,以前使用webwork的朋友转过来并不困难,Struts2的几个核心的部分,比如拦截器、Result Configuration、OGNL stack等等还是需要仔细的体会,深入了解,才能做出优秀的系统。

(这篇文章在JavaEye上发表过)

发表评论

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word