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的方式,这种一般用于数据传递的参数和接收参数不一致的情况,会在后面再写出来。






 

原创粉丝点击