`
esffor
  • 浏览: 1352444 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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容器中的两个对象

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics