Struts2中,支持直接使用领域对象获取用户输入的值,再也不用像Servlet中要Request.request.getparameter(" ");才能获取到; Struts中的领域对象 ,ModelDriven ,action属性三种方式
Struts中的领域对象;将类的属性写成一个User类,在Struts转发的页面会创建这个类,并创建set和
get方法;代码下面已经实现 ; 代码下面已经实现
action属性;与java bean的方式差不多 ;代码下面已经实现
ModelDriven ;需要实现Action,ModelDriven接口,在实现的接口页面 手工穿件User的对象,不能创建Set和Get方法 ,实现getModel()方法 ;代码下面已经实现
1,Struts中的领域对象:
a,登陆页面;
<form action="login" method="post"> 登录页面: 账号:<input type="text" name="user.username" /> 密码:<input type="password" name="user.userpwd"/> <input type="submit" value="提交" /> </form>
b,struts.cml配置文件
<package name="default" namespace="/" extends="struts-default"> <action name="hello" > <result >/login.html</result> </action> </package> <package name="main" extends="struts-default" > <action name="login" class="ActionDemo.ActionDemoSupport"> <result name="hello">/1.jsp</result> <result name="hellow">/loginfail.html</result> </action> </package>
c,User类
package ActionDemo; import java.io.Serializable; public class User implements Serializable{ private String username; private String userpwd; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpwd() { return userpwd; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } }
d,Struts的转发类
public class ActionDemoSupport extends ActionSupport { //接收username和userpwd传过来的参数 private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { if("scott".equals(user.getUsername())&&"tiger".equals(user.getUserpwd())){ return "hello"; }else{ return "hellow"; } }
e,jsp显示页面
<%@ taglib prefix="s" uri="/struts-tags" %>
欢迎:<s:property value="user.username" />登陆
2,ModelDriven操作领域对象
a,登陆页面;
<form action="login" method="post"> 登录页面: 账号:<input type="text" name="username" /> 密码:<input type="password" name="userpwd"/> <input type="submit" value="提交" /> </form>
b,Struts,xml配置
<package name="default" namespace="/" extends="struts-default"> <action name="hello" > <result >/login.html</result> </action> </package> <package name="main" extends="struts-default" > <action name="login" class="ActionDemo.ActionDemoSupport"> <result name="hello">/1.jsp</result> <result name="hellow">/loginfail.html</result> </action> </package>
c, User类
public class User implements Serializable{ private String username; private String userpwd; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpwd() { return userpwd; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } }
d,使用ModelDriver接收用户数据
public class ActionDemoSupport implements Action,ModelDriven<User> { //ModelDriven接收用户输入必须要new ,action和领域对象接收用户输入是不需要创建对象的 private User user =new User(); @Override public User getModel() { // TODO Auto-generated method stub return user; } @Override public String execute() throws Exception { if ("scott".equals(user.getUsername()) && "tiger".equals(user.getUserpwd())) { return "hello"; } return "hellow"; }
e,jsp显示页面
<%@ taglib prefix="s" uri="/struts-tags" %>
欢迎:<s:property value="username" />登陆
3,action属性操作接收用户输入
a,登陆页面 ;
<form action="login" method="post"> 登录页面: 账号:<input type="text" name="username" /> 密码:<input type="password" name="userpwd"/> <input type="submit" value="提交" /> </form>
b,struts.xml配置
<package name="default" namespace="/" extends="struts-default"> <action name="hello" > <result >/login.html</result> </action> </package> <package name="main" extends="struts-default" > <action name="login" class="ActionDemo.ActionDemoSupport"> <result name="hello">/loginsucc.html</result> <result name="hellow">/loginfail.html</result> </action> </package>
c, struts的action转发类
package ActionDemo; import com.opensymphony.xwork2.ActionSupport; /** * * @author Administrator Struts里面的转发类 */ public class ActionDemoSupport extends ActionSupport { //接收username和userpwd传过来的参数 private String username; private String userpwd; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpwd() { return userpwd; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } //转发的方法 @Override public String execute() throws Exception { System.out.println(">"+username+"<>"+userpwd+"<"); if ("scott".equals(username) && "tiger".equals(userpwd)) { return "hello"; } else { return "hellow"; } } }