Struts2慢慢学之五----参数传递
来源:互联网 发布:数据库作图软件 编辑:程序博客网 时间:2024/05/22 10:45
参数传递即将参数传输到程序后台中,后台可能做一些处理,然后再将内容存入数据库之类嗒!
参数传递的方法较多,一一说明如下。
1、Action中直接参数法
有如下的index.jsp文件
<?xml version="1.0" encoding="GB18030" ?><%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%><% String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030" /><base href="<%=basePath %>"/><title>Insert title here</title></head><body>使用action属性接收参数<a href="user/user!add?name=a&age=8">添加用户</a></body></html>
对于其中的<a></a>来说,传递两个参数至程序,一个是name,一个是age,在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> <constant name="struts.devMode" value="true" /> <package name="user" extends="struts-default" namespace="/user"> <action name="user" class="com.bjsxt.struts2.user.action.UserAction"> <result>/user_add_success.jsp</result> </action> </package></struts>
这时我们的UserAction该如何写呢?范例如下:
package com.bjsxt.struts2.user.action;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {private String name;private int age;public String add() {System.out.println("name=" + name);System.out.println("age=" + age);return SUCCESS;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
很简单,就是定义了两个属性,注:这两个属性的set和get方法必须要写,可以使用eclipse的快速生成方式,非常简单。这样上述程序在运行时就会打印出所要的结果
name=a和age=8。
有说明如下:第一,struts2会自动进行参数传递,这个过程无需我们参与;第二,struts传递参数时针对的是set和get方法,而不是name和age属性,也就是说,假如我们修改其中的name为其他名称,如username,但是方法仍然是setName和getName的话,对于整个功能的实现来说没有任何区别,只是有点别扭而已;第三,也是最重要的一点,就是假如有很多的属性,这样的话我们就需要非常多的set和get方法,这是非常不方便的,因此引伸出了下面这种方式。
2、Action添加类对象法
这个时候我们1中的属性都归于一个类中,如User
package com.bjsxt.struts2.user.model;public class User {private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
这样在Action类中的写法就变得简单了很多
package com.bjsxt.struts2.user.action;import com.bjsxt.struts2.user.model.User;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport {private User user;public String add() {System.out.println("name=" + user.getName());System.out.println("age=" + user.getAge());return SUCCESS;}public User getUser() {return user;}public void setUser(User user) {this.user = user;}}
注:此时我们不需要自己手动生成一个User对象,这个过程是由Struts2自动完成的。
并且此时的url也需要作下修改,即index中的<a></a>标签作下修改:
<?xml version="1.0" encoding="GB18030" ?><%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%><% String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030" /><base href="<%=basePath %>"/><title>Insert title here</title></head><body> 使用Domain Model接收参数<a href="user/user!add?user.name=a&user.age=8">添加用户</a></body></html>
修改成上述18行部分。
还有一种是DTO的方式,这种一般用于数据传递的参数和接收参数不一致的情况,会在后面再写出来。
- Struts2慢慢学之五----参数传递
- Struts2慢慢学之五(接上)----参数传递方法三
- struts2系列五之可以传递参数
- Struts2慢慢学之二-----路径问题解决
- Struts2慢慢学之三------DMI
- Struts2慢慢学之四----通配符
- Struts2慢慢学之八----Result
- Struts2慢慢学之一
- Struts2慢慢学之六----简单的数据校验
- Struts2慢慢学之七----访问Web元素
- Struts2慢慢学之九----Result其他内容
- struts2之jsp参数传递
- struts2中Action参数传递之DTO
- 慢慢学,慢慢炼
- Struts2中的参数传递
- Struts2中的参数传递
- Struts2参数传递
- Struts2 配置文件传递参数
- 使用PNN进行数据分类
- 蓝牙技术研究
- Wireshark
- Android 指定Toast信息显示的位置并使用Toast显示其他View
- [新闻] 20120504,微软五月份安全补丁提前通知
- Struts2慢慢学之五----参数传递
- 【JAVA】之【string转time方法】
- Android 实现书籍翻页效果
- 【Tiny6410 And Linux】—(1.3)—bus 设备模型——原理
- Access SQL 函数
- Android内存优化的两个类:SoftReference 和 WeakReference
- C#反射实例
- Base64
- p_department