`

struts2使用总结

阅读更多

---------------------------源码及文档位置-------------------------------------------------------------------------------------------

源码位置:strtus-2.1.6/src/core/src/main/java

文档位置:strtus-2.1.6/strtus2-core/apidocs

jar包 加源码 加文档

---------------------------源码及文档位置--------------------------------------------------------------------------------------------

 

 

---------------------------web.xml中配置strust访问形式---------------------------------------------------------------------------

web.xml 中 /* , *.action 映射方式都可以,但是建议/*
访问时可以省略.action  例如: http://localhost:8080/myproject/hello  (hello.action)

---------------------------web.xml中配置strust访问形式---------------------------------------------------------------------------

 

 

----------------------------------- struts.xml中配置开发模式---------------------------------------------------
<constant name="struts.devMode" value="true"/> 开发模式,不用重新发布

 

----------------------------------- struts.xml中配置开发模式---------------------------------------------------

 

 


myEclipse--window 搜索 catalog --XML Catalog 增加 struts.dtd

 

 

--------------------Action三种实现形式----------------------------------------------------

1、直接XxxAction 内有excute()方法

2、实现Action接口

3、继承ActionSupport
推荐 继承 ActionSupport  不用实现Action接口的写法

--------------------Action三种实现形式----------------------------------------------------

 

---------------------jsp中设置basePath绝对路径-------------------------------------------------------------------
jsp中<base href="<%=basePath%>" />

<!-- 设置绝对路径,其余在页面里直接用相对路径即可,例如 personmanage/person!add.action-->
---------------------jsp中设置basePath绝对路径-------------------------------------------------------------------

 


---------------------------------strtus.xml-----------------------------------------------------------------------------------------------
通配符  约定优于配置
<struts>
    <constant name="struts.devMode" value="true"/>
    <package name="model_1"  namespace="/model_1" extends="mydefault"><!-- 包的继承-->
        <action name="*_*" class="com.cpcns.model1.action.{1}Action" method="{2}">
        <result>model_1/{1}_{2}.jsp</result>
    </action>
    </package>

    <!--输入一个不存在的Action不会报错误页面,而是跳转到设定好的default页面,比如说登陆页面或者首页 -->
    <package name="mydefault" extends="struts-default" namespace="/">
        <default-actin-ref name="index" />
        <action name="index">
        <result>index.jsp</result>
        </action>
    </package>
</struts>
<s:debug></s:debug><!--在页面中查看堆栈信息-->
---------------------------------strtus.xml-----------------------------------------------------------------------------------------------


-------------------Action 三种接收参数形式-----------------------------------------------------------------------------------
1、Action定义变量,2、DomainModel,3、继承ModelDriver 实现getModel方法
Action 中 用VO继承DomainModel 的形式,User.name。。。避免Action中代码过乱

XXXAction implements ModelDriver<User>{
private User user = new User();
@Override
public User getModel(){
   return user;
}
}
-------------------Action 三种接收参数形式-----------------------------------------------------------------------------------

-------------------------------------------------表单校验-----------------------------------------------------------------------

extends ActionSupport

用父类的
addFieldError("passwordError","密码长度不足8位");
addFieldError("passwordError","密码中至少包含1个字母1个数字1个特殊字符");
页面中两种方式:
<s:fielderror fieldName="error1" theme="simple">
passwordError1:<s:property value="errors.passwordError[0]"/>
passwordError2:<s:property value="errors.passwordError[1]"/>

-------------------------------------------------表单校验-----------------------------------------------------------------------

---------------------------------------------resultType------------------------------------------------------------------------
<package name="model_x" namespace="/space_x" extends="struts-default">
        <global-results><!--全局result-->
        <result name="reLogin">/reLogin.jsp</rssult>
    </global-results>
    <action name="action1">
         <result type="dispatcher">a1.jsp</result>
     </action>
     <action name="action3">
         <result type="chain">
         <param name="actionName">action1</param>
         <param name="namespace">/space_x</param>
         </result>
     </action>
</package>

dispatcher 服务器端跳转
redirect 客户端跳转
chain 跳转到另一个Action
redirectAction 客户端跳转另一个Action

---------------------------------------------resultType------------------------------------------------------------------------

 

-------------------------动态结果集---------------------------------------------------------------------------------------------
<package name="model_x" namespace="/space_x" extends="struts-default">
       
    <action name="xxxaction" class="x.x.XXXAction">
         <result>${resultStr}</result>
         <!-- 一次request(多次forward)共享同一valueStack无需传值,只有重定向才需要重新传值-->
         <result name="addResult" type="redirect">a.jsp?t={type}</result><!-- 在a.jsp中 <s:property value="#parameters.t">来获取-->
     </action>
     
</package>

puglic XXXAction{

private String resultStr;
private int type;

public void setResultStr(String str){
   this.resultStr = str;
}
public String getResultStr(){
   return resultStr;
}
public void setType(int i){
   this.type = i;
}
public int getType(){
   return type;
}

public String execute() throws Exception{
 
    switch(type){
      //这种方式在程序里写死,不常用
      case 0: resultStr = "a0.jsp"; break;
      case 1: resultStr = "a1.jsp"; break;
      case 2: resultStr = "a2.jsp"; break;
         .
         .
      default:resultStr = "default.jsp";
    }
  

    return resultStr;
}

public String add(){

   return "addResult";

}

}

-------------------------动态结果集---------------------------------------------------------------------------------------------


-------------------------OGNL----------------------------------------------------------------------------------------------------------------------------
<constant name="struts.ognl.allowStaticMethedAccess" value="true"><!--struts.xml-->
<!--允许用OGNL方式访问静态方法 @com.xxx.a@staticMethod(),访问Method类:@@max(2,3) -->

<s:property  value="users.{age}"/> 取所有user的age组成一个数组
<s:property  value="users.{age}[0]"/>等价于<s:property  value="users[0].age"/>
<s:property  value="dogMap.dog001"/>
<s:property  value="dogMap['dog001']"/>
<s:property  value="dogMap[\"dog001\"]"/>
<s:property  value="dogMap.keys"/> 访问Map所有的key
<s:property  value="dogMap.values"/> 访问Map所有的values
<s:property  value="dogMap.size()"/>|<s:property  value="dogMap.size"/> 访问Map大小
投影
<s:property  value="users.{?#this.age==1}.{age}"/>
<s:property  value="users.{^#this.age>1}.{age}"/><!--age>1的所有user中的第一个-->
<s:property  value="users.{$#this.age>1}.{age}"/><!--age>1的所有user中的最后一个-->
<s:property  value="users.{$#this.age>1}.{age}==null"/><!--判断是否有满足条件的对象 -->
<s:property  value="[0]"/><!--读取OGNL栈里的元素,从0开始的对象(Action,chain时会有多个Action)-->
-------------------------OGNL----------------------------------------------------------------------------------------------------------------------------


--------------------------------------------strtus2标签------------------------------------------------------------------------
<s:set var="titleText" value="'XXX页面'" >  不建议用name或id,建议用var
struts.xml
<constant name="strtus.ui.theme" value="mysimple"/><!--页面中struts标签的样式 -->
新建一个文件夹src/template/mysimple
把strtus-core下的主题 拷贝过来,src/template/mysimple/fielderror.ftl 重写fielderror.ftl 文件(freemarker语言)覆盖struts样式
--------------------------------------------strtus2标签------------------------------------------------------------------------



------------------------------------propertiesEditer插件---------------------------------------------------------------------
propertiesEditer插件,properties 中的中文必须是UTF-8的编码(\u6b22\u8fce),编写不方便,尤其的国际化的时候,
所以下载个propertiesEditer插件到MyEclipse很是必要
jp.gr.java_conf.ussiy.app.propedit_5.3.3.zip
------------------------------------propertiesEditer插件---------------------------------------------------------------------

 


-------------------------------------token防止重复提交-----------------------------------------------------------------------

<constant name="strtus.devMode" value="true">
<package name="manage" namespace="/" extends="struts-default">
  <!-- token 提交返回本页面有问题,整个html返回 -->
  <action name="input" class="com.xxx.manage.action.InputAction">
       <result>/manage/input.jsp</result>
  </action>
  <action name="user" class="com.xxx.manage.action.UserAction">
       <result>/manage/ok.jsp</result>
       <interceptor-ref name="defaultStack"></interceptor-ref>
       <interceptor-ref name="token"></interceptor-ref>
       <result name="invaild.token">/token.jsp</result>
  </action>
</packag>


input.jsp
<s:token></s:token>

-------------------------------------token防止重复提交-----------------------------------------------------------------------

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics