struts2学习笔记

  1. 1.<constant name="struts.devMode" value="true"/>  
  2.    value="true"表示在开发时改写代码立即生效  
  3.   
  4. 2.<package name="front" extends="struts-default" namespace="/">  
  5.      <action name="index" class="com.bjsxt.struts2.front.action.IndexAction1">  
  6.         <result name="success">/ActionIntroduction.jsp</result>  
  7.      </action>  
  8.   </package>  
  9.    访问路径为:http://localhost:8080/Struts2_0300_Action/index  
  10.    访问路径中不需要package的name,直接是:...+...+项目名称+命名空间+action的name  
  11.    result的name默认是success,name值用于根据class返回值来选择result  
  12.    这里class指定的类有execute函数,返回一个String,例如:“success”,用于选择result  
  13.   
  14. 3.action中的class有三种写法:  
  15.   (1) public class IndexAction1 {  
  16.        public String execute() {  
  17.             return "success";  
  18.         }  
  19.       }   
  20.   (2) import com.opensymphony.xwork2.Action;  
  21.       public class IndexAction2 implements Action {  
  22.          public String execute() {  
  23.              return "success";  
  24.          }  
  25.       }  
  26.   (3) import com.opensymphony.xworks.ActionSupport;  
  27.       public class IndexAction3 extends ActionSupport {  
  28.          public String execute() {  
  29.              return "success";  
  30.          }  
  31.       }  
  32.    第三种方法最好,ActionSupport帮我们封装了很多方法供使用  
  33.   
  34. 4. <package name="front" extends="struts-default" namespace="/">  
  35.      <action name="index" class="com.bjsxt.struts2.front.action.IndexAction1" method="add">  
  36.         <result name="success">/ActionIntroduction.jsp</result>  
  37.      </action>  
  38.    </package>  
  39.    如果写method代表指定函数add()  
  40. 如果不写method,且需要指定调用IndexAction1中的add()方法,访问路径中写...+...+项目名称+/+index!add,这就是动态方法调用DMI  
  41.   
  42. 5. 访问路径问题,在JSP中添加<base href="<%=basePath%>" />,在其他访问地址中只需写jsp文件名和后缀名即可如:  
  43.       <base href="<%=basePath%>" />  
  44.        <a href="index.jsp">index.jsp</a>  
  45.   
  46. 6. 通配符问题,在action中,name可能存在不确定的值,如student*,或者*_*,那么{1},{2}就表示其未显示的部分,class、method、result等中都可以用{1}和{2}来表示未显示部分。  
  47.  如:<action name="student*" class="......" method="{1}">  
  48.         <result>/Student{1}_success.jsp</result>  
  49.      </action>  
  50.      <action name="*_*" class="com.bjsxt.struts2.action.{1}Action">  
  51.         <result>/{1}_{2}_success.jsp</result>  
  52.      </action>  
  53.   
  54. 7. 用Action的属性接受参数,例如add()方法中传参name和age  
  55.    http://localhost:8080/Struts2_0700_ActionAttrParamInput/user/user!add?name=aa&age=8  
  56.   
  57. 8. 用DomainModel接收参数,访问路径中写  
  58.    http://localhost:8080/Struts2_0700_ActionAttrParamInput/user/user!add?user.name=aa&user.age=8  
  59.    其中user是name为user的action对应的class(userAction)中的一个属性,其也是一个对象,里面有很多属性,如name和age  
  60.   
  61. 9. 一般情况下解决中文问题需要加<constant name="struts.i18n.encoding" value="GBK" />  
  62. 10. 模块包含,struts配置文件struts.xml包含别的struts配置文件login.xml中的内容  
  63.     直接在struts.xml中写<include file="login.xml" />  
  64.   
  65. 11. 简单数据验证,在action的class中对传入的参数进行判断,如下:  
  66.     public String add() {  
  67.         if(name==null||!name.equals("admin")) {  
  68.             this.addFieldError("name","name is error");  
  69.             return ERROR;  
  70.         }  
  71.         return SUCCESS;  
  72.     }  
  73.     在jsp文件中取这个name的值,方法:  
  74.     先引入标签<%@taglib uri="/struts-tags" prefix="s" %>  
  75.     然后取值<s:fielderror fieldName="name"/>  
  76.   
  77. 12. result类型,根据不同的type来用不同的方式返回结果  
  78.     主要用两种:  
  79.     <action name="r1">  
  80.      <result type="dispatcher">/r1.jsp</result>    <!--服务器端跳转-->  
  81.     </action>  
  82.     <action name="r2">  
  83.         <result type="redirect">/r2.jsp</result>      <!--客户端跳转-->  
  84.     </action>  
  85.   
  86. 13. global-results全局结果集  
  87.     <global-results>  
  88.         <result name="mainpage">/main.jsp</result>  
  89.     </global-results>  
  90. 如果一个package中需要用另一个package中的内容,则需要继承,那么在packge行写上<package name=".." namespace=".."  extends="user(被继承的package名称)">  
posted @ 2014-01-09 16:17  刘俊鹏123  阅读(217)  评论(0编辑  收藏  举报
重生之大文豪