RequestDemo1.java
package com.cherry.struts2.demo1;
import java.util.Arrays;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 访问Servlet的API方式一:完全解耦合的方式
* ps:这种方式只能获得代表request、session、application的数据的map集合,不能操作这些对象本身的方法
* @author zhang
*
*/
public class RequestDemo1 extends ActionSupport{
@Override
public String execute() throws Exception {
//一:接收参数:
//利用Struts2中的对象ActionContext对象
ActionContext context=ActionContext.getContext();
//调用ActionText中的方法
//类似于request.getParameterMap();
Map<String, Object> map = context.getParameters();
for (String key : map.keySet()) {
String[] value=(String[])map.get(key);
System.out.println(key+" "+Arrays.toString(value));
}
//二:向域对象中存入数据
context.put("reqName", "reqValue");//相当于request.setAttribute();
context.getSession().put("sessionName", "sessionValue");
context.getApplication().put("appName", "appValue");
return SUCCESS;
}
}
RequestDemo2.java
package com.cherry.struts2.demo1;
import java.util.Arrays;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 访问Servlet的API的方式二:原生的方式
* ps:这种方式可以操作域对象的数据,同时也可以获得对象的方法
* @author zhang
*
*/
public class RequestDemo2 extends ActionSupport {
@Override
public String execute() throws Exception {
//一:接收数据
//直接获得request对象,通过ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest();
Map<String, String[]> map = request.getParameterMap();
for (String key : map.keySet()) {
String[] values=map.get(key);
System.out.println(key+" "+Arrays.toString(values));
}
//二:向域对象中保存数据
//向request中保存数据
request.setAttribute("reqName", "reqValue");
//向session中保存数据
request.getSession().setAttribute("sessionName", "sessionValue");
//向application中保存数据
ServletActionContext.getServletContext().setAttribute("appName", "appValue");
return SUCCESS;
}
}
RequestDemo3.java
package com.cherry.struts2.demo1;
import java.util.Arrays;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* 访问Servlet的API的方式三:接口注入的方式
* @author zhang
*
*/
public class RequestDemo3 extends ActionSupport implements ServletRequestAware,ServletContextAware{
private HttpServletRequest request;
private ServletContext context;
@Override
public String execute() throws Exception {
//一:接收参数
//通过接口注入的方式获得request对象
Map<String, String[]> map = request.getParameterMap();
for (String key : map.keySet()) {
String[] values=map.get(key);
System.out.println(key+" "+Arrays.toString(values));
}
//二:向域对象中保存数据
//向request域中保存数据
request.setAttribute("reqName", "reqValue");
//向session中保存数据
request.getSession().setAttribute("sessionName", "sessionValue");
//向application中保存数据
context.setAttribute("appName", "appValue");
return super.execute();
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
@Override
public void setServletContext(ServletContext context) {
this.context=context;
}
}
struts_demo1.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>
<package name="demo1" extends="struts-default" namespace="/">
<action name="requestDemo1" class="com.cherry.struts2.demo1.RequestDemo1">
<result name="success">/demo1/demo2.jsp</result>
</action>
<action name="requestDemo2" class="com.cherry.struts2.demo1.RequestDemo2">
<result name="success">/demo1/demo2.jsp</result>
</action>
<action name="requestDemo3" class="com.cherry.struts2.demo1.RequestDemo3">
<result name="success">/demo1/demo2.jsp</result>
</action>
</package>
</struts>
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>
<!-- 配置Struts2的常量 -->
<constant name="struts.action.extendsion" value="action"></constant>
<include file="com/cherry/struts2/demo1/struts_demo1.xml"></include>
</struts>
demo1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Struts2访问Servlet的API</h1>
<h3>方式一:完全解耦合的方式</h3>
<form action="${pageContext.request.contextPath }/requestDemo1.action" method="post">
姓名:<input type="text" name="name"/><br>
密码:<input type="password" name="password"/><br>
<input type="submit" value="提交">
</form>
<h3>方式二:使用原生的方式访问</h3>
<form action="${pageContext.request.contextPath }/requestDemo2.action" method="post">
姓名:<input type="text" name="name"/><br>
密码:<input type="password" name="password"/><br>
<input type="submit" value="提交">
</form>
<h3>方式三:接口注入的方式</h3>
<form action="${pageContext.request.contextPath }/requestDemo3.action" method="post">
姓名:<input type="text" name="name"/><br>
密码:<input type="password" name="password"/><br>
<input type="submit" value="提交">
</form>
</body>
</html>
demo2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>显示数据</h1>
${reqName }
${sessionName }
${appName }
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>struts2_day02</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>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>