struts2自动接收表单数据
提交页面主要代码:
<form method="post" action="messageAction">
<input type="text" name="userName" value="userName Andy"/><br/>
<input type="submit" value="提交"/>
</form>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
action类:
- package action;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class MessageAction extends ActionSupport{;
- private String userName; //变量名要和表单的名字一样,这样才能自动接收表单数据
- private static final long serialVersionUID = 1L;
- //方法get和set不能少,不然会取不到表单数据。
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String execute(){
- System.out.println("execute");
- System.out.println(userName);
- ServletActionContext.getRequest().setAttribute("info",userName);
- return SUCCESS;
- }
- }