您的位置:首页 > 编程语言 > Java开发

Struts 2 第7part 使用action属性接收参数

2012-12-29 22:27 417 查看
2012年12月29

Struts 2 第7part 使用action属性接收参数

今天这一part 比较简单,介绍的是使用action属性接收参数。这咋Struts里是如何实现的?

实例:Struts2_ActionAttrParamInput

struts.xml配置文件:

<struts>
<package name="user" extends="struts-default" namespace="/user">
<action name="user" class="com.wwj.struts2.user.action.UserAction">
<result name="add">/user_add_success.jsp</result>
<result name="delete">/user_delete_success.jsp</result>
</action>
</package>
</struts>

键入链接:http://localhost:8080/Struts2_ActionAttrParamInput/

显示index.jsp页面

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>

使用action属性接收参数
<a href="user/user!add?name=a&age=8">添加用户</a>
<br />

</body>
</html>





点击"添加用户"





点击“添加用户”连接后会去struts.xml中找到命名空间:user下的action:user,再找到相应的Action实现类,通过Action类参数name,age的Set方法来保存数据。就这样起到了接收参数的作用。

对应Action类实现:

package com.wwj.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);
System.out.println(age);
return "add";
}
public String delete() {
return "delete";
}
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;
}

}


本文出自 “一曲待续” 博客,请务必保留此出处http://wwj9520.blog.51cto.com/5769922/1104307
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: