Struts2获取表单数据
1:属性驱动
前端页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Index Page</title> </head> <body> <form action="login.action" method="post"> UserName:<input type="text" name="username"/> <br/> Password:<input type="password" name="password"/> <br/> <input type="submit" value="Submit"/> </form> </body> </html>
Action:
package com.zhao.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import com.opensymphony.xwork2.ActionSupport; /** * @ParentPackage:用来指定struts.xml中的package * @Action注解将一个普通Java类标识为可以处理用户请求的Action类 * @Namespace是命名空间 * @author zhao * */ @ParentPackage("default") @Action @Namespace("/") public class LoginAction extends ActionSupport { /* * 属性驱动 */ private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { System.out.println("UserName: "+this.username); System.out.println("Password: "+this.password); return SUCCESS; } }
2:模型驱动
前端页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Index Page</title> </head> <body> <form action="login.action" method="post"> UserName:<input type="text" name="username"/> <br/> Password:<input type="password" name="password"/> <br/> <input type="submit" value="Submit"/> </form> </body> </html>
User:
package com.zhao.entity; import java.io.Serializable; public class User implements Serializable { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Action:
package com.zhao.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.zhao.entity.User; /** * @ParentPackage:用来指定struts.xml中的package * @Action注解将一个普通Java类标识为可以处理用户请求的Action类 * @Namespace是命名空间 * @author zhao * */ @ParentPackage("default") @Action @Namespace("/") public class LoginAction extends ActionSupport implements ModelDriven<User>{ private User user=new User(); @Override public String execute() throws Exception { System.out.println("UserName: "+user.getUsername()); System.out.println("Password: "+user.getPassword()); return SUCCESS; } @Override public User getModel() { if (user==null) { user=new User(); } return user; } }
3:不完全模型驱动
前端页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Index Page</title> </head> <body> <form action="login.action" method="post"> UserName:<input type="text" name="user.username"/> <br/> Password:<input type="password" name="user.password"/> <br/> <input type="submit" value="Submit"/> </form> </body> </html>
User:
package com.zhao.entity; import java.io.Serializable; public class User implements Serializable { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Action:
package com.zhao.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.zhao.entity.User; /** * @ParentPackage:用来指定struts.xml中的package * @Action注解将一个普通Java类标识为可以处理用户请求的Action类 * @Namespace是命名空间 * @author zhao * */ @ParentPackage("default") @Action @Namespace("/") public class LoginAction extends ActionSupport{ private User user=new User(); @Override public String execute() throws Exception { System.out.println("UserName: "+user.getUsername()); System.out.println("Password: "+user.getPassword()); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }