Struts2学习笔记(2)值传递

Struts2中的传值实现


1.action接收jsp传过来的值:
 a.定义一个User类   有name  pwd属性,  添加相应的get,set方法
  
 b.<!-- 方式1 普通java类 -->
  在UserAction中
   添加: private User user;  并添加相应的get,set方法

  <!-- 方式2 实现ModelDriven<T>接口-->

?
1
2
3
4
5
   在UserAction中
   @Override
   public  User getModel() {
    return  user;
   }

  
  <!--方式3 在UserAction中 直接写属性>
  

?
1
2
3
4
5
6
private  String name;
   private  String pwd;
   public  String methodxxx() {
    User user= new  User();
    ...xxx;
   }

  注意:表单里面name定义的方式,根据name更改Action中方法的判断方式
      
 
 c.jsp界面中添加form表单
  (方式1)
  jsp中input 属性中name="user.name"这么写框架就会自动new  user();所以Action不能用user是否为空判断
 

?
1
2
3
4
5
6
  < form  action = "User_register"  method = "post" >
    < label  style = "color:red;" >${errorMsg }</ label >< br />
    注册用户名< input  type = "text"  name = "user.name" />< br />
    注册密码< input  type = "password"  name = "user.pwd" />< br />
    < input  type = "submit"  value = "注册" />
   </ form >

  
  (方式2)
  name="name"这么写如果没有值""则不会自动new User();可用user是否为空判断

?
1
2
3
4
5
6
   < form  action = "User_register"  method = "post" >
   < label  style = "color:red;" >${errorMsg }</ label >< br />
    注册用户名< input  type = "text"  name = "name" />< br />
    注册密码< input  type = "password"  name = "pwd" />< br />
    < input  type = "submit"  value = "注册" />
   </ form >

  
 表单提交之后到UserAction的register中直接用user就可以了。
  框架直接将数据存如user中了  已经

 如果action中class未定义  则调用框架里面默认的action

 <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
---------------------------------以上内容在g:codespace/Struts2代码区间中 Test中--------------------------

2.action传值:
 3种方式都是在action处理
 (1)解耦合:间接使用
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public  String execute(){
   //获得Action的上下文    ActionContext ac=ActionContext.getContext();
   //①获取传值对象    Map<String,Object> request=(Map<String, Object>) ac.get( "request" );
   Map<String,Object> session=ac.getSession();
   Map<String,Object> application=ac.getApplication();
   //②存储值    request.put( "reqValue" , "这是通过request解耦合方式设置的值" );
   session.put( "sessionValue" , "这是通过session解耦合方式设置的值" );
   application.put( "applicationValue" , "这是通过application解耦合方式设置的值" );
    
   return  "success" ;
  }

 (2)实现RequestAware,SessionAware,ApplicationAware
 
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public  class  TestAction02  implements  RequestAware,SessionAware,ApplicationAware{
   private  Map<String,Object> request;
   private  Map<String,Object> session;
   private  Map<String,Object> application;
    
   public  String execute(){
    //2.存储值     request.put( "reqValue" , "这是通过request解耦合RequestAware方式设置的值" );
    session.put( "sessionValue" , "这是通过session解耦合SessionAware方式设置的值" );
    application.put( "applicationValue" , "这是通过application解耦合ApplicationAware方式设置的值" );
     
    return  "success" ;
   }
   @Override
   public  void  setApplication(Map<String, Object> application) {
    this .application=application;
   }
   @Override
   public  void  setSession(Map<String, Object> session) {
    this .session=session;
   }
   @Override
   public  void  setRequest(Map<String, Object> request) {
    this .request=request;
   }
  }

 
 (3)直接使用  耦合方式

?
1
2
3
4
5
6
7
8
9
10
11
12
   public  String execute(){
    //耦合:直接使用     HttpServletRequest request=ServletActionContext.getRequest();
    HttpSession session=request.getSession();
    ServletContext application=ServletActionContext.getServletContext();
    //2.存储值     request.setAttribute( "reqValue" , "这是通过request耦合方式设置的值" );
    session.setAttribute( "sessionValue" , "这是通过session耦合方式设置的值" );
    application.setAttribute( "applicationValue" , "这是通过application耦合方式设置的值" );
     
    return  "success" ;
   }

 
 ------以上三种传值的同一实现的jsp
  

