1. 模型驱动

示例:

注册表单reg.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>reg.jsp</title>
</head>
<body>
<s:actionerror/>
<s:form namespace="/md" action="MdAction_reg" method="post" theme="xhtml" validate="true">
<s:textfield name="name" label="UserName" />
<s:textfield name="age" label="UserAge" />
<s:submit />
</s:form>
</body>
</html>

用户显示list页面 userList.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>reg.jsp</title>
</head>
<body>
<table border="1">
<tr>
<td>id</td><td>name</td><td>age</td><td>edit</td>
</tr>
<s:iterator value="userList">
<tr>
<td><s:property value="id" /></td>
<td><s:property value="name" /></td>
<td><s:property value="age" /></td>
<td>
<a href='<s:url action="MdAction_edit" namespace="/md"><s:param name="uid" value="id" /></s:url>'>edit</a>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>

处理action,模型驱动需要实现ModelDriven 接口,实现Preparable 接口,在modelDriven拦截器之前先运行 prepare拦截器。

package cn.itcast.struts2.modeldriven;

import java.util.ArrayList;
import java.util.List; import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation; import cn.itcast.struts2.model.User; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable; /**
* MdAction:某型驱动
*/
public class MdAction extends ActionSupport implements ModelDriven<User>,Preparable {
private static final long serialVersionUID = -6933309304624396640L; private String name; private Integer uid ; private User user = new User(); private List<User> userList ; public String reg() {
return "success";
} @SkipValidation
public String toRegView() {
System.out.println("toRegView");
return "regView";
} /**
* 查询所有用户
*/
public String findAllUsers(){
userList = new ArrayList<User>();
User u = null ;
for(int i= 0 ; i < 10 ; i ++){
u = new User();
u.setId(1 + i);
u.setName("tom" + i);
u.setAge(20 + i);
userList.add(u);
}
return "userListView";
} public String edit(){
return "editView" ;
} //
public User getModel() {
return user;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public List<User> getUserList() {
return userList;
} public void setUserList(List<User> userList) {
this.userList = userList;
} public Integer getUid() {
return uid;
} public void setUid(Integer uid) {
this.uid = uid;
} /**
* 该方法在getModel之前运行,在modelDriven拦截器之前先运行
*/
public void prepareDoEdit() throws Exception {
//
User u = new User();
u.setId(uid);
u.setName("jerry");
u.setAge(30);
user = u ;
} public void prepare() throws Exception {
}
}

struts配置文件md.xml, 需要添加paramsPrepareParamsStack, 在prepare之前填充参数。

<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="MdPkg" namespace="/md" extends="struts-default">
<action name="MdAction_*" class="cn.itcast.struts2.modeldriven.MdAction" method="{1}">
<result name="success">/md/reg.jsp</result>
<result name="regView">/md/reg.jsp</result>
<result name="editView">/md/edit.jsp</result>
<result name="userListView">/md/userList.jsp</result>
<interceptor-ref name="paramsPrepareParamsStack" />
</action>
</package>
</struts>

 

JavaWeb -- Struts2 模型驱动的更多相关文章

  1. 【Java EE 学习 36】【struts2】【struts2系统验证】【struts2 ognl值栈】【struts2 ongl标签】【struts2 UI标签】【struts2模型驱动和令牌机制】

    一.struts2系统验证 1.基于struts2系统验证的方式实际上就是通过配置xml文件的方式达到验证的目的. 2.实际上系统校验的方法和手工校验的方法在底层的基本实现是相同的.但是使用系统校验的 ...

  2. struts2 模型驱动的action赋值优先顺序

    struts2 模型驱动的action赋值优先顺序: 1.优先设置model的属性. 2.如果model属性中没有对应的成员变量,则向上冒泡,寻找action中的属性进行set. 如果action中的 ...

  3. struts2 模型驱动

    public class User3Action extends ActionSupport implements ModelDriven<User> { private User use ...

  4. Struts2 模型驱动及页面回显

    * 要从页面中获取表单元素的值,需要在动作类中声明与页面元素同名的属性.导致动作类中既有javabean又有业务方法.        * 将javabean和业务方法进行分离:        * 将重 ...

  5. [java]struts2 模型驱动 2016-05-01 21:40 702人阅读 评论(19) 收藏

    一开始敲网上商城的时候,对于数据的传递方式我是很惊艳了一把的,感觉特别高大上,就感觉,竟然不用像.net一样取谁的值,给谁赋值这样,只要需要用的时候,简单的get一下就ok了,简直高大上啊. 然后发现 ...

  6. (九)Struts2模型驱动和属性驱动

    出于结构清晰的考虑,应该采用单独的Model实例来封装请求参数和处理结果,这就是所谓的模型驱动, 所谓模型驱动,就是使用单独的JavaBean来贯穿整个MVC流程. 所谓属性驱动,就是使用属性来作为贯 ...

  7. JavaWeb -- Struts2 验证框架

    1. 验证框架 示例 表单提交Jsp, reg.jsp <%@ page language="java" contentType="text/html; chars ...

  8. JavaWeb -- Struts2 ResultType细化, 国际化

    1. ResultType细化 <result-types> <result-type name="chain" class="com.opensymp ...

  9. JavaWeb -- Struts2 构建视图:标签和结果, UI组件标签

    1. 示例 action 注入数据 和 处理action /** * OgnlAction */ public class UiAction extends ActionSupport { priva ...

随机推荐

  1. cdr X6 64位32位缩略图补丁包

    cdr X6 64位32位缩略图补丁包下载 安装了X6没有缩略图的话,点击下面链接下载安装插件即可 点击下载

  2. mysql_系统数据库认识

    show databases:查看mysql自带数据库有information_schema,mysql, performance_schema, test information_schema数据库 ...

  3. 淘宝订单数据转CSV

    <html> <body> <div id="result"> </div> <div> <textarea st ...

  4. java 子类不能继承父类的static方法

    先来看一段代码 /** * Created by bjchengpeng on 2018/7/19. */ /**运行结果 * woof * woofaa * * woof * Basenjiaa * ...

  5. ubuntu14.04 编译安装CPU版caffe

      本文,试图中一个干净的ubuntu14.04机器上安装caffe的cpu版本. http://blog.csdn.net/sinat_35188997/article/details/735304 ...

  6. 利用kubeadm部署kubernetes 1.7 with flannel

    一.Installation 1.安装环境为CentOS 7 2.安装Docker yum install -y docker systemctl enable docker systemctl st ...

  7. 【MonogDB】The description of index(二) Embedded and document Index

    In this blog, we will talk about another the index which was called "The embedded ". First ...

  8. leetcode -day 15 Distinct Subsequences

    1.  Distinct Subsequences  Given a string S and a string T, count the number of distinct subsequen ...

  9. openPOWERLINK代码在vs2008下编译

    以openPOWERLINK_V1.08为例: 1.在主目录下新建Build目录 2.使用cmake-gui对代码进行配置 3.配置完成后生成工程文件xxx.sln 4.使用vs2008打开上述文件, ...

  10. R中apply函数族

    参考于:http://blog.fens.me/r-apply/ 1. apply的家族函数 2. apply函数 apply函数是最常用的代替for循环的函数.apply函数可以对矩阵.数据框.数组 ...