1、以前Servlet用request对象里头的getParameter,request.getParameterMap方法获取数据
2、 Action没有request对象不能直接用request对象
一、使用ActionContext类获取
1、使用到的方法
- Map<String,Object>getParameter() 返回一个含有HttpServletRequest参数信
- static ActionContext getContext() 获取当前线程的ActionContext对像
(1)因为方法不是静态的,需要创建ActionContext类对象
(2)这个ActionContext对象不是new出来的
2、实现代码
- 先创建表单,提交到action里面
- 在action使用ActionContext获取数据
index.jsp表单页面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align="center">
<h1>Login</h1>
<form action="${pageContext.request.contextPath }/loginAction " method="post">
username:<input type="text" name="username"/><br><br>
password:<input type="password" name="password"/><br><br>
type:<input type="text" name="type"/><br><br>
<input type="submit" value="Login"/>
</form>
</div>
</body>
</html>
Action.java
package work.zhangdoudou;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Action extends ActionSupport{
@Override
public String execute() throws Exception {
//第一种使用ActionContext获取
//1、获取ActionContext对象分
ActionContext context = ActionContext.getContext();
//2、调用方法得到表单数据
//key是表单name属性value是值
Map<String, Object> map=context.getParameters();
Set<String> keys=map.keySet();
for(String key:keys){
//根据key得到valu
//数组形式:因为输入项里可能有复选框
Object obj[]=(Object[])map.get(key);
System.out.println(key+":"+Arrays.toString(obj));
}
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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="loginAction" class="work.zhangdoudou.Action" method="">
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
3、运行结果
image.png image.png二、使用ServletActionContext获取
1、使用的方法
- static HttpServletRequest() 获取Web应用的 HttpServletRequest对象
- static HttpServletResponse() 获取Web应用的 HttpServletResponse对象
- static ServletContext.getServletContext 获取Web应用的 ServletContext对象
- static PageContext.getPageContext 获取Web应用的 PageContext对象
(1)调用类里头的静态方法,得到request对象
2、实现代码
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div align="center">
<h1>Login</h1>
<form action="${pageContext.request.contextPath }/loginAction2 " method="post">
username:<input type="text" name="username"/><br><br>
password:<input type="password" name="password"/><br><br>
type:<input type="text" name="type"/><br><br>
<input type="submit" value="Login"/>
</form>
</div>
</body>
</html>
Action2.java
package work.zhangdoudou;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Action2 extends ActionSupport{
@Override
public String execute() throws Exception {
//第二种种使用ServletActionContext获取
//1、用ServletActionContext获取request
HttpServletRequest request=ServletActionContext.getRequest();
//2、调用request里面的方法得到结果
String username=request.getParameter("username");
String password=request.getParameter("password");
String type=request.getParameter("type");
System.out.println("username:"+username);
System.out.println("password:"+password);
System.out.println("type:"+type);
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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="loginAction2" class="work.zhangdoudou.Action2" method="">
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>struts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
3、运行结果
image.png image.png三、使用接口注入方式获取(了解)
1、实现的接口
- ServletRequestAware:实现该接口的action可以直接访问Web应用的HttpServletRequets实例
- ServletResponseAware:实现该接口的action可以直接访问Web应用的HttpServletResponse实例
- SessionAware:实现该接口的action可以直接访问Web应用的HttpSession实例
- ServletContextAware:实现该接口的action可以直接访问Web应用的ServletContext实例
(1)让action实现接口,为了得到request对象
2、实现接口类的代码Action3.java
package work.zhangdoudou;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
/*
* 接口注入方式
*/
public class Action3 extends ActionSupport implements ServletRequestAware{
//定义成员request
private HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest request) {
//获取到request
this.request=request;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return NONE;
}
}
网友评论