?
1
2
3
4
5
6
7
8
jsp的取值界面
   < h1 >
    request的值:${requestScope.reqValue }< br />
    request的值:s标签---< s:property  value = "#request.reqValue" />< br />
    session的值:${sessionScope.sessionValue }< br />
    session的值:s标签---< s:property  value = "#session.sessionValue" />< br />
    ...
   </ h1 >

 以上三种存值方式都是将值存放在Stack Context中
  
 (4)值栈的使用
  
  ValueStack 由 OGNL框架实现,可以把它简单的看作一个栈(List)
  Stack Context(保存方式是map类型):stack的上下文,它包含一系列对象,包括
  request,session,attr,application,map 等ValueStack中保存的值可以直接取,而stack中的需要在前面加#(request,session,application)
  
  for example ①:(s:debug中有显示):
  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   public  String execute(){
    ActionContext ac=ActionContext.getContext();
    //vs将值推入Value Stack Contents    由ognl框架实现     ValueStack vs=ac.getValueStack();
       
    user.setName( "张三" );
    user.setPwd( "abc" );
    vs.push(user);
     
    User u1= new  User();
    u1.setName( "李四" );
    u1.setPwd( "2345" );
    vs.push(u1);
     
    //这里将值传入Stack Context(保存方式是map类型)stack的上下文,它包含一系列对象     Map<String,Object> request=(Map<String,Object>)ac.get( "request" );
    //2.存储值     request.put( "reqValue" , "这是通过request耦合方式设置的值" );
    request.put( "user" , user);
     
    return  "success" ;
   }

  
  jsp的取值界面
   request中取值:<s:property value="#request.user.name"/><br/>
   

?
1
2
3
4
5
< s:property  value = "name" />< br /> <!-- 默认从栈顶开始找数据 -->
    < s:property  value = "[0].name" />< br /> <!-- [0]值栈位置 -->
    < s:property  value = "[1].name" />< br />
     
    < s:property  value = "[1].toString()" />< br />  <!--User类型中重写toString方法-->

  for example ②:(s:debug中无显示)
  

?
1
2
3
4
5
6
7
8
9
10
11
public  String execute() {
    ActionContext ac = ActionContext.getContext();
    ValueStack vs = ac.getValueStack();
    List<String> ls =  new  ArrayList<String>();
    ls.add( "s1" );
    ls.add( "s2" );
    ls.add( "s3" );
     
    vs.push(ls);
    return  "success" ;
   }

  
  jsp的取值界面

?
1
2
3
4
5
    < s:iterator  value = "[0]" >
  
     < s:property />
  
    </ s:iterator >

   
  
  
3. s标签调用方法

?
1
2
3
4
5
6
7
8
9
  <!--s:set设定值var 定义名字,value定义值 -->
  
  < s:set  var = "n"  value = "200" />
  
  <!--s:property 调用值 详解请看
http://blog.csdn.net/lfp0202/article/details/6188484
  -->
  
     < s:property  value = "#n" />

  注意:s:property标签可以调用类的方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
< s:property  value = "[1].toString()" />< br />  <!--调用user类中的toString()方法-->
  <!-- 这个是调用静态方法的写法 -->
  <!-- 调用静态方法要开启:<constant name="struts.ognl.allowStaticMethodAccess" value="true"/> -->
  < s:property  value = "@java.lang.Math@PI" />< br />
  <!-- 调用jdk自带的静态方法,就可以省略类名 -->
  < s:property  value = "@@PI" />< br />
   
  < s:property  value = "@com.yc.bean.User@add(3,6)"  default = "没有调到" />
   
  <!-- 普通值的存取 -->
      < s:set  var = "n"  value = "200" />
      < s:property  value = "#n" />< br />
       
      <!-- 数组或者list对象的存取 -->
      < s:set  var = "list"  value = "{'lll','sss','ddd'}" />< br />
      < s:iterator  value = "list" >
       < s:property />
      </ s:iterator >< br />
       
      <!-- map对象的存取 -->
      < s:set  var = "maps"  value = "#{'aaa':'AAA','bbb','BBB','ccc','CCC' }" />
      < s:property />< br />
      < s:iterator  value = "#maps" >
       < s:property  value = "key" />------< s:property  value = "value" />
      </ s:iterator
    
   <!--map<String,Object>这种样子的对象-->
    
   < s:iterator  value = "profileMap"  id = "entry" >
    < s:iterator  value = "#entry.value" >
     < tr >
      < td >< s:property  value = "id"  /></ td >
      < td >< s:property  value = "name"  /></ td >
      < td >< s:property  value = "birthday"  /></ td >
      < td >< s:property  value = "gender"  /></ td >
      < td >< s:property  value = "career"  /></ td >
      < td >< s:property  value = "address"  /></ td >
      < td >< s:property  value = "mobile"  /></ td >
     </ tr >
    </ s:iterator >
   </ s:iterator >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值