码迷,mamicode.com
首页 > 其他好文 > 详细

struts开发<struts中的参数传递.三>

时间:2014-07-19 02:17:06      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:风飞雪未扬   struts传递参数   数据交互   

不说废话,直接上干货



1.通过set和get传递参数

增加username 和password两个属性并增加set和get方法

<span style="font-size:18px;">package fzl.user.struts.demo;


import com.opensymphony.xwork2.ActionSupport;


public class UserAction extends ActionSupport {
<span style="white-space:pre">	</span>private String  username;
<span style="white-space:pre">	</span>private String password;
public String getUsername() {
<span style="white-space:pre">		</span>return username;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public void setUsername(String username) {
<span style="white-space:pre">		</span>this.username = username;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public String getPassword() {
<span style="white-space:pre">		</span>return password;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public void setPassword(String password) {
<span style="white-space:pre">		</span>this.password = password;
<span style="white-space:pre">	</span>}
public String list(){
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>System.out.println("list");
<span style="white-space:pre">	</span>return "success";
}
public String input(){
<span style="white-space:pre">	</span>System.out.println("input");
<span style="white-space:pre">	</span>return "success";
}<span style="white-space:pre">	</span>


public String add(){
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>System.out.println("add");
return "success";
}}
</span>

在list使用EL表达式和struts标签调用

<pre name="code" class="html"><span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
通过EL访问
${username }-->${password }
<h1>------------------list -----------------</h1>
通过struts标签访问
<s:property value="username"/>--><s:property value="password"/>
</body>
</html></span>

在浏览器输入http://localhost:9000/strustDemo1/User_list?username=fzl&password=123 传入参数

bubuko.com,布布扣


第二种方法,通过Actioncontext完成

<span style="font-size:18px;">package fzl.user.struts.demo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	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;
	}
public String list(){
	ActionContext.getContext().put("username", "flyou");
	ActionContext.getContext().put("password", "553274238");
	System.out.println("list");
	return "success";
}
public String input(){
	System.out.println("input");
	return "success";
}	

public String add(){
	
	System.out.println("add");
return "success";
}}</span>
list文件不用修改

bubuko.com,布布扣




第三种方法,通过servletAPI传值



<span style="font-size:18px;">package fzl.user.struts.demo;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	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;
	}
public String list(){
	//ActionContext.getContext().put("username", "flyou");
	//ActionContext.getContext().put("password", "553274238");
	ServletActionContext.getRequest().setAttribute("username", "flyou");
	ServletActionContext.getRequest().setAttribute("password", "553274238");
	System.out.println("list");
	return "success";
}
public String input(){
	System.out.println("input");
	return "success";
}	

public String add(){
	
	System.out.println("add");
return "success";
}}</span>

list文件

<span style="font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
    <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
通过EL访问
${username }-->${password }
<h1>------------------list -----------------</h1>
通过struts标签访问
<span style="background-color: rgb(204, 0, 0);"><s:property value="#request.username"/>--><s:property value="#request.password"/></span>
</body>
</html></span>
bubuko.com,布布扣

获取的三种方式

1.通过seter和geter方法接受并传递

2.通过ActionContext.getContext().put("username", "flyou");传递参数

3.通过 ServletActionContext.getRequest.setAttribute("","")传值


struts开发<struts中的参数传递.三>

标签:风飞雪未扬   struts传递参数   数据交互   

原文地址:http://blog.csdn.net/u013616976/article/details/37941903

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!