Struts2--ModelDriven接收参数

时间:2022-05-22 16:52:58

1. JSP文件调用格式:

<a href="user/user!add?name=a&age=8">添加用户</a>

2. struts.xml文件:

 <package name="user" extends="struts-default" namespace="/user">
<action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result>/user_add_success.jsp</result>
</action>
</package>

3. action文件:和jsp传递过来的name和age一一对应传值进去

package com.bjsxt.struts2.user.action;

import com.bjsxt.struts2.user.model.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements ModelDriven<User>{

private User user = new User();   //里面并没有name和age,而是通过getModel()返回的对象传值

public String add() {
  System.out.println("name=" + user.getName());
  System.out.println("age=" + user.getAge());
  return SUCCESS;
}

@Override
public User getModel() {
  return user;
}
}