用域模型接收参数

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接收参数的更多相关文章

  1. 01_8_Struts用DomainModel接收参数

    01_8_Struts用DomainModel接收参数 1. 配置struts.xml文件 <package name="user" namespace="/use ...

  2. Struts2_用ModelDriven接收参数

    通过实现 ModelDriven 接口来接收请求参数,这种方法用的比较少,一般还是用前两种. 请求: <a href="user/user!add?name=xiaoer&ag ...

  3. Struts2中Action接收参数的四种形式

    1.Struts2的Action接收参数的三种形式.      a. 使用Action的属性接收(直接在action中利用get方法来接收参数):                   login.js ...

  4. Struts2中Action接收参数的方法

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt112 Struts2中Action接收参数的方法主要有以下三种: 1.使用A ...

  5. Struts2中Action接收参数的方法主要有以下三种:

    Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数(最原始的方式):     a.定义:在Action类中定义属性,创建get和set方法:     b.接 ...

  6. 学习笔记之Struts2—浅析接收参数

    最近自己通过视频与相关书籍的学习,对action里面接收参数做一些总结与自己的理解. 0.0.接收参数的(主要)方法   使用Action的属性接收参数 使用DomainModel接收参数 使用Mod ...

  7. struts2学习笔记(4)接收参数

    ①用action属性接收 登录界面例子 在webroot下创建login.jsp和success.jsp login.jsp中加入表单: <form action="LoginActi ...

  8. Struts2中Action接收参数

    Struts2中Action接收参数的方法主要有以下三种: Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数:     a.定义:在Action类中定义属 ...

  9. Struts2 DomainModel、ModelDriven接收参数

    一.DomainModel(域模型) 1. 应用场景:一般我们在struts2的action中接收参数通常是如下方式 package cn.orlion.user; import com.opensy ...

随机推荐

  1. 提交表单存在html标签报错-检测到有潜在危险的 Request.Form 值

    1..aspx页面 在.aspx文件头中加入这句<%@ Page validateRequest="false" %> 2.通用方法 修改web.config文件, & ...

  2. Job for postfix.service failed because the control process exited with error code. See "systemctl status postfix.service" and "journalctl -xe" for details.

    这是因为防火墙或者配置文件导致,无法启动的邮件服务!! 首先关闭防火墙! 修改配置文件 vim /etc/postfix/main.cf inet_protocols = ipv4 inet_inte ...

  3. IDEA 中 使用 git(Git)

    GitLab GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务.安装方法是参考GitLab在GitHub上的Wiki页面. Git Git( ...

  4. yum安装zabbix故障报错

    装zabbix时报错 [root@zabbix ~]# rpm -ivh zabbix-server-mysql-2.2.3-1.el6.x86_64.rpm zabbix-web-mysql-2.2 ...

  5. Nginx静态服务,域名解析

    安装这里就不写了在LNMP里有具体的安装 1.1 常用来提供静态Web服务的软件有如下三种:     Apache:这是中小型Web服务的主流,Web服务器中的老大哥.     Nginx:大型网 ...

  6. BeautifulSoup4模块的使用

    1. 安装 pip3 install beautifulsoup42. 使用 from bs4 import BeautifulSoup obj = BeautifulSoup("HTML内 ...

  7. ConvertLongToInstantUtil

    package com.test; import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneId ...

  8. Python镜像源

    镜像源 官方:https://pypi.python.org/pypi 豆瓣:https://pypi.doubanio.com/simple/ 阿里:http://mirrors.aliyun.co ...

  9. Silverlight 中图片路径的设置

    在Silverlight中图片的设置方法有几种 如上图在一个工程中有个images文件夹,buttons.xaml页面中的Image控件要引用一张图片 第一种方法 xaml: <Image x: ...

  10. promise之nodejsQ的详细用法总结

    这里主要讲node.js中Q的各种用法及说明总结,不详细介绍promise及原理.关于promise介绍可以查看我的另一篇文章: https://www.cnblogs.com/yzeng/p/976 ...