struts2框架学习笔记4:获取参数
第一种参数获取方式:
编写一个前端页面,提交表单,做示例:
<form action="${pageContext.request.contextPath}/Demo1Action">
用户名:<input type="text" name="name" /><br>
年龄:<input type="text" name="age" /><br>
生日:<input type="text" name="birthday" /><br>
<input type="submit" value="提交" />
</form>
每次访问Action都会创建一个新的实例(线程安全):
package param; import java.util.Date; import com.opensymphony.xwork2.ActionSupport;
//第一种方式
public class Demo1Action extends ActionSupport { private String name;
private Integer age;
private Date birthday; public String execute() throws Exception{
System.out.println("name参数值:"+name+",age参数值:"+age+",生日:"+birthday);
return SUCCESS;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} 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;
} }
第二种方式获取参数:
封装一个实体类:
package domain;
import java.util.Date;
public class User {
private String name;
private Integer age;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
}
}
表单要修改下:
<form action="${pageContext.request.contextPath}/Demo2Action">
用户名:<input type="text" name="user.name" /><br>
年龄:<input type="text" name="user.age" /><br>
生日:<input type="text" name="user.birthday" /><br>
<input type="submit" value="提交" />
</form>
获取参数:
package param; import com.opensymphony.xwork2.ActionSupport; import domain.User; //struts2如何获得参数-方式2
public class Demo2Action 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;
} }
第三种方式获取参数:
模型驱动:
前端代码:
<form action="${pageContext.request.contextPath}/Demo3Action">
用户名:<input type="text" name="name" /><br>
年龄:<input type="text" name="age" /><br>
生日:<input type="text" name="birthday" /><br>
<input type="submit" value="提交" />
</form>
获取参数:
package param; import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; import domain.User; public class Demo3Action extends ActionSupport implements ModelDriven<User> { private User user = new User(); public String execute() throws Exception { System.out.println(user); return SUCCESS;
} @Override
public User getModel() {
return user;
} }
第四种获取参数方式:
集合类型封装:
前端表单:
<form action="${pageContext.request.contextPath}/Demo4Action" method="post" >
list:<input type="text" name="list" /><br>
list:<input type="text" name="list[3]" /><br>
map:<input type="text" name="map['haha']" /><br>
<input type="submit" value="提交" />
</form>
获取参数:
package param; import java.util.List;
import java.util.Map; import com.opensymphony.xwork2.ActionSupport; //struts2 封装集合类型参数
public class Demo4Action extends ActionSupport {
//list
private List<String> list;
//Map
private Map<String,String> map; public String execute() throws Exception { System.out.println("list:"+list);
System.out.println("map:"+map); return SUCCESS;
} public List<String> getList() {
return list;
} public void setList(List<String> list) {
this.list = list;
} public Map<String, String> getMap() {
return map;
} public void setMap(Map<String, String> map) {
this.map = map;
} }
这里获得的参数直接封装到集合中
struts2框架学习笔记4:获取参数的更多相关文章
- j2ee开发之struts2框架学习笔记
Struts2框架技术重点笔记 1.Struts2 是在webwork基础上发展而来. 2.Struts2 不依赖struts API和 servlet API 3.Struts2提供了拦截器,表现层 ...
- Struts2框架学习笔记--strtus2初识
struts2概述: 1.struts2框架应用于javaEE三层结构中的Web层框架 2.struts2框架是在struts1和webwork基础之上发展的全新框架(脱胎换骨 ,用法完全不一样)ps ...
- struts2框架学习笔记3:获取servletAPI
Struts2存在一个对象ActionContext(本质是Map),可以获得原生的request,response,ServletContext 还可以获得四大域对象(Map),以及param参数( ...
- struts2学习笔记 day02 获取参数 访问ServletAPI 结果类型
- Struts2框架学习笔记1
1,框架概述 1.1,什么是框架(了解) 将一些重复性的代码进行封装,简化程序员的编程操作,可以使得程序员在编码中把更多的精力放到业务需求的分析和理解上面,相当于一个半成品软件. 1.2,三大框架(掌 ...
- 02 Struts2框架----学习笔记2(了解一下,已过时)
1.*号通配符优化struts.xml代码 创建一个UserAction的动作类 package action; import com.opensymphony.xwork2.ActionSuppor ...
- struts2框架学习笔记5:OGNL表达式
OGNL取值范围分两部分,root.Context两部分 可以放置任何对象作为ROOT,CONTEXT中必须是Map键值对 示例: 准备工作: public void fun1() throws Ex ...
- struts2框架学习笔记1:搭建测试
Servlet是线程不安全的,Struts1是基于Servlet的框架 而Struts2是基于Filter的框架,解决了线程安全问题 因此Struts1和Struts2基本没有关系,只是创造者取名问题 ...
- struts2框架学习笔记7:struts2标签
三大标签: 1.JSP:脚本,为了替代servlet,已过时 2.JSTL:标准标签库(core.format.sql.xml),还未淘汰的只有core库 3.Struts2标签库:由Struts2开 ...
随机推荐
- 尚硅谷springboot学习33-整合mybatis
引入mybatis依赖(还要引入mysql和jdbc依赖) <dependency> <groupId>org.mybatis.spring.boot</groupId& ...
- 通过adb启动app应用
由于某些原因,我需要自动启动雷电模拟器里面的一个应用.(利用Windows任务计划) 怎么自启动雷电模拟器就不用说了,很简单. 自启动app我倒是不熟悉,我没用安卓方面的知识.再官网论坛上面查到了相关 ...
- .do的消除
其实就是在web.xml中去掉.do即可 那里有拦截器作用,什么样的文件可以进入前端控制器1
- 关于http以及aphace配置https
我是通过腾讯云配置的ssl. 网站:www.xian029.cn 免费申请,然后通过phpstudy 来配置的. 密码学: 研究密码编码与解码的学科,可以分为编码学和破译学. HTTPS ...
- typedef typename
所以根据上述两条分析, typedef typename RefBase::weakref_type weakref_type; 语句的真是面目是: typedef创建了存在类型的别名,而typen ...
- DOM 节点node
DOM可以将任何HTML或XML文档描绘成一个有多层节点构成的结构,即在HTML中所有内容都是节点.文档节点是每个文档的根节点,文档节点有一个子节点,称为文档元素.每个文档只能有一个文档元素.在HTM ...
- jQueryEasyUI学习笔记
data-options 是jQuery Easyui的一个特殊属性.通过这个属性,我们可以对easyui组件的实例化可以完全写入到html中 data-options="region:'w ...
- 168. Excel Sheet Column Title (Math)
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- TPL DataFlow初探(二)
上一篇简单的介绍了TDF提供的一些Block,通过对这些Block配置和组合,可以满足很多的数据处理的场景.这一篇将继续介绍与这些Block配置的相关类,和挖掘一些高级功能. 在一些Block的构造函 ...
- 高级编程T-SQL函数
--字符串函数--1.LEN:返回一个字符串的字符数select LEN('中国'),LEN('abc123!')select LEN('abc '+'1'),LEN(' abc')--2.DataL ...