首页 > 代码库 > Struts2中属性驱动与模型驱动

Struts2中属性驱动与模型驱动

属性驱动:

LoginAction.java:

public class LoginAction extends ActionSupport implements ModelDriven<User>{
	private User mdoel = new User();
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return this.mdoel;
	}
	
	public String login(){
		System.out.println(this.getModel().getUsername());
		System.out.println(this.getModel().getPassword());
		return SUCCESS;
	}
}
User.java:
public class User {
	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;
	}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 
		常量
		   用来改变default.properties文件中的常量的设置
	 -->
	<constant name="struts.ui.theme" value=http://www.mamicode.com/"simple">>struts-modeldriver.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="login" namespace="/" extends="struts-default">
		<action name="loginAction_*" method="{1}" class="com.leaf.struts.action.LoginAction">
			<result>index.jsp</result>
		</action>
	</package>
</struts>
web.xml中加入过滤器:

 <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href=http://www.mamicode.com/"">>
原理图如下:



注意事项:

1、  必须使用struts2默认的拦截器栈中的ParameterInterceptor

2、  Action中的属性和表单中的name属性的值保持一致

3、  利用valueStack.setValue方法可以赋值了

模型驱动:

LoginAction.java:

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;
	}
	
	
	public String login(){
		System.out.println(this.username);
		System.out.println(this.password);
		return SUCCESS;
	}
}
struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<!-- 
		常量
		   用来改变default.properties文件中的常量的设置
	 -->
	<constant name="struts.ui.theme" value=http://www.mamicode.com/"simple">>
struts-propertydriver.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="login" namespace="/" extends="struts-default">
		<action name="loginAction_*" method="{1}" class="com.leaf.struts.action.LoginAction">
			<result>index.jsp</result>
		</action>
	</package>
</struts>

web.xml中加入过滤器:

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href=http://www.mamicode.com/"">>

原理图如下:




从上图可以看出,ModelDriverInterceptor有两个作用:

1、  当前请求的action必须实现ModelDriver接口

2、  把model对象放入到了栈顶