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 ...
随机推荐
- HashMap 1.8的源码分析三
线程安全问题: 在添加时候并没有进行安全考虑,枷锁 所以是线程不安全的,接下来进行代码测试; package com.mmall.concurrency.example.commonUnsafe; i ...
- c语言定义指针类型需注意事项
1)在定义说明语句中,指针变量名之前的星号“*“是指针变量的修饰符,也就是说它所修饰的变量是指针变量. 2)指针变量是用它们所指向的对象类型来区分的.如定义 int *ip,类型int并不是指针的类型 ...
- 【转】eclipse上的.properties文件中文编辑显示问题
首先,解决.properties文件中的中文编辑问题,我们发现,在.properties文件中输入的中文变成了这个样子: 这是eclipse的.properties文件,默认的编码方式是iso-885 ...
- P4345 [SHOI2015]超能粒子炮·改
传送门 看到数据和模数大小就知道要上 lucas 了 然后开始愉快地推公式: 答案为 $\sum _{i=0}^kC_{n}^{i}\ (mod\ 2333)$ 设 $f [ i ] [ j ] = ...
- 解决windows server 2008 r2 登录进入桌面只显示一片蓝色背景
非常高的可能性问题是:explorer.exe进程无法加载.请按ctrl+shift+esc调出任务管理器--文件-新任务-输入‘explorer’--确定即可.如无法解决,请进入安全模式关闭非必要的 ...
- linux系统延时和定时任务
系统延时任务延时任务:只做一次的at命令: 系统定时及延时任务 延时任务:**有输出任务**不会输出到终端上而是发送邮件给你/var/mail/root/执行 mail at 时间 ...
- Spark2.3.0 报 io.netty.buffer.PooledByteBufAllocator.metric
Spark2.3.0依赖的netty-all-4.1.17.Final.jar 与 hbase1.2.0依赖的netty-all-4.0.23.Final.jar 冲突 <!-- Spark2. ...
- inventor卸载不干净
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- inventor安装不了
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- WSGI学习系列WSME
Introduction Web Services Made Easy (WSME) simplifies the writing of REST web services by providing ...