1. JSP文件调用格式:
<a href="user/user!add?user.name=a&user.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;
public class UserAction extends ActionSupport {
private User user;
//private UserDTO userDTO;
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;
}
}
Model:
package com.bjsxt.struts2.user.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; } }
衍伸: DTO:通过DTO new一个User对象出来,把值传过去: newUser(UserDTO dto)()
package com.bjsxt.struts2.user.dto; public class UserDTO { private String name; private String password; private String confirmingPassword; }
时间: 2024-10-08 18:53:37