第一种参数获取方式:

编写一个前端页面,提交表单,做示例:

    <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:获取参数的更多相关文章

  1. j2ee开发之struts2框架学习笔记

    Struts2框架技术重点笔记 1.Struts2 是在webwork基础上发展而来. 2.Struts2 不依赖struts API和 servlet API 3.Struts2提供了拦截器,表现层 ...

  2. Struts2框架学习笔记--strtus2初识

    struts2概述: 1.struts2框架应用于javaEE三层结构中的Web层框架 2.struts2框架是在struts1和webwork基础之上发展的全新框架(脱胎换骨 ,用法完全不一样)ps ...

  3. struts2框架学习笔记3:获取servletAPI

    Struts2存在一个对象ActionContext(本质是Map),可以获得原生的request,response,ServletContext 还可以获得四大域对象(Map),以及param参数( ...

  4. struts2学习笔记 day02 获取参数 访问ServletAPI 结果类型

  5. Struts2框架学习笔记1

    1,框架概述 1.1,什么是框架(了解) 将一些重复性的代码进行封装,简化程序员的编程操作,可以使得程序员在编码中把更多的精力放到业务需求的分析和理解上面,相当于一个半成品软件. 1.2,三大框架(掌 ...

  6. 02 Struts2框架----学习笔记2(了解一下,已过时)

    1.*号通配符优化struts.xml代码 创建一个UserAction的动作类 package action; import com.opensymphony.xwork2.ActionSuppor ...

  7. struts2框架学习笔记5:OGNL表达式

    OGNL取值范围分两部分,root.Context两部分 可以放置任何对象作为ROOT,CONTEXT中必须是Map键值对 示例: 准备工作: public void fun1() throws Ex ...

  8. struts2框架学习笔记1:搭建测试

    Servlet是线程不安全的,Struts1是基于Servlet的框架 而Struts2是基于Filter的框架,解决了线程安全问题 因此Struts1和Struts2基本没有关系,只是创造者取名问题 ...

  9. struts2框架学习笔记7:struts2标签

    三大标签: 1.JSP:脚本,为了替代servlet,已过时 2.JSTL:标准标签库(core.format.sql.xml),还未淘汰的只有core库 3.Struts2标签库:由Struts2开 ...

随机推荐

  1. json 异常

    com.google.gson.JsonSyntaxException: 1530842820000 1530842820000 是服务器直接返回的Date值由 Gson 解析后出来的值. 后台发出: ...

  2. MySQL InnoDB内存压力判断以及存在的疑问

    本文出处:http://www.cnblogs.com/wy123/p/7259866.html(保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错误 ...

  3. ps命令详解

    1.简介: ps 命令有两种不同的语法风格 —— BSD 与 UNIX 两种风格.新手常常对这两种形式产生误解,因此我们有必要在这里作一个简单的说明: ps aux 与 ps -aux 是不同的,例如 ...

  4. EOS 权限

    [EOS权限] 1.查看权限 cleos get account $(Account_Name) 2.使用 cleos set account permission 命令来修改权限 可以看到,owne ...

  5. toastr简单用法及修改垂直居中

    toastr是一个基于Jquery简单.漂亮的消息提示插件,使用简单.方便,可以根据设置的超时时间自动消失. 1.使用很简单,首选引入toastr的js.css文件html <script sr ...

  6. 【python深入】获取对象类型及属性

    在python中,查看当前的对象所能够调用的所有方法? 查看类型可以通过type,也可以通过isinstance方法,查看属性可以通过dir() 下面是对type的介绍: ————>基本类型的判 ...

  7. 【转载】重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor ...

  8. 2017-2018-2 20165315 实验四《Android程序设计》实验报告

    2017-2018-2 20165315 实验四<Android程序设计>实验报告 第24章:初识Android Android Studio项目的目录树 1 build:该目录包含了自动 ...

  9. node.js中express框架的基本使用

    express是一个基于node.js平台的,快速,开放,极简的web开发框架. 一.安装 express npm install express --save 二.简单使用 express //引入 ...

  10. MFC里面解析json文件格式

    CString strTemp; //CString ->string; string stringMsg = (LPCSTR)(CStringA)strTemp; //string -> ...