action解耦方式
ServletAction方式,必须要有Servlet容器作支持
package com.hanqi.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
public class TestServletAction {
public String testServletAction()
{
//访问原生的web资源
//1得到servletContext,相当于application
ServletContext sc= ServletActionContext.getServletContext();
System.out.println("app1="+sc.getAttribute("app1"));
sc.setAttribute("app2", "原生的方式加入的application");
//2访问Session
HttpServletRequest hsr=ServletActionContext.getRequest();
HttpSession hs=hsr.getSession();
System.out.println("ses1="+hs.getAttribute("ses1"));
hs.setAttribute("ses2", "原生方式放入的session");
//3访问request
System.out.println("req1="+hsr.getAttribute("req1"));
hsr.setAttribute("req2", "原生方式放入的request");
//4 访问parameter
System.out.println("user1="+hsr.getParameter("user"));
System.out.println("user2="+hsr.getParameter("user"));
return "success";
}
}
实现接口方式
package com.hanqi.action;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;
public class TestServletActionAware
implements ServletRequestAware, ServletContextAware {
private ServletContext sc;
private HttpServletRequest hsr;
private HttpSession hs;
@Override
public void setServletContext(ServletContext arg0) {
sc=arg0;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
hsr=arg0;
hs=arg0.getSession();
}
public String testServletAware()
{
System.out.println("app1="+sc.getAttribute("app1"));
sc.setAttribute("app2", "新的application");
System.out.println("ses1="+hs.getAttribute("ses1"));
hs.setAttribute("ses2","新的session");
System.out.println("user="+hsr.getParameter("user"));
hsr.setAttribute("req2", "新的request");
return "success";
}
}
action解耦方式的更多相关文章
- Struts2中的Action类(解耦方式,耦合方式)
一.解耦方式 特点:对web资源进行了封装,便于单元测试. 实现:ActionContext和接口方式 1.ActionContext 特点:Action执行的上下文对象.保存了执行Action所需要 ...
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- MVC 在视图中获取当前的Controller、Action的方式
在视图中获取Controller和Action的方式: Controller: @ViewContext.RouteData.Route.GetRouteData(this.Context).Valu ...
- action 耦合方式
//ActionContext 方式 package com.hanqi.action; import java.util.Map; import com.opensymphony.xwork2.Ac ...
- structs2的action实现方式
Action的实现方式第一种:在web.xml中添加配置<filter> <filter-name>struts2</filter-name> <filter ...
- [水煮 ASP.NET Web API2 方法论](3-7)默认 Action 请求方式以及 NonActionAttribute
问题 在 Controller 中有一个 public 的方法,但是又不想将这个 publlic 方法暴露成为一个 API. 解决方案 ASP.NET Web API 中,正常是通过 HTTP 谓词来 ...
- MVC Action控制方式
1.Controller 的OnActionExecuting中控制 protected override void OnActionExecuting(ActionExecutingContext ...
- 路由组件传参-props解耦方式(主要)
在组件中使用 $route 会使之与其对应路由形成高度耦合,从而使组件只能在某些特定的 URL 上使用,限制了其灵活性. 使用 props 将组件和路由解耦: 取代与 $route 的耦合 const ...
- Struts 2 实现Action的几种方式_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 Action用于处理用户的请求,因此也被称为业务控制器.每个Action类就是一个工作单元,Struts 2框架负责将用 ...
随机推荐
- Web Api初试
Web Api初试 前言 ASP.NET Web API 与之前的内建HTTP服务解决方案的不同之处在于,它一开始就是围绕HTTP协议及其消息语义构建起来的.与WCF REST或ASP.NET AJA ...
- linux学习心得之vim/Cvim篇
linux学习心得之vim/Cvim篇 在linux 下,vim 时一种最常见的编辑器,很多linux的发行版就自带了.我的是CentOS 6.3. 基本上Vim共分为3种模式,分别是一般模式,编辑模 ...
- Castle Windsor 项目中快速使用
Castle Windsor 项目中快速使用 新建项目如下: 一个模型类,一个接口,一个实现方法.我的目的很明确就是在UI层通过Castle 调用数据访问层的方法. 添加项目引用 CastleDemo ...
- 一个吊丝android个人开发者的逆袭之路
转眼间,一年多过去了,记得我开发第一款android应用的时候,那是在前年的冬天,我本人是做java的,android的学习和开发完全是业余爱好,从前年上半年到前年下半年大约花了半年的业余时间把and ...
- 一个意想不到的Javascript内存泄漏
原文:http://point.davidglasser.net/2013/06/27/surprising-javascript-memory-leak.html 本周我在Meter的同事追踪到了一 ...
- Microsoft.VisualBasic.dll的妙用and 改善C#公共程序类库质量的10种方法
Microsoft.VisualBasic.dll的妙用(开发中肯定会用到哦) 前言 做过VB开发的都知道,有一些VB里面的好的函数在.NET里面都没有,而Microsoft.VisualBasic. ...
- kivy create a package for Android
Now that you've successfully coded an app. Now you want to deploy it to Android. So now we would nee ...
- Java Concurrency (1)
Memory that can be shared betweenthreads is called shared memory or heap memory. The term variable a ...
- 怀念的东西:Pirka咖啡,芬兰的味道
怀念的东西:Pirka咖啡,芬兰的味道 前一段收到了小牛同学从芬兰托人寄来的咖啡.拿着提货单的时候,我满脑子问号.这寄货人是谁的呢,我完全没有印象.而且写的是食品.我又想起了最近报道的诈骗消息,给你寄 ...
- [ios2]iOS 使用subversion管理iOS源代码 【转】
使用subversion管理iOS源代码 1.安装和配置subversion服务器 在windows 服务器上安装VisualSVN-Server,下载地址http://www.visualsvn.c ...