Struts2(八)访问Servlet API
一、Struts2中的Servlet API
1.1、struts2的Action实现了MVC中C层的作用
针对请求用户显示不同的信息
登录后段保存用户信息 ----session
保存当前在线人数等功能---application
1.2、传统的Servlet API类型
HttpServletRequest
HttpSession
ServletContext
1.3、Struts2中将传统的Servlet API类型被处理成Map类型
访问更方便
不依赖传统Servlet API 类型--解耦合
二、ActionContext
所在包com.opensymphony.xwork2
ActionContext被称为Action上下文或者Action环境
ActionContext,提供每个Action运行时与之相关的所有信息
访问Servlet API
ActionContext类提供getContext()方法获得实例
通过ActionContext类的实例可获得Map类型的请求,会话等
通过Put()和Get()方法在Map类型的对象中存取数据
二、简单例子
web.xml中添加
<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>
login.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="user01.action" method="post">
<table>
<tr>
<td>用户名:<input name="username"/></td>
</tr>
<tr>
<td>用户名:<input type="password" name="password"/></td>
</tr>
<tr>
<td><input type="submit" value="提交"/><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>
loginSuccess.jsp
<%@ 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=UTF-8">
<title>登录成功页面</title>
</head>
<body>
<h1>jsp方式取值:</h1>
<%=request.getAttribute("linkaddress") %><br/>
<%=session.getAttribute("uname") %><br/>
<%=application.getAttribute("online") %><br/>
<h1>EL表达式取值:</h1>
${request.linkaddress }<br/>
${session.uname }<br/>
${application.online }<br/>
<h1>struts取值:</h1>
<!-- 推荐使用这种 -->
<s:property value="#request.linkaddress"/><br/>
<!-- 使用attr取值不推荐-->
<s:property value="#attr.linkaddress"/><br/>
<s:property value="#session.uname"/><br/>
<s:property value="#attr.uname"/><br/>
<s:property value="#application.online"/><br/>
<s:property value="#attr.online"/><br/>
<s:debug/>
</body>
</html>
UserAction3.java
package com.pb.web.action; import java.util.Map; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport; public class UserAction3 extends ActionSupport { private String username;
private String password;
private Map<String , Object> request;
private Map<String , Object> session;
private Map<String , Object> application; public String login(){
ActionContext ac=ActionContext.getContext();
request=(Map<String, Object>) ac.get("request");
session=ac.getSession();
application=ac.getApplication();
if(this.getUsername().equals("accp")&&this.getPassword().equals("accp")){
request.put("linkaddress", "这次的登录地点:SZ");
session.put("uname", username);
application.put("online", "当前在线人数:xxxx");
System.out.println(request.get("linkaddress"));
System.out.println(session.get("uname"));
System.out.println(application.get("online")); return SUCCESS;
}
return INPUT; } 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 Map<String, Object> getRequest() {
return request;
} public void setRequest(Map<String, Object> request) {
this.request = request;
} public Map<String, Object> getSession() {
return session;
} public void setSession(Map<String, Object> session) {
this.session = session;
} public Map<String, Object> getApplication() {
return application;
} public void setApplication(Map<String, Object> application) {
this.application = application;
} }
struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="user01" class="com.pb.web.action.UserAction3" method="login">
<result name="success">/loginSuccess.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
三、IOC方式获取Servlet API对象
更改以上的代码
login.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
<!-- <form action="user01.action" method="post"> -->
<form action="user02.action" method="post">
<table>
<tr>
<td>用户名:<input name="username"/></td>
</tr>
<tr>
<td>用户名:<input type="password" name="password"/></td>
</tr>
<tr>
<td><input type="submit" value="提交"/><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
<s:property value="#session.msg"/>
</body>
</html>
loginSuccess.jsp
<%@ 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=UTF-8">
<title>登录成功页面</title>
</head>
<body>
<h1>jsp方式取值:</h1>
<%=request.getAttribute("linkaddress") %><br/>
<%=session.getAttribute("uname") %><br/>
<%=application.getAttribute("online") %><br/>
<h1>EL表达式取值:</h1>
${request.linkaddress }<br/>
${session.uname }<br/>
${application.online }<br/>
<h1>struts取值:</h1>
<!-- 推荐使用这种 -->
<s:property value="#request.linkaddress"/><br/>
<!-- 使用attr取值不推荐-->
<s:property value="#attr.linkaddress"/><br/>
<s:property value="#session.uname"/><br/>
<s:property value="#attr.uname"/><br/>
<s:property value="#application.online"/><br/>
<s:property value="#attr.online"/><br/>
<s:debug/>
</body>
</html>
UserAction2.java
package com.pb.web.action; import java.util.Map; import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport; public class UserAction2 extends ActionSupport implements RequestAware,
SessionAware, ApplicationAware { private static final long serialVersionUID = 1L;
//属性要有getter、setter方法
private String username;
private String password;
//request,session,application不需要getter、setter方法
private Map<String , Object> request;
private Map<String , Object> session;
private Map<String , Object> application; public String login(){ if(this.getUsername().equals("accp")&&this.getPassword().equals("accp")){
request.put("linkaddress", "这次的登录地点:SZ");
session.put("uname", username);
application.put("online", "当前在线人数:xxxx");
System.out.println(request.get("linkaddress"));
System.out.println(session.get("uname"));
System.out.println(application.get("online")); return SUCCESS;
}
session.put("msg", "用户名或者密码不正确!");
return INPUT; } //使用struts2提供接口来生成application,session,request对象,
@Override
public void setApplication(Map<String, Object> application) {
this.application=application;
} @Override
public void setSession(Map<String, Object> session) {
this.session=session;
} @Override
public void setRequest(Map<String, Object> request) {
this.request=request;
} 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;
} }
struts.xml
<action name="user02" class="com.pb.web.action.UserAction2" method="login">
<result name="success" type="dispatcher">/loginSuccess.jsp</result>
<result name="input" type="redirect">/login.jsp</result>
</action>
使用传统的方式获取Servlet API
实现ServletRequestAware接口
package com.pb.web.action; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionSupport; public class UserAction04 extends ActionSupport implements ServletRequestAware,
ServletContextAware { private String username;
private String password;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application; public String login() {
// 获取对象
session = request.getSession();
// 保存数据
request.setAttribute("loginAddress", "您本次登录地点是:北京");
session.setAttribute("username", username);
application.setAttribute("online", "当前在线人数:XXX");
// 获取数据
System.out.println(request.getAttribute("loginAddress"));
System.out.println(session.getAttribute("username"));
System.out.println(application.getAttribute("online"));
return SUCCESS;
} 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;
} @Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
} @Override
public void setServletContext(ServletContext application) {
this.application = application; } }
或者使用时直接获取对象
package com.pb.web.action; import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport; public class UserAction03 extends ActionSupport { private String username;
private String password;
private HttpServletRequest request;
private HttpSession session;
private ServletContext application; public String login() {
// 获取对象
request = ServletActionContext.getRequest();
session = request.getSession();
application = ServletActionContext.getServletContext();
// 保存数据
request.setAttribute("loginAddress", "您本次登录地点是:北京");
session.setAttribute("username",username);
application.setAttribute("online","当前在线人数:XXX");
// 获取数据
System.out.println(request.getAttribute("loginAddress"));
System.out.println(session.getAttribute("username"));
System.out.println(application.getAttribute("online"));
return SUCCESS;
} 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;
} }
Struts2(八)访问Servlet API的更多相关文章
- struts2中访问servlet API
Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...
- java框架之Struts2(2)-访问Servlet API及请求数据封装
准备 为后面测试示例编写代码及配置如下: package com.zze.bean; import java.util.Date; public class User { private String ...
- struts2的action访问servlet API的三种方法
学IT技术,就是要学习... 今天无聊看看struts2,发现struts2的action访问servlet API的三种方法: 1.Struts2提供的ActionContext类 Object g ...
- struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- struts2访问servlet API
搭建环境: 引入jar包,src下建立struts.xml文件 项目配置文件web.xml. web.xml: <?xml version="1.0" encoding=&q ...
- 关于Struts2自动装配和访问Servlet API
自动装配 1.根据属性的getter和setter获取值 index.jsp <s:form action="hello" method="POST"& ...
- 配置Struts2及Struts2访问servlet api的方式
Struts2的起源与背景 在很长的一段时间内,在所有的MVC框架中,Struts1处于绝对的统治地位,无论是从市场的普及范围,还是具体的使用者数量. 其他MVC框架都无 法与其相比,作为一一款优秀的 ...
- Struts2笔记--Action访问Servlet API
Web应用中通常需要访问的Servlet API就是HttpServletRequest.HttpSession和ServletContext,这三个接口分别代表JSP内置对象中的request.se ...
- Struts2学习二----------访问Servlet API
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2提供了三种方式去访问Servlet API -ActionContext -实现*Aware接口 -ServletActionConte ...
随机推荐
- 华为S5300系列交换机V100R005SPH021升级补丁
S23_33_53-V100R005SPH021.pat 附件: 链接:https://pan.baidu.com/s/1xaEZa8hn8psHSQXrk2d9yA 密码:9b6o
- apache如何支持asp.net
Apache是目前广泛使用的一种网络服务器程序,不仅在UNIX/LINUX平台上被大量使用,而且在Windows平台上也有许多站点放弃了IIS而转向Apache..NET是微软推出的功能强大的开发技术 ...
- ELMAH--Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components 77 out of 90 rated th
MSDN===http://msdn.microsoft.com/en-us/library/aa479332.aspx PROJECT==https://code.google.com/p/elma ...
- C#程序集系列11,全局程序集缓存
全局程序集缓存(GAC:Global Assembly Cache)用来存放可能被多次使用的强名称程序集.当主程序需要加载程序集的时候,优先选择到全局程序集缓存中去找寻需要的程序集. 为什么需要全局程 ...
- MySQL数据库事务各隔离级别加锁情况--Repeatable Read && MVCC(转)
本文转自https://m.imooc.com/article/details?article_id=17289 感谢作者 上节回顾 上两篇记录了我对MySQL 事务 隔离级别read uncommi ...
- mormot当作内存数据库(缓存)使用
mormot当作内存数据库(缓存)使用 mormot的TSQLRestStorageInMemory可以作为内存数据库来使用. 上图是在笔者4代I5笔记本上做的测试,增加10万记录,耗时:562毫秒. ...
- msgpack和protobuf的对比
msgpack和protobuf的对比 msgpack的序列化速度比protobuf要快一些,但反序列化要比protobuf要慢一些,但总体都接近msgpack可以直接序列化类对象,但protobuf ...
- Android Activity的四种LaunchMode!!!
本文转自: http://marshal.easymorse.com/archives/2950. 写的非常好,分享给大家!!! 在多Activity开发中,有可能是自己应用之间的Activity跳转 ...
- 【JVM】调优笔记3-----JVM参数配置 JDK1.8
一.关于JVM参数配置,有多种途径. 1.在tomcat中直接配置的 打开tomcat的安装目录, 在bin下修改catalina.bat文件 添加如下: set "JAVA_OPTS=-X ...
- Spring Data Jpa 查询返回自定义对象
转载请注明出处:http://www.wangyongkui.com/java-jpa-query. 今天使用Jpa遇到一个问题,发现查询多个字段时返回对象不能自动转换成自定义对象.代码如下: //U ...