Struts2使用OGNL表达式封装输入数据(直接封装Map类型)

struts2使用OGNL表达式封装数据,不仅支持对象,而且还支持诸如List,Map的容器

User.java

 

package  HelloWorld;

public   class  User  {
    
private String username;
      
private String password;
      
private String[] books;
      
public String[] getBooks() {
            
return books;
        }

        
public void setBooks(String[] books) {
            
this.books = books;
        }

        
public String getUsername() {
            
return username;
        }

        
public void setUsername(String username) {
            
this.username = username;
        }

        
public String getPassword() {
            
return password;
        }

        
public void setPassword(String password) {
            
this.password = password;
        }

}

 LoginAction.java

 

package  HelloWorld;

import  java.util.Map;

import  com.opensymphony.xwork2.Action;
import  com.opensymphony.xwork2.ActionContext;

public   class  LoginAction  implements  Action {
 
 
private String tip;

private Map<String,User> users;



public Map<String, User> getUsers() {
    
return users;
}


public void setUsers(Map<String, User> users) {
    
this.users = users;
}


public String execute() throws Exception{
    
//用第一个user做逻辑判断
      if(this.getUsers().get("one").getUsername().equals("admin")&&this.getUsers().get("one").getPassword().equals("1234")){
          ActionContext.getContext().getSession().put(
"user"this.getUsers().get("one").getUsername());
          BookService bs
=new BookService();
          
this.getUsers().get("one").setBooks(bs.getBooks());
          
this.setTip("welcome welcome");
          
return SUCCESS;
      }
else{
          
return ERROR;
      }

  }


public String getTip() {
    
return tip;
}

public void setTip(String tip) {
    
this.tip = tip;
}



  
}

 

index.jsp

 

<% @ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding
="gb2312"
%>
<% @ taglib prefix="s" uri="/struts-tags"  %>
< html >
  
< head >  
    
< title ></ title >
  
</ head >
  
< body >       

     
< s:form  action ="Login" >
       
< s:textfield  name ="users['one'].username"  key ="username" ></ s:textfield >
       
< s:textfield  name ="users['one'].password"  key ="password" ></ s:textfield >
       
< s:textfield  name ="users['two'].username"  key ="username" ></ s:textfield >
       
< s:textfield  name ="users['two'].password"  key ="password" ></ s:textfield >
       
< s:submit  value ="login" ></ s:submit >
     
</ s:form >                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
   
  
</ body >
</ html >

上面使用users['one'].username直接将输入数据封装成Map容器中的两个对象

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值