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开 ...
随机推荐
- WordCount 3
学号:201631062130.201631062304 码云地址:https://gitee.com/xnsy/WordCountPlus 一.代码互审情况:在代码的互审过程中,在命令和路径没有没有 ...
- mongodb文件损坏的恢复--无可恢复数据
1.mongodb 启动异常error code 100,检查日志,数据文件损坏 2 检查collection-15-6548623434943640018.wt 可恢复数据,为空,不存在恢复的数据 ...
- netty(六) websocket开发应用
package com.lance.net.server.common; import java.net.InetSocketAddress; import org.springframework.s ...
- Sping Cloud hystrix.stream 自动发现-监控
相关组件安装脚本 [root@java_gateway4 java_tps]# cat cront_install.sh #!/bin/bashyum install jq -ymkdir /home ...
- 测验2: Python基础语法(上) (第4周)
快乐的数字 描述 编写一个算法来确定一个数字是否“快乐”. 快乐的数字按照如下方式确定:从一个正整数开始,用其每位数的平方之和取代该数,并重复这个过程,直到最后数字要么收敛等于1且一直等于1,要么将无 ...
- [SpringBoot]Web综合开发-笔记
Web开发 Json接口开发 @RestController 给类添加 @RestController 即可,默认类中的方法都会以 json 的格式返回. 自定义filter filter作用: 用于 ...
- js数组去除重复数据
一个有重复数据的数组,准备一个空数组,遍历有重复数据的数组同时用indexOf对比那个空数组判断是否有一样的,不一样的push进去空数组 let arr = dataInfo.map(item =&g ...
- Spring事务管理的四种方式(以银行转账为例)
Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败. 原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...
- thinkphp用ajax遇到的坑——ajax请求没有反应
view视图的 html 的 js 代码如下, $.ajax({ url:"test",//这里指向的就不再是页面了,而是一个方法. ...
- 五、secureCRT远程连接工具的使用
1.secureCRT实现远程传输文件到服务器机器 alt+p ,进入sftp模式,输入命令:put 文件所在的本机位置