一、用Action属性

在action里定义要接收的参数,并提供相应的set和get方法。

如:

public class LoginAction extends ActionSupport {
private String username;
private String password; //对应的get set方法
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() {
// TODO Auto-generated method stub
System.out.println("username = "+username);
System.out.println("password = "+password);
return SUCCESS;
}
}
<form action="login" method="post">
用户名:<input type="text" name="username"><br/>
密 码:<input type="password" name="password"><br/>
<input type="submit" value="提交">
</form>

二、使用DomainModel

如果Action中属性过多,则可以Model保存为一个对象,并提供get和set

Model类

public class User {
private String username;
private String password; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

Action类

public class LoginAction extends ActionSupport {
private User user;
public String execute() {
// TODO Auto-generated method stub
System.out.println("username = "+user.getUsername());
System.out.println("password = "+user.getPassword());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}

JSP

<form action="login" method="post">
用户名:<input type="text" name="user.username"><br/>
密 码:<input type="password" name="user.password"><br/>
<input type="submit" value="提交">
</form>

三、使用ModelDriven(模型驱动)

使用模型驱动模式时,Acton必须实现ModelDriven接口,实现该接口则必须实现getModel()方法,该方法用于把Action和与之对应的Model实例关联起来。

Model类同DomainModel

Action类

public class LoginAction extends ActionSupport implements ModelDriven<User> {
private User user = new User(); //需实例化
public String execute() {
// TODO Auto-generated method stub
System.out.println("username = "+user.getUsername());
System.out.println("password = "+user.getPassword());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}

JSP

<form action="login" method="post">
<!-- 属性可以为 对象.XXX 也可以直接为XXX 则前提Action中要自己实例化Model对象 -->
用户名:<input type="text" name="user.username"><br/>
密 码:<input type="password" name="password"><br/>
<input type="submit" value="提交">
</form>

四、使用request接收参数

此方法和传统的JSP页面接收参数一样,用request.getParameter("")方法。

Struts2接收参数的几种方式的更多相关文章

  1. ssh框架总结之action接收参数的三种方式

    页面将参数传递给action的三种方式 一是通过属性传值: 将页面和action的的属性值保持一致,在action上写上该属性的set和get方法,这样在页面提交参数的时候,action就会调用set ...

  2. SpringBoot Controller接收参数的几种方式盘点

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:SpringBoot Controller接收参数的几种方式盘点: SpringBoot Controller接收参数的几种常用方式盘点 ...

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

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

  4. struts2接收参数的5种方法

    以下形式中最常用的是前两种 1. 使用Action的属性: 在action 里面定义要接收的参数,并提供相应的setter,getter,和提交参数的名称一致, 并不用做数据类型的转换相应提交方式可以 ...

  5. strut2接收参数的三种方式

    strut2接收参数有三种方式(普通属性\领域对象\模型驱动),分别对三种进行一个总结: 一.普通属性 Jsp代码 <body> <h1>普通属性</h1> < ...

  6. 前端ajax异步传值以及后端接收参数的几种方式

    原文参考 异步传值 前台往后台传值呢,有很多种方式,大家听我细细道来. 第一种呢,也是最简单的一种,通过get提交方式,将参数在链接中以问号的形式进行传递. // 前台传值方法 // 触发该方法调用a ...

  7. spring-boot-route(一)Controller接收参数的几种方式

    Controller接收参数的常用方式总体可以分为三类.第一类是Get请求通过拼接url进行传递,第二类是Post请求通过请求体进行传递,第三类是通过请求头部进行参数传递. 1 @PathVariab ...

  8. PHP接收参数的几种方式

    PHP5在默认的情况下接收参数是需要使用 $_GET['value']; $_POST['value']; 还可以在PHP.ini 文件中的  将register_globals = Off  改re ...

  9. ASP.NET MVC post请求接收参数的三种方式

    1.在控制器中建立一个PostDemo方法,建立视图创建一个表单 <h2>PostDemo</h2> name的值:@ViewBag.name <br /> nam ...

随机推荐

  1. JavaMail API 1.4.7邮件发送

    下载oracle javaMail API: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive- ...

  2. 330. Patching Array--Avota

    问题描述: Given a sorted positive integer array nums and an integer n, add/patch elements to the array s ...

  3. HDU5311 Hidden String

    Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a str ...

  4. SGU 119.Magic pairs

    题意: 对于给出的一个整数N,和一对(A0,B0) 找到所有的整数对(A,B)满足 : 对于任意 X,Y 当 A0 * X + B0 * Y 能被 N 整除时 A * X + B * Y 也能被 N ...

  5. 4种检测是否支持HTML5的方法,你知道几个?

    4种检测是否支持HTML5的方法,你知道几个? 1,检查特定的属性是否存在于全局的对象里面,比如说window或navigator. 比如geolocation,它是HTML5新加支持的新特性:它是由 ...

  6. South入门教程

    对于django自带的syncdb同步models和数据库的缺陷,所以我选择第三方的工具South. 1.安装South:pip install South 2.South设置: 把 south 加到 ...

  7. bzoj2215: [Poi2011]Conspiracy

    Description Byteotia的领土被占领了,国王Byteasar正在打算组织秘密抵抗运动.国王需要选一些人来进行这场运动,而这些人被分为两部分:一部分成为同谋者活动在被占领区域,另一部分是 ...

  8. 酷炫地给py代码标上行数

    Python IDLE是没有显示行号的功能的,今天学了一个方式可以酷炫地给自己的代码加上行号,该方法直接修改代码,慎用哦!代码如下: import fileinput for line in file ...

  9. mapreduce (七) 几个实例

    http://hi.baidu.com/hzd2712/item/d2465ae65270ab3e4cdcaf55 MapReduce几个典型的例子 在Google的<MapReduce: Si ...

  10. Python 日期格式化 及 schwartzian排序

    __author__ = 'root' import datetime import time import copy # 12/Dec/2012:23:59:50 # 12/Sep/2012:23: ...