Struts2使用OGNL表达式封装输入数据

我们在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 >

 

 

你可能感兴趣的:(html,jsp,struts,idea)