struts2中的ActionSupport类

struts2中的ActionSupport类

我们在使用struts2,写其中的Action类的时候,为了规范同时也为了方便使用更多已经声明或者写好了的方法,我们通常会继承ActionSupport类,其中默认使用execute作为执行方法,如果你写了自己的方法,就不用执行这个方法了,最后的return返回一个字符串,其实是返回一个页面,这个需要在struts2的配置文件中配置。

继承AcitionSupport后,通过一个属性声明,然后实现set,get方法,就可以获取前台传递过来的参数。ActionContext类似于request,客户端发送一个请求,当请求完毕后,ActionContext里的内容将被释放。
如果想用session也可以用下面的方式:
Map<String, Object> session = ActionContext.getContext().getSession();
session.put("userList", list);

注意默认的返回值里面在Struts中,它把一些常用的SUCCESS, NONE, ERROR, INPOUT, LOGIN 都定义了一遍。SUCCESS其实就是"success"。

1.首先来一个开始的网页,也就是开始访问的网页,点击下边的提交按钮,就去后台中的配置文件struts.xml中寻找一个action,名字叫sSHActionTest

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form action="sSHActionTest" ,method="post">
这个是开始网页</br><!-- 添加一个换行符 -->

		用户名:<input type="text" name="username" /><br>
		 密码:<input type="password" name="password" /><br>
		<input type="submit" value="登录"/>
</form>
</body>
</html>

2,写配置类

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package name="cqupt" extends="struts-default">
		<action name="sSHActionTest" class="cqupt.ssh.action.SSHActionTest">
			<result name="success2">/success2.jsp</result>
		</action>
	</package>
</struts>

3.写一个Action类,也就是点击提交按钮后,去struts.xml中找一个action name="sSHActionTest”对应的一个访问的action类

package cqupt.ssh.action;

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

public class SSHActionTest 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 execute() throws Exception {

		//获取ActionContext对象,同时把前端获取的东西放入到一个对象里面
		ActionContext context=ActionContext.getContext();
		context.put("username", username);
		context.put("password", password);
		
		
		return "success2";//然后要去struts.xml中配置一波,我们的success2对应的跳转页面
	}
}

4.来一个返回的页面,根据action中的return语句中的字符串success2,取配置文件中struts.xml中找这个字符串对应的跳转页面是success2.jsp.

这个返回页面是把最开始页面的表单填写的数据,拿过来的(中间显示表单数据被后台的action类取到,然后在action类中,再将数据存入到某一个域中,最后到成功跳转的页面取出这个域中的数据,同时展现出来。

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
这里是成功跳转后的网页:</br><!-- 添加了一个换行符 -->

用户名:  ${username} 
密码:   ${password }
</body>
</html>

5.最后是启动项目,然后访问页面:http://localhost:8080/SSHProject/login.jsp

最后是把表单上写的数据,通过struts这个框架,传递到另一个页面了

猜你喜欢

转载自blog.csdn.net/Handsome2013/article/details/86350541