Struts中Action三种接收参数的方式?
前言:
前面已经有一篇随笔介绍了Struts2的大概原理。本文就Struts2中Action与jsp页面进行数据对接时介绍几种常见方法!
值栈ValueStack

3个Action
Action1
package com.gdufe.action; import com.opensymphony.xwork2.ActionSupport; /*
* Action接收参数之后通过set方法赋给普通变量age,name;
*/
public class UserAction1 extends ActionSupport{ private int age;
private String name; public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String execute(){ return SUCCESS;
} public String test(){
System.out.println(age +"|"+ name);
return SUCCESS;
}
}Action2
package com.gdufe.action; import com.gdufe.pojo.User;
import com.opensymphony.xwork2.ActionSupport; /*
* Action接收参数之后赋给引用对象“user”,内部是set方法赋值
*/
public class UserAction2 extends ActionSupport { private User user; public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} public String test(){
System.out.println(user.getName() + "|" + user.getAge());
return "success";
}
}Action3
package com.gdufe.action; import com.gdufe.pojo.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class UserAction3 extends ActionSupport implements ModelDriven<User> { private User user = new User(); public String test(){ System.out.println(user.getName() + "|" + user.getAge());
return "success";
} public User getModel() {
return user;
}
}2个页面
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head> <body>
    <h2>Action传值测试</h2>
        <a href="userAction1!test?age=8&name=admin">test1:Attribution</a> <br>
        <a href="userAction2!test?user.age=8&user.name=admin">test2:JavaBean</a> <br>
        <a href="userAction3!test?age=8&name=admin">test3:ModelDriven</a> <br>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head> <body> <h2>Action传值双击debug</h2>
<s:debug></s:debug>
<!-- debug重要的strut2标签调试工具 -->
</body>
</html>
1个struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- devMode设置为开发模式 -->
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default">
<!-- 注:因为Action采用DMI方式,故不需要指明method 以及 ‘result’ -->
<action name="userAction1" class="com.gdufe.action.UserAction1" >
<result>/success.jsp</result>
</action> <action name="userAction2" class="com.gdufe.action.UserAction2" >
<result>/success.jsp</result>
</action> <action name="userAction3" class="com.gdufe.action.UserAction3" >
<result>/success.jsp</result>
</action> </package> </struts>
运行结果:
对应Action1——------------------*-------------------------

对应Action2——------------------*-------------------------

对应Action3——------------------*-------------------------

注意:新手的话请勿按部就班,因为还有初始配置没有说明,比如jar包及web.xml配置。详细配置自己读manual帮助文档或者上网参考!
==============================
结语:近期在接手Web开发时,数据对接不熟练;鉴于此,才再次翻起struts2的一些基础知识加深理解。希望能给有打算从事Java的朋友些许借鉴!
Struts中Action三种接收参数的方式?的更多相关文章
- Struts2之Action三种接收参数形式与简单的表单验证
		
有了前几篇的基础,相信大家对于Struts2已经有了一个很不错的认识,本篇我将为大家介绍一些关于Action接收参数的三种形式,以及简单的表单验证实现,下面进入正题,首先我们一起先来了解一下最基本的A ...
 - 参数对象Struts2中Action的属性接收参数
		
题记:写这篇博客要主是加深自己对参数对象的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. Action中三种传递并接受参数: 1. 在Action添加成员属性接受参数 例如请求的 ...
 - struts用action的属性接收参数
		
新建一个javaweb项目 在项目中加入Struts.xml( 选中项目右键MyEclipse-->project facets-->Struts2-->finish) 在src项目 ...
 - C++中的三种继承方式
		
1,被忽略的细节: 1,冒号( :)表示继承关系,Parent 表示被继承的类,public 的意义是什么? class Parent { }; class Child : public Parent ...
 - 关于在Struts2的Action中使用domain模型接收参数的问题
		
最近在搭建一个最新的ssh2框架,今天在调试的时候,发现了一个以前一直没有注意过的问题,我在Action中使用域模型的方式去接收jsp画面中的参数的时候,发现参数总是接收不完,头一次遇到这种问题,现在 ...
 - Netty中的三种Reactor(反应堆)
		
目录: Reactor(反应堆)和Proactor(前摄器) <I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor> <[转]第8章 前摄器(Proa ...
 - js oop中的三种继承方法
		
JS OOP 中的三种继承方法: 很多读者关于js opp的继承比较模糊,本文总结了oop中的三种继承方法,以助于读者进行区分. <继承使用一个子类继承另一个父类,子类可以自动拥有父类的属性和方 ...
 - Vue中的三种Watcher
		
Vue中的三种Watcher Vue可以说存在三种watcher,第一种是在定义data函数时定义数据的render watcher:第二种是computed watcher,是computed函数在 ...
 - Java三大框架之——Hibernate中的三种数据持久状态和缓存机制
		
Hibernate中的三种状态 瞬时状态:刚创建的对象还没有被Session持久化.缓存中不存在这个对象的数据并且数据库中没有这个对象对应的数据为瞬时状态这个时候是没有OID. 持久状态:对象经过 ...
 
随机推荐
- poj1573 Robot Motion
			
Robot Motion Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12507 Accepted: 6070 Des ...
 - 用AutoCompleteTextView实现历史记录提示
			
自定义AutoCompleteTextView 博客分类: android进阶 android 网上找到的都是同ArrayAdapter一起使用的,有时候需要自定义风格,咋办?follow me! ...
 - HTML-学习笔记(文本格式化,引用,计算机代码)
			
HTML 可定义很多供格式化输出的元素,比如粗体和斜体字. <b>定义粗体字体 <p>这是一段<b>粗体字体</b>通过标签定义</p> & ...
 - Mysql备份系列(4)--lvm-snapshot备份mysql数据(全量+增量)操作记录
			
Mysql最常用的三种备份工具分别是mysqldump.Xtrabackup(innobackupex工具).lvm-snapshot快照.前面分别介绍了:Mysql备份系列(1)--备份方案总结性梳 ...
 - rpc使用举例
			
#server.py from SimpleXMLRPCServer import SimpleXMLRPCServer def add(x,y): return x+y server=SimpleX ...
 - html代码中显示系统时间
			
可以显示系统的静态时间和动态时间 1,静态时间 <script type="text/javascript"> var myDate = new Date(); doc ...
 - HTML <map> 设置图热点
			
需要在一张图片中,设置一个区域为热点就用到了<map> 定义一个客户端图像映射.图像映射(image-map)指带有可点击区域的一幅图像. <img src="planet ...
 - Shell脚本语法
			
索引 1. 第一行必须是"#!/bin/sh" 2. 注释:一行开头为 # 3. 定义变量 4. 逻辑符号 5. 接收参数 6. 控制流:if/then/elif/else/f ...
 - C#一元运算重载的深入理解
			
using System; using System.Diagnostics; using System.Text; using System.Collections; using System.Co ...
 - java的守护线程与非守护线程
			
最近重新研究Java基础知识,发现以前太多知识知识略略带过了,比较说Java的线程机制,在Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) ,(PS:以 ...