码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2中属性驱动与模型驱动

时间:2014-06-01 09:20:40      阅读:266      评论:0      收藏:0      [点我收藏+]

标签: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="simple"></constant>
	<!-- 
		一般在开发的情况下,设置struts.devMode为true,这样修改完xml文件以后不用重新启动了
	 -->
	<constant name="struts.devMode" value="true"/>
	<include file="struts-modeldriver.xml"></include>
</struts>	
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="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  	<s:form action="loginAction_login.action">
    	<table>
    		<tr>
    			<td>用户名</td>
    			<td><s:textfield name="username"/></td>
    		</tr>
    		<tr>
    			<td>密码</td>
    			<td><s:password name="password"/></td>
    		</tr>
    		<tr>
    			<td></td>
    			<td><s:submit/></td>
    		</tr>
    	</table>
    </s:form>
  </body>
</html>

原理图如下:


bubuko.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="simple"></constant>
	<!-- 
		一般在开发的情况下,设置struts.devMode为true,这样修改完xml文件以后不用重新启动了
	 -->
	<constant name="struts.devMode" value="true"/>
	<include file="struts-propertydriver.xml"></include>
</struts>	

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="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  	<s:form action="loginAction_login.action">
    	<table>
    		<tr>
    			<td>用户名</td>
    			<td><s:textfield name="username"/></td>
    		</tr>
    		<tr>
    			<td>密码</td>
    			<td><s:password name="password"/></td>
    		</tr>
    		<tr>
    			<td></td>
    			<td><s:submit/></td>
    		</tr>
    	</table>
    </s:form>
  </body>
</html>

原理图如下:


bubuko.com,布布扣

bubuko.com,布布扣

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

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

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



Struts2中属性驱动与模型驱动,布布扣,bubuko.com

Struts2中属性驱动与模型驱动

标签:struts2

原文地址:http://blog.csdn.net/u013361361/article/details/27740629

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!