1 struts2 获得参数 1-属性驱动获得参数

1 Demo8Action

package www.test.c_param;

import java.util.Date;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; //struts2获得参数的方式1-属性驱动获得参数
public class Demo8Action extends ActionSupport { //每次请求Action时都会创建新的Action实例对象
public Demo8Action() {
super();
System.out.println("demo8Action被创建了!");
} public String execute() throws Exception {
ActionContext actionContext = ActionContext.getContext();
actionContext.put("username", username);
actionContext.put("age", age);
actionContext.put("birthday", birthday);
return SUCCESS;
} //准备与参数键名称相同的属性
private String username; //自动类型转换 只能转换8大基本数据类型以及对应包装类
private Integer age; //支持特定类型字符串转换为Date ,例如 yyyy-MM-dd
private Date birthday; public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
} }

2 struts.xml

<action name="Demo8Action" class="www.test.c_param.Demo8Action" method="execute">
<result name="success" type="dispatcher">/regist.jsp</result>
</action>

3 form1.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/Demo8Action" method="post">
用户名:<input type="text" name="username"><br/>
年龄:<input type="text" name="age"><br/>
生日:<input type="text" name="birthday"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>

4 regist.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
用户名:${requestScope.username }<br/>
年龄:${requestScope.age }<br/>
出生日期:${requestScope.birthday }<br/>
</body>
</html>

2 struts2 获得参数 2-对象驱动

1 User类

package www.test.domain;

import java.util.Date;

public class User {

    private String username;
private Integer age;
private Date birthday;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User [username=" + username + ", age=" + age + ", birthday=" + birthday + "]";
} }

2 Demo9Action

package www.test.c_param;

import com.opensymphony.xwork2.ActionSupport;

import www.test.domain.User;

//struts2获得参数的方式1-属性驱动获得参数
public class Demo9Action extends ActionSupport { //准备user对象
private User user; public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user =
user;
}
}

3 struts.xml

<action name="Demo9Action" class="www.test.c_param.Demo9Action" method="execute">
<result name="success" type="dispatcher">/form2.jsp</result>
</action>

4 form2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/Demo9Action" method="post">
用户名:<input type="text" name="user.username"><br/>
年龄:<input type="text" name="user.age"><br/>
生日:<input type="text" name="user.birthday"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>

3 struts2 获得参数 3-模型驱动

1 Demo10Action

package www.test.c_param;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; import www.test.domain.User; //struts2 获得参数 3-模型驱动
public class Demo10Action extends ActionSupport implements ModelDriven<User> { // User对象
private User user = new User(); public String execute() throws Exception {
ActionContext context = ActionContext.getContext();
context.put("username", user.getUsername());
context.put("age", user.getAge());
context.put("username", user.getBirthday());
return SUCCESS;
} @Override
public User getModel() { return user;
} }

2 struts.xml

<action name="Demo10Action" class="www.test.c_param.Demo10Action" method="execute">
<result name="success" type="dispatcher">/regist.jsp</result>
</action>

3 form3.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>struts2获得参数方式3-模型驱动</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/Demo8Action" method="post">
用户名:<input type="text" name="username"><br/>
年龄:<input type="text" name="age"><br/>
生日:<input type="text" name="birthday"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>

4 struts2 获得参数方式-集合类型封装

1 Demo11Action

package www.test.c_param;

import java.util.List;
import java.util.Map; import com.opensymphony.xwork2.ActionSupport; //struts2 封装集合类型参数
public class Demo11Action extends ActionSupport {
//list
private List list;
//map
private Map<String,String> map; public String execute() throws Exception {
//list.clear();
System.out.println(list);
System.out.println(map);
return SUCCESS;
} public List getList() {
return list;
} public void setList(List list) {
this.list = list;
} public Map<String, String> getMap() {
return map;
} public void setMap(Map<String, String> map) {
this.map =
map;
}

}

2 struts.xml

<action name="Demo11Action" class="www.test.c_param.Demo11Action" method="execute">
<result name="success" type="dispatcher">/form4.jsp</result>
</action>

3 form4.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>struts2 获得参数方式-集合类型封装</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/Demo11Action" method="post">
list:<input type="text" name="list"><br/>
list:<input type="text" name="list[5]"><br/>
map:<input type="text" name="map['name']"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>

