Struts2_用DomainModel接收参数
用域模型接收参数
User类
package com.bjsxt.struts2.user.model;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
UserAction
package com.bjsxt.struts2.user.action; import com.bjsxt.struts2.user.model.User;
import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ private static final long serialVersionUID = -2514433281517403937L; User user;
//private UserDTO userDTO; public String add(){
System.out.println("name = " + user.getName());
System.out.println("age = " + user.getAge());
return SUCCESS;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<constant name="struts.configuration.xml.reload" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/> <package name="user" namespace="/user" extends="struts-default">
<action name="user" class="com.bjsxt.struts2.user.action.UserAction">
<result>/user_add_success.jsp</result>
</action>
</package> </struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
<base href="<%=basePath%>"/>
</head>
<body>
<h1>Hello World~~~</h1>
<a href="user/user!add?user.name=xiaoer&user.age=33">添加用户</a>
</body>
</html>
结果:

有时候,页面输入的值并非就是单纯的与数据库对应的model,也许会加上其他的东西:验证码,确认密码,标识列...
这时候可以在 model 前又加上一层 dto(Data Transfer Object),或者叫VO。
我们可以先用dto接收,接收完在方法中处理了相关业务之后,再把dto中与model相关的值复制到model中,dto相当一个中间对象,两者之间的关系如下图。

链接: http://pan.baidu.com/s/1dFf4HXJ 密码: a73w
Struts2_用DomainModel接收参数的更多相关文章
- 01_8_Struts用DomainModel接收参数
01_8_Struts用DomainModel接收参数 1. 配置struts.xml文件 <package name="user" namespace="/use ...
- Struts2_用ModelDriven接收参数
通过实现 ModelDriven 接口来接收请求参数,这种方法用的比较少,一般还是用前两种. 请求: <a href="user/user!add?name=xiaoer&ag ...
- Struts2中Action接收参数的四种形式
1.Struts2的Action接收参数的三种形式. a. 使用Action的属性接收(直接在action中利用get方法来接收参数): login.js ...
- Struts2中Action接收参数的方法
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt112 Struts2中Action接收参数的方法主要有以下三种: 1.使用A ...
- Struts2中Action接收参数的方法主要有以下三种:
Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数(最原始的方式): a.定义:在Action类中定义属性,创建get和set方法: b.接 ...
- 学习笔记之Struts2—浅析接收参数
最近自己通过视频与相关书籍的学习,对action里面接收参数做一些总结与自己的理解. 0.0.接收参数的(主要)方法 使用Action的属性接收参数 使用DomainModel接收参数 使用Mod ...
- struts2学习笔记(4)接收参数
①用action属性接收 登录界面例子 在webroot下创建login.jsp和success.jsp login.jsp中加入表单: <form action="LoginActi ...
- Struts2中Action接收参数
Struts2中Action接收参数的方法主要有以下三种: Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数: a.定义:在Action类中定义属 ...
- Struts2 DomainModel、ModelDriven接收参数
一.DomainModel(域模型) 1. 应用场景:一般我们在struts2的action中接收参数通常是如下方式 package cn.orlion.user; import com.opensy ...
随机推荐
- CF581B Luxurious Houses 模拟
The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...
- centos下yum搭建安装linux+apache+mysql+php环境教程
我们利用linux系统中yum安装Apache+MySQL+PHP是非常的简单哦,只需要几步就可以完成,具体如下: 一.脚本YUM源安装: 1.yum install wget ...
- #!/usr/bin/env python 是什么
#!/usr/bin/env python 这一行注释经常出现在Python脚本的第一行.这一行到底用来干嘛的呢?原因很简单,任何Python脚本执行都需要靠Python解释器.这一行注释正是告诉计算 ...
- JavaScript权威指南--立即执行函数
千万不要停下追逐梦想的脚步 (function(){ //execute this method immediatly. //content... }());
- 最长上升子序列问题(O(n^2)算法)
[题目描述] 给定N个数,求这N个数的最长上升子序列的长度. [样例输入] 7 2 5 3 4 1 7 6 [样例输出] 4 什么是最长上升子序列? 就是给你一个序列,请你在其中求出一段不断严格上升的 ...
- HDU计算机学院大学生程序设计竞赛(2015’12)Happy Value
Problem Description In an apartment, there are N residents. The Internet Service Provider (ISP) want ...
- 09-排序2 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- Java中的ThreadLocal使用
ThreadLocal用于下面的场景: 1. 不允许多个线程同时访问的资源 2. 单个线程存活过程只使用一个实例 官方定义如下: This class provides thread-local va ...
- Ubuntu 安装 phpredis扩展
官网 https://github.com/phpredis/phpredis 下载->然后解压->上传服务器 /etc/phpredis 进行 cd /etc/phpredisphpiz ...
- js input复选框选中父级同时子级也选中
js实现复选框选中父级元素子级元素也选中,没有子级元素选中父级也不选中的效果 HTML <tr> <td> <label> <input name=" ...