属性驱动:
1、概念
能够利用属性驱动获取页面表单元素的内容
2、步骤
1、在action中声明属性,属性的名称和页面元素中name属性的值保持一致
2、action中的属性必须有set和get方法
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>
原理图例如以下:
注意事项:
1、 必须使用struts2默认的拦截器栈中的ParameterInterceptor
2、 Action中的属性和表单中的name属性的值保持一致
3、 利用valueStack.setValue方法能够赋值了
总结:jsp页面表单中有name为username和password的文本框,相应在action中放入了username和password的相应属性。当jvm运行时,会先运行action,action会被压入栈顶,这样action的属性就暴露在了对象栈(对象栈的特点就是能够在jsp中利用ognl表达式直接取出属性的值)中,然后底层运行了ParameterInterceptor类的doIntercept方法,例如以下图:
在这种方法中具有拦截器的详细处理细节,并且表单中的数据被存放在ParameterInterceptor类中的Map结构中,即Map<String,Object>:[{username:value},{password:value}],它是通过valueStack将其放到栈顶即valueStack.setValue()方法(为action中的属性赋值),最后doInterceptor()返回invocation.invoke()将放行继续往下运行,之后运行action中的方法,而action中的属性在这步已经赋值了。
模型驱动:
1、假设页面上元素内容太多,用属性驱动实现,action中代码就会非常庞大,这个时候能够考虑用模型驱动来实现
2、步骤
1、action实现一个接口ModelDriver
2、在action中声明一个属性,该属性会封装页面中的数据,而且用new的方法给该属性创建对象
3、填充接口中的方法getModel,返回该属性的值
public class UserAction implementsModelDriver<User>{
privateUser model = new User();
publicUser getModel(){
returnthis.model;
}
}
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>
原理图例如以下:
从上图能够看出,ModelDriverInterceptor有两个作用:
1、 当前请求的action必须实现ModelDriver接口
2、 把model对象放入到了栈顶
总结:
当表单中数据提交到相应的action中时,struts2容器会创建action,而且把action放入到栈顶,实现ModelDriven的action类在对象栈中会有一个属性为model(username,password),之后ModelDrivenInterceptor类会把action中的model属性放入栈顶(这样model属性直接能够訪问了),之后再由ParameterInterceptor类负责把页面上表单中的值赋值给对象栈中的属性,由该类中的doInterceptor方法返回invocation.invoke()放行,回到action中,最后由jsp页面通过ognl表达式显示相应的信息。