java之struts2之ServletAPI
在之前的学习中struts2已经可以处理大部分问题了。但是如果要将用户登录数据存入session中,可以有两种方式开存入ServletAPI。
一种解耦合方式,一种耦合方式。
1. 解耦合方式
解耦合方式---ActionContext : 解耦合方式获取的都是map对象
//登录
public String login(){
if("siggy".equals(user.getName())&&"1111".equals(user.getPwd())){
//登录成功 数据存入Session中
//通过ActionContext可以获得Session
Map<String,Object> session = ActionContext.getContext().getSession();
session.put("currentUser", user);
/**
* 通过解耦合的方式还可以获取request,application
*/
Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
Map<String,Object> application = ActionContext.getContext().getApplication();
return Action.SUCCESS;
}
return Action.LOGIN;
}
好处:方便测试,没有侵入性,ActionContext可以不在Servlet容器环境中使用
2.耦合方式获取ServletAPI
下面讲的是耦合方式获取的Servlet对象
2.1 通过ActionContext获取
//登录
public String login(){
if("siggy".equals(user.getName())&&"1111".equals(user.getPwd())){
//登录成功 数据存入Session中
/**
* 通过ActionContext对象获取HttpServletRequest对象
*/
HttpServletRequest req = (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
HttpSession session = req.getSession();
session.setAttribute("currentUser", user);
/**
* 在ActionContext中还可以获取HttpServletResponse对象
* 和ServletContext对象
*/
HttpServletResponse resp = (HttpServletResponse)ActionContext.getContext().get(StrutsStatics.HTTP_RESPONSE);
ServletContext sc =(ServletContext)ActionContext.getContext().get(StrutsStatics.SERVLET_CONTEXT);
return Action.SUCCESS;
}
return Action.LOGIN;
}
2.2 通过ServletActionContext获取
//登录
public String login(){
if("siggy".equals(user.getName())&&"1111".equals(user.getPwd())){
//登录成功 数据存入Session中
//通过ServletActionContext以耦合方式获取ServletAPI对象
HttpServletRequest req = ServletActionContext.getRequest();
HttpSession session = req.getSession();
session.setAttribute("currentUser", user);
/**
* 通过ServletActionContext工具类还可以获取到其他的ServletAPI对象
*/
HttpServletResponse resp = ServletActionContext.getResponse();
ServletContext sc = ServletActionContext.getServletContext();
return Action.SUCCESS;
}
return Action.LOGIN;
}
2.3 通过实现接口的方式来获取
public class UserAction4 implements ServletRequestAware{
private User user;
private HttpServletRequest req;
@Override
public void setServletRequest(HttpServletRequest request) {
this.req = request;
}
//登录
public String login(){
if("siggy".equals(user.getName())&&"1111".equals(user.getPwd())){
//登录成功 数据存入Session中
//通过ServletActionContext以耦合方式获取ServletAPI对象
HttpSession session = this.req.getSession();
session.setAttribute("currentUser", user);
return Action.SUCCESS;
}
return Action.LOGIN;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
java之struts2之ServletAPI的更多相关文章
- 微信企业号回调模式配置解说 Java Servlet+Struts2版本号 echostr校验失败解决
微信企业号回调模式配置解说 Java Servlet+Struts2版本号 echostr校验失败解决 echostr校验失败,请您检查是否正确解密并输出明文echostr 异常java.securi ...
- Java之struts2框架学习
Java之struts2框架学习 About Struts2 Struts也是一款MVC框架 , Struts2是Struts的下一代产品,是在Struts1和WebWork的技术基础上进行了合并的全 ...
- Java框架Struts2
struts2的核心和工作原理 在学习struts2之前,首先我们要明白使用struts2的目的是什么?它能给我们带来什么样的好处? 设计目标 Struts设计的第一目标就是使MVC模式应用于we ...
- JAVA框架Struts2 servlet API
一:servlet API 1)完全解耦接口: 使用ActionContext类进行相关操作: package jd.com.actioncontex; import com.opensymphony ...
- 2018.11.21 struts2获得servletAPI方式及如何获得参数
访问servletAPI方式 第一种:通过ActionContext (重点及常用 都是获得原生对象) 原理 Action配置 被引入的配置文件 在页面调用取值 第二种:通过ServletAction ...
- java 的 struts2 Spring Hibernate 三大框架的整合
原理就不说了,直接上配置文件及代码,用来备用 首先,将三大框架所需要的jar包导入项目中 导入 struts2-spring-plugin-2.3.3.jar包 此包的作用是作为struts2 与 ...
- [Java Web]Struts2解决中文乱码问题
1.设置struts的字符编码,能够在struts.xml中添加下面代码: <constant name="struts.i18n.encoding" value=" ...
- [Java Web]Struts2加起来(一个)
Struts2环境配置 进口Struts2的需要jar包 在WEB-INF/classes(src)文件夹下创建struts.xml文件 在web.xml文件里加入Struts过滤器信息 经常使用配置 ...
- java里Struts2学习登录练习详解
最近在学struts2里面遇到很多错误,今天跟大家分享一下,我的开发工具是Eclipse: 1.到网上下载Struts2的包,这里不再累赘,百度有很多: 2.新建一个项目,记得后面加上web.xml文 ...
随机推荐
- 一次修复linux的efi引导的集中方法总结记录
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/grub_uefi_repair 起因:EFI分区被删除导致引导问 ...
- Noip2019暑期训练2
题目名称 骑士遍历 和谐俱乐部 农场派对 对称二叉树 存盘文件名 knight Beautiful party tree 输入文件名 knight.in Beautiful.in party.in ...
- VSCode 本地如何查看历史页面
1.首先要在VSCode的扩展中安装一个 Local history插件,蓝色框部分不用管,直接安装即可 2.安装并操作:安装后,修改 productManage/supplierList/addSu ...
- 坑:jmeter部署AWS云服务器时出现连接超时Non HTTP response code: org.apache.http.conn.HttpHostConnectException
背景: jmeter脚本部署到云服务器(AWS EC2)公网上时,启动jmeter脚本运行了5个小时才运行完毕,后面发现脚本报错timeout(如图),找了很久不知道原因,后面进入脚本发现全部在报错. ...
- 【POJ2996】Help Me with the Game
题目传送门 本题知识点:模拟(如果对国际象棋不熟悉的同学可以先百度一下) 题意很简单,就是让我们找出白棋跟黑棋每枚棋子的位置,并要按照一定的顺序输出( K -> Q -> R -> ...
- ICEM-一种网格画法的思考
原视频下载链接:https://pan.baidu.com/s/1kV4Zj3x 密码: uthc
- _nl_intern_locale_data: Assertion `cnt < (sizeof (_nl_value_type_LC_TIME) / sizeof (_nl_value_type_LC_TIME[0]))' failed
在Ubuntu18上使用交叉编译工具,报这个错.研究之下发现,工具的绝对路径过长,ubuntu18对其优化,修改路径,导致报错. 使用命令:export LC_ALL=C
- Python之schedule用法,类似linux下的crontab
# -*- coding: utf-8 -*- # author:baoshan import schedule import time def job(): print("I'm work ...
- Gson字符串编码,字符串转换成图片保存,二进制转换成图片保存
import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.File; import ...
- 【linux学习笔记六】压缩 解压缩命令
所有的压缩文件一定要写压缩格式的扩展名 .zip格式压缩 #压缩文件 zip 压缩文件名 源文件 #压缩目录 zip -r 压缩文件名 源目录 #解压缩 unzip 压缩文件 .gz格式压缩 #压缩为 ...