1. 程式人生 > >struts2中的ActionSupport類

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這個框架,傳遞到另一個頁面了