我们在action中有一个封装好的user对象,其中有username和password两个属性

我们可以在页面上直接使用user,username作为输入框的名字,struts2可以直接把输入的username和password封装到user对象中,这点和webwork2如出一辙

 

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 com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction implements Action{
 
 
private String tip;

  
private User user;

public User getUser() {
    
return user;
}


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


public String execute() throws Exception{
      
if(this.getUser().getUsername().equals("admin")&&this.getUser().getPassword().equals("1234")){
          ActionContext.getContext().getSession().put(
"user"this.getUser().getUsername());
          BookService bs
=new BookService();
          
this.getUser().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="user.username" key="username"></s:textfield>
       
<s:textfield name="user.password" key="password"></s:textfield>
       
<s:submit value="login"></s:submit>
     
</s:form>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
   
  
</body>
</html>

 

 

评论
发表评论

您还没有登录,请登录后发表评论

esffor
搜索本博客
最近加入圈子
存档
最新评论