分享

Struts2的Collection和Map支持

 pengx 2008-11-23

struts2提供了从页面直接封装到List的功能,实现起来有两个办法

1.使用泛型定义集合中的对象类型
2.配合配置文件定义集合中的对象类型

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.List;

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

public class LoginAction implements Action{
 
 
private String tip;

private List users;





public List getUsers() {
    
return users;
}


public void setUsers(List users) {
    
this.users = users;
}


public String execute() throws Exception{
    
//用第一个user做逻辑判断
      if(((User)this.getUsers().get(0)).getUsername().equals("admin")&&((User)this.getUsers().get(0)).getPassword().equals("1234")){
          ActionContext.getContext().getSession().put(
"user", ((User)this.getUsers().get(0)).getUsername());
          BookService bs
=new BookService();
          ((User)
this.getUsers().get(0)).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;
}



  
}

 

LoginAction_conversion.properties (放在和LoginAction同目录)

Element_users=HelloWorld.User 

表示集合中的Element类型是User,如果是Map类型,则还需要定义Key_users=****

 

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[0].username" key="username"></s:textfield>
       
<s:textfield name="users[0].password" key="password"></s:textfield>
       
<s:textfield name="users[1].username" key="username"></s:textfield>
       
<s:textfield name="users[1].password" key="password"></s:textfield>
       
<s:submit value="login"></s:submit>
     
</s:form>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
   
  
</body>
</html>

 

如果使用泛型定义List users,则可以省略LoginAction-conversion.properties文件

private List<List> users

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多