[12.7]Struts2学习笔记一

Action:
1.
  Action继承ActionSupport类,默认执行execute()方法
  jsp中插入struts标签  <%@taglib uri="/struts-tags" prefix = "s" %>
  在struts.xml中配置 :
   (1)   打开调试模式  <constant name="struts.devMode" value="true" />
   (2)   <package name="yu" namespace="/yu" extends="struts-default">
     访问action路径为namespace下(/yu/ActionName)
     extends="struts-default"   默认过滤器,拦截器不要忘


2.
动态调用Action中的方法  /XxxxAction!方法名?参数1=xxx&参数2=xxx...
action中   this.addFieldError("name", "用户名有误"),
   <s:fielderror fieldName="name" theme="simple"/>
   <s:property value="fieldErrors.name[0]"/>
   添加信息进入value stack,可以在debug中查看(在jsp中插入标签   <s:debug></s:debug>)

3.
通配符 
        <action name="User_*" class="com.yu.UserAction" method="{1}">
  <result name="success">/{1}_success.jsp</result>
  <result name="error">/{1}_error.jsp</result>
        </action>
  
  
4.
3种往action中传参数的方法
  (1)直接在action 里面写属性,生成对应的set,get方法
  (2)DomainModel:写一个封装类,里面写个无参的构造方法,在写属性然后生成set,get方法进行封装,然后在action中 private 之前那个类名 实例名,生成这个对象的set,get方法,
   从jsp往这个action中传参数时要用 实例名.属性=xxx来传,这样action会自动new 那个封装属性的实例(需要封装类的无参构造方法).
  (3)ModelDriven:写一个封装类,里面写属性然后生成set,get,然后action继承ModelDriven<泛型>类,action中 要自己new封装类的实例(注意跟2的区别),然后生成set,get方法
   然后实现ModelDriven的getModel方法返回那个封装类的实例
  public class UserAction extends ActionSupport implements ModelDriven<User>{
           
   public User user = new User();
  
   public String execute(){
   
    ......
    return ...;
   }

   public User getUser() {
    return user;
   }

   public void setUser(User user) {
   this.user = user;
   }

   public User getModel() {
  
   return user;
   }

  }

5.
IOC控制反转获取request,session,application
  public class WebAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware{
 
   public Map<String,Object> request;
   public Map<String,Object> session;
   public Map<String,Object> application;
 
   public String execute(){
    request.put("name","fish");
    session.pur("color","red");
    application.put("age","18");
    return SUCCESS;
   }
 
   public void setRequest(Map<String, Object> request) {  
    this.request = request;
   }

   public void setSession(Map<String, Object> session) {
     this.session = session;
   }

   public void setApplication(Map<String, Object> application) {
     this.application = application;
   }

  }
  
  struts.xml 取出MAP      <s:property value ="#request.name"/>

Result:
1.
  result type中不在一个包中的跳转方法
  <result name="xxx" type="chain">
  <param name="namespace">/xxx</param>
  <param name="actionName">xxx</param>
  <param name="method">xxx</param>
  </result>

2.
result type

        <package name="fish" namespace="/fish" extends="struts-default">
  <action name="r1">
        <result type = "dispatcher">/r1.jsp</result>
        </action>
        </package>

  <package name="yu" namespace="/yu" extends="struts-default">
        <action name="r2">     
        <result type = "redirect">/r2.jsp</result>
        </action>
       
        <action name="r3">      
        <result type = "chain">
        <param name="namespace">/fish</param>
        <param name="actionName">r1</param>
        <param name="method">execute</param>       
        </result>
        </action>
       
  <action name="r4">      
        <result type = "redirectAction">r2</result>
        </action>
  </package>
    
  注意:chain和redirectAction 时应写跳转的action名,dispatcher(服务器跳转)和redirect(客户端跳转)写jsp的path
     注意action = r3 跳转的到action = r1 时他们不在同一个包中的写法

3.
DynamicResult:设置一个字符串属性用来存返回的path
  private String result;
  public String execute(){
  
  if(){   
   result="/1.jsp";      
  }else{   
   result="/2.jsp";
  }
  return SUCCESS;    
 }
  Struts.xml中
  <result>${result}</result>

4.
global results
  <global-results>
  <result name="*">/index.jsp</result>
  </global-results>
  
  
  
  
  
  
OGNL:
1.
取Action中的属性:   <s:property value="xxx"/>
调用Action中的方法:    <s:property value="xxx()"/>
取对象的属性:    <s:property value="user.userName"/>
调用对象的方法:    <s:property value="user.self()"/>
取静态属性:  @类名@属性名 <s:property value="@com.yu.UserAction@PASSWORD"/>
调用静态方法: @类名@方法名<s:property value="@com.yu.UserAction@staticMethod()"/>  
注意:后两个类名要写全  com.xxx.xxxx
调用静态方法  需要改struts.xml 默认配置      <constant name="struts.ognl.allowStaticMethodAccess" value="true"/>

转载于:https://www.cnblogs.com/Ryan512/archive/2012/12/07/2806525.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值