Struts2模型驱动

Struts2 2017/04/25 00:04

1.属性驱动

struts2属性驱动和模型驱动帮助我们完成了数据自动获取,数据自动封装

表单中的name值要与action中定义的属性值一致

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
		"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
		"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
		<package name="userlogin" extends="struts-default">
			<action name="loginAction" class="com.cheng.action.LoginAction">
				<result name="success">/index.jsp</result>
			</action>
		</package>
</struts>

LoginAction.action

package com.cheng.action;
import com.opensymphony.xwork2.ActionSupport;
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+password);
		return "success";
	}

}

index.jsp

<body>
<form action="loginAction.action" method="post">
	username:<input type="text" name="username"><br>
	password:<input type="password" name="password"><br>
	<input type="submit" value="提交">
</form>
</body>

2.模型驱动

action中需要实现ModelDriven接口,接口中的泛型为我们要封装的实体类

当请求发送到action之前,调用MlogionAction类中的getModel()获取要将表单数据封装到哪个实例化的对象中。获得到该对象之后,我们可以获得类类型。获得类类型后,获得类中属性。request.getParameters获得表单提交的所有数据名。从而获取值。如果表单提交的name值与实体类中属性名一致,那么我们将获得表单中的数据封装到us对象当中去

Users.java

package com.cheng.pojo;
public class Users {

	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;
	}

}

MloginAction.java

package com.cheng.action;
import com.cheng.pojo.Users;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class MloginAction extends ActionSupport implements ModelDriven<Users>{

	private Users us=new Users();//前台提交的数据自动封装到对象中

	@Override
	public String execute() throws Exception {
		System.out.println(us.getUsername());
		System.out.println(us.getPassword());
		return "success";
	}

	@Override
	public Users getModel() {
		return us;
	}

}

struts.xml

<struts>
	<package name="userlogin" extends="struts-default">
		<action name="muserlogin" class="com.cheng.action.MloginAction">
			<result name="success">/index.jsp</result>
			<!-- 需要加入两个拦截器:模型封装拦截器,默认的拦截器 -->
			<interceptor-ref name="modelDriven"></interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>
		</action>			
	</package>
</struts>

index.jsp

<body>
    <form action="muserlogin.action" method="post">
    	username:<input type="text" name="username"><br>
    	password:<input type="password" name="password"><br>
    	<input type="submit" value="提交">
    </form>
</body>

3.标签实现模型驱动

Users.java

package com.cheng.pojo;
public class Users {

	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;
	}

}

SloginAction.java

package com.cheng.action;
import com.cheng.pojo.Users;
import com.opensymphony.xwork2.ActionSupport;
public class SloginAction extends ActionSupport{

	private Users us;

	public Users getUs() {
		return us;
	}

	public void setUs(Users us) {
		this.us = us;
	}

	@Override
	public String execute() throws Exception {
		System.out.println(us.getUsername());
		System.out.println(us.getPassword());
		return "success";
	}

}

struts.xml

<struts>
	<!-- 使表单样式不被struts标签控制,从而手动控制-->
	<constant name="struts.ui.theme" value="simple"/>
	<constant name="struts.ui.templateDir" value="template"/>
	<constant name="struts.ui.templateSuffix" value="ftl"/>

	<package name="userlogin" extends="struts-default">
		<action name="suserlogin" class="com.cheng.action.SloginAction">
			<result name="success">/index.jsp</result>
		</action>		
	</package>
</struts>

index.jsp

<body>
<s:form action="suserlogin" method="post" namespace="">
	<!-- us.username action中定义的对象.实体属性 -->
	username:<s:textfield name="us.username"/><br>
	password:<s:password name="us.password"/><br>
	<s:submit value="提交" />
</s:form>
</body>

4.源代码下载 密码:yw3e

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

Powered by codetiler.com