1.Myeclipse修改jsp,xml等文件默认编码格式
以jsp为例:
Widows/Preferences/MyEclipse/Files and Editors/jsp
Encoding:选择Chinese,National Standard
2.自定义action
action可以是一个普通java类,里面有public String execute方法即可,或者实现Action接口,或者是继承ActionSupport
常用的是:继承ActionSupport,因为ActionSpport有封装好的struts2方法。
3.动态方法调用DMI(dynamic method invoke)
Action执行的时候并不一定要执行execute方法
可以在配置文件中配置Action的时候用method=来指定执行哪个方法
也可以在url地址中动态指定(动态方法调用DMI)(推荐)
<a href="<%=context %>/user/userAdd">添加用户</a>
<a href="<%=context %>/user/user!add">添加用户</a>
前者会产生太多的action,所以不推荐使用
4.通配符 *{1}{2}
如:<action name="*_*" class="com.....{1}Action" method="{2}">
<result>/{1}_{2}_success.jsp</result>
</action>
5.解决中文乱码
struts.xml 添加<constant name="struts.i18n.encoding" value="GBK" />
6.包含文件配置
在struts.xml里添加: <include file="struts2配置文件.xml" />
7.Result类型
dispatcher,redirect,chain,redirectAction,freemarker,httpheader,stream,velocity,xslt,plaintext,tiles
常用的是dispatcher(默认),redirect,chain,redirectAction
dispatcher:服务器端跳转,只能跳转到页面(jsp,html等)
redirect:客户端跳转,只能跳转到页面
chain:服务器端跳转,forward,跳转到action
redirectAction:客户端跳转,跳转到action
8.全局结果集
global-results
一般package包继承struts-default,同时也可以继承别的package包,
如:<package name="user" namespace="/user" extends="struts-default">
<package name="XX" namespace="/XX" extends="user">
9.<s:debug></s:debug>取值
取值栈(Value Stack Contents):<s:property value="属性名"/>
取actioncontext(Stack Context): #XX
---These items are available using the #key notation
<s:property value="#parameters.name"/>
#attr #request ...
附:在配置文件里,取值栈的值:${}