04-struts2获得参数的更多相关文章

  1. Struts 2.3.24源码解析+Struts2拦截参数,处理请求,返回到前台过程详析

    Struts2官网:http://struts.apache.org/ 目前最新版本:Struts 2.3.24 Struts1已经完全被淘汰了,而Struts2是借鉴了webwork的设计理念而设计 ...

  2. Struts2接受参数的几种类型和接受复杂类型参数(list<String>和list<Object>)

    Struts2接受参数的几种类型 大概有这几种类型: 1.使用Action的属性接受参数 在Action中加入成员变量,配置Getter和Setter方法,Getter而和Setter方法的名字和表单 ...

  3. Struts2请求参数校验

    校验的分类 客户端数据校验 和 服务器端数据校验 客户端数据校验 ,通过JavaScript 完成校验 (改善用户体验,使用户减少出错 ) 服务器数据校验 ,通过Java代码 完成校验 struts2 ...

  4. struts2接收参数——域模型、DTO

    在开始介绍域模型之前我们要明白一点,为什么通过域模型我们可以把参数这么方便的在后台接收. 那是因为 通过参数拦截器(params interceptor)自动的把前台传过来的参数给域对象(domain ...

  5. Struts2 请求参数接收

    在Struts2中提供了更为简单的参数请求与接收方法,可以直接在Action中定义属性:Struts2通过反射机制将参数反射到属性的set方法上实现参数的传递: GET方式传送参数 <strut ...

  6. Struts2请求参数合法性校验机制

    在Action中通过代码执行数据校验 请求参数的输入校验途径一般分两种:客户端校验 :通过JavaScript 完成 (jquery validation插件),目的:过滤正常用户的误操作. 服务器校 ...

  7. SpringMVC札集(04)——SpringMVC传递参数

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  8. Struts2获取参数的几种方式

    Struts2由于是一个贴心的框架,所以获取参数这种体力活,就无需再通过原生的request来getParameter了,有如下几种方式进行获取 1.Action中属性驱动,必须提供与form表单na ...

  9. struts2 复杂参数封装

    1.1.1    Struts2中封装复杂类型的数据: 封装到List集合: 页面: 商品名称:<input type="text" name="products[ ...

  10. Python进阶04 函数的参数对应

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们已经接触过函数(function)的参数(arguments)传递.当时我们根 ...

随机推荐

  1. MySQL联合索引运用-最左匹配原则

    前言 之前看了很多关于MySQL索引的文章也看了<高性能MySQL>这本书,自以为熟悉了MySQL索引使用原理,入职面试时和面试官交流,发现对复合索引的使用有些理解偏颇,发现自己的不足整理 ...

  2. ubuntu 14.04 x64下安装libreoffice

    LibreOffice是ubuntu 上的办公软件很多人都知道微软公司的的Word.Excel.PowerPoint和Outlook,但是很少有人知道LibreOffice. LibreOffice靠 ...

  3. 引用面试官文章 :如何准备Java初级和高级的技术面试

    本人最近几年一直在做java后端方面的技术面试官,而在最近两周,又密集了面试了一些java初级和高级开发的候选人,在面试过程中,我自认为比较慎重,遇到问题回答不好的候选人,我总会再三从不同方面提问,只 ...

  4. 加密模块(md5)

    一.md5加密 import hashlib s = ' print(s.encode()) m = hashlib.md5(s.encode())# 必须得传一个bytes类型的 print(m.h ...

  5. luoguP4213 [模板]杜教筛

    https://www.luogu.org/problemnew/show/P4213 同 bzoj3944 考虑用杜教筛求出莫比乌斯函数前缀和,第二问随便过,第一问用莫比乌斯反演来做,中间的整除分块 ...

  6. iOS导入c++语言的静态库之后编译不过,先要检查是否导入了libstdc++

    iOS项目中引入c++库,编译链接时报如下错: "std::string::_Rep::_M_destroy(std::allocator<char> const&)&q ...

  7. 在FC中如何获取fcdot文件

    在FlexiCapture中一些客户在问如何获取.fcdot文件(在测试序列号下或者没有测试模板的情况下) 第一步: 1.查看License Manager查看是否找到序列号 首先我们在开始菜单里面打 ...

  8. centos虚拟机设置静态ip

    1.虚拟机网络配置设置为桥接模式 2.设置配置文件: vi /etc/sysconfig/network-scripts/ifcfg-ens33 DEVICE=ens33 HWADDR=:0c::c3 ...

  9. 008 Android activity实现多个界面的相互跳转(主要利用Intent)

    1.activity介绍 一个activity就把他理解成一个页面 2.新建activity流程 如图所示在com.lucky.test06的目录下,右击new--->Activity---&g ...

  10. 在URL地址栏中显示ico

             <!-- 在URL地址栏中显示ico -->         <link Rel="SHORTCUT ICON" href="imag ...