Struts之獲取表單資料

Struts之獲取表單資料

1.ActionContext獲取表單資料

form.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<form action="${pageContext.request.contextPath}/formAction.action" method="post" enctype="application/x-www-form-urlencoded">
使用者名稱:<input type="text" name="username"><br/>
密碼:<input type="text" name="password"><br/>
性別:<input type="radio" name="sex" value="man">男
<input type="radio" name="sex" value="woman">女<br>
愛好:<input type="checkbox" name="hobby" value="study">學習
<input type="checkbox" name="hobby" value="music">音樂
<input type="checkbox" name="hobby" value="sleep">睡覺<br/>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>

FormAction.java

package zh.action.demo;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 通過ActionContext獲取表單資料
* @author ZH
*
*/
public class FormAction extends ActionSupport {
public String params() {
// 通過ActionContext類的靜態方法來獲取ActionContext物件
ActionContext actionContext = ActionContext.getContext();
// 獲取所有的表單資料
Map<String, Object> parameters = actionContext.getParameters();
// 遍歷表單資料
Set<Entry<String, Object>> entrySet = parameters.entrySet();
for (Entry<String, Object> entry : entrySet) {
String key = entry.getKey();
Object[] array = (Object[]) entry.getValue();
System.out.println(key   "="   Arrays.toString(array));
}
return NONE;
}
}

struts.xml

<?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>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<package name="demo1" namespace="/" extends="struts-default">
<action name="formAction" class="zh.action.demo.FormAction" method="params"></action>
</package>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>	
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--  配置Struts的攔截器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

2.ServletActionContext獲取表單資料

ServletActionContext類的靜態方法如下:

package zh.action.demo;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 通過ServletActionContext獲取表單資料
* @author ZH
*/
public class FormAction extends ActionSupport {
public String params() {
// 獲取HttpServletRequest 物件
HttpServletRequest request = ServletActionContext.getRequest();
// 獲取表單資料
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String[] hobbyArray = request.getParameterValues("hobby");
System.out.println(username);
System.out.println(password);
System.out.println(sex);
System.out.println(Arrays.toString(hobbyArray));
return NONE;
}
}

3.介面注入方式獲取表單資料

package zh.action.demo;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* 通過實現ServletRequestAware介面,獲取表單資料
* 
* @author ZH
*/
public class FormAction extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
// 重寫ServletRequestAware介面中的方法
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public String params() {
// 獲取表單資料
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String[] hobbyArray = request.getParameterValues("hobby");
System.out.println(username);
System.out.println(password);
System.out.println(sex);
System.out.println(Arrays.toString(hobbyArray));
return NONE;
}
}