提交页面主要代码:

<form method="post" action="messageAction">

   <input type="text" name="userName" value="userName Andy"/><br/>

   <input type="submit" value="提交"/> 

  </form>

struts2自动接收表单数据_表单

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

action类:


  1. package action;  
  2. import org.apache.struts2.ServletActionContext;  
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. public class MessageAction extends ActionSupport{;  
  5.     private String userName; //变量名要和表单的名字一样,这样才能自动接收表单数据  
  6.     private static final long serialVersionUID = 1L;  
  7.       
  8.     //方法get和set不能少,不然会取不到表单数据。  
  9.     public String getUserName() {  
  10.         return userName;  
  11.     }  
  12.     public void setUserName(String userName) {  
  13.         this.userName = userName;  
  14.     }  
  15.   
  16.     public String execute(){  
  17.         System.out.println("execute");  
  18.         System.out.println(userName);  
  19.         ServletActionContext.getRequest().setAttribute("info",userName);  
  20.         return SUCCESS;  
  21.     }  
  22. }