Struts2 用DomainModel接收参数

1、struts.xml

<struts>
 <constant name="struts.devMode" value="false" /> 
 <constant name="struts.enable.DynamicMethodInvocation" value="true" />
 <constant name="struts.i18n.encoding" value="utf-8"></constant>
 <package name="user"  extends="struts-default">                 
        <action name="user" class="com.wxh.action.UserAction" >
            <result >/useraddsuccess.jsp</result>
        </action>         
  </package>  
</struts>

2、index.jsp

<body>
<form action="user!add" method="get">  
        姓名:<input type="text" name="user.name"/><br>  
        年龄:<input type="text" name="user.age"/><br>  
        <input type="submit" value="添加">  
</form>  
</body>

3、UserAction.java

package com.wxh.action;

import com.wxh.model.User; 
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {	
	private static final long serialVersionUID = 1L;
	 
	private User user;
	
	public String add(){
		System.out.println("name="+user.getName());
		System.out.println("age="+user.getAge());
		return SUCCESS;
	}
	public User getUser(){
		return user;
	}
	public void setUser(User user){
		this.user=user;
	}
	
}

4、User.java

package com.wxh.model;

public class User {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	

}


Struts2 用DomainModel接收参数_第1张图片


你可能感兴趣的:(struts2.0)