struts2中Action的面向对象式的模型驱动

 

 struts.xml
----------------------------------------------------------------
<action name="registAction2" class="regist.RegistAction2">
    <result name="success">/successpage2.jsp</result>
</action>
----------------------------------------------------------------
Action
----------------------------------------------------------------
package regist;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class RegistAction2 extends ActionSupport implements ModelDriven<User>
{
	User user  = new User();
	@Override
	public User getModel()
	{
		return user;
	}
	
	@Override
	public String execute() throws Exception
	{
		
		System.out.println(user.getName());
		return  SUCCESS;
	}
	
}

----------------------------------------------------------------
User.java
------------------------------------------------------
package regist;

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

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

regist.jsp
--------------------------------------------------
  <body>
	<form action="registAction2.action">
	name:<input type="text" name="name">
	age:<input type="text" name="age">
	<input type="submit" value="submit">
	</form>
  </body>
--------------------------------------------------


result.jsp
--------------------------------------------------
 <body>
	${requestScope.name }
	${requestScope.age }
  </body>
--------------------------------------------------
继属性模式的又一种更加符合面向对象风格的构建bean模型的一种action

你可能感兴趣的:(struts2中Action的面向对象式的模型驱动)