一、访问或添加Application、session、request属性

《一》方式一

HelloWorldAction类中添加如下代码

//此方法适用于仅对Application、session、request对象添加属性值
public String execute(){
//访问或添加Application、session、request属性
ActionContext act=ActionContext.getContext();
act.getApplication().put("app", "application范围(应用范围)");
act.getSession().put("ses", "session范围");
act.put("req", "request范围");
return "success";
}

Struts.xml文件内容:(使用了通配符的方式)

<action name="helloworld_*" class="test.struts2.HelloWorldAction" method="{1}" >

<result name="success">/WEB-INF/page/hello.jsp</result>

</action>

Hello.jsp页面内容:

${applicationScope.app}</br>

${sessionScope.ses}</br>

${requestScope.req}</br>

访问连接可以为:

http://localhost:8080/struts2/test/helloworld_execute.do(注意:do,xxx为自定义后缀,这样访问时必须加上)

http://localhost:8080/struts2/test/helloworld_execute.xxx

http://localhost:8080/struts2/test/helloworld_execute.action

http://localhost:8080/struts2/test/helloworld_execute为无效链接

http://localhost:8080/struts2/test/helloworld为无效链接

http://localhost:8080/struts2/test/helloworld.action也可以访问

http://localhost:8080/struts2/test/helloworld.do也可以访问

结论:

在使用通配符的方式下(name="helloworld_*",method="{1}")如果地址栏中地址helloworld后没有直接跟方法名execute或addUI等,那么默认执行execute方法,因为在配置文件中  <constant name="struts.action.extension" value="do,action,xxx,aiai"/>定义了几个后缀,所以访问连接必须添加其中之一方有效。以上五个有效连接访问结果一样。

结果为:

application范围(应用范围)

session范围

request范围

《二》方式二

此方式适用于需要获取绝对路径等重要信息时使用

此时在Action类HelloWorldAction类中添加如下代码,其他地方不改动

    public String rsa()
{
HttpServletRequest request=ServletActionContext.getRequest();
ServletContext servletContext=ServletActionContext.getServletContext();
request.setAttribute("req", "请求方位属性");
request.getSession().setAttribute("ses", "会话范围属性");
servletContext.setAttribute("app", "应用范围属性");
return "success";
}

访问地址:

http://localhost:8080/struts2/test/helloworld_execute!rsa.do

http://localhost:8080/struts2/test/helloworld!rsa.do

http://localhost:8080/struts2/test/helloworld_rsa.do

http://localhost:8080/struts2/test/helloworld_addUI!rsa.do

http://localhost:8080/struts2/test/helloworld_addUI!Rsa无效

http://localhost:8080/struts2/test/helloworld_rsa无效

结果为:

应用范围属性

会话范围属性

请求方位属性

补充:

public String execute(){
//访问或添加Application、session、request属性
ActionContext act=ActionContext.getContext();
act.getApplication().put("app", "application范围(应用范围)");
act.getSession().put("ses", "session范围");
act.put("req", "request范围");
act.put("names",Arrays.asList("幽林孤狼","心晴","幽林孤狼B组"));//新
return "success";
}

再\WebRoot\WEB-INF\lib目录下引入如下两个jar文件

在hello.jsp文件中添加:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"  %>

以及:

<c:forEach items="${names}" var="name">

${name}</BR>

</c:forEach>

访问结果为:

幽林孤狼

心晴

幽林孤狼B组

struts2中访问和添加Application、session以及request属性的更多相关文章

  1. Struts2中访问web元素的四种方式

    Struts2中访问web元素的四种方式如下: 通过ActionContext来访问Map类型的request.session.application对象. 通过实现RequestAware.Sess ...

  2. struts2中访问servlet API

    Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...

  3. Struts2中 ValueStack、ActionContext、ServletContext、request、session等 表格解析

    变量 从ActionContext中获得 生命周期 用Ongl来读取值 使用ServletConfigInterceptor来注入 ActionContext类 静态方法ActionContext. ...

  4. struts2中的使用BaseAction获取Session

    package com.owen.ma; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; impor ...

  5. Struts2中的ModelDriven机制及其运用、refreshModelBeforeResult属性解决的问题

    1.为什么需要ModelDriven? 所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User如下: package cn.com.leadfar.struts2 ...

  6. Struts2(八)访问Servlet API

    一.Struts2中的Servlet API 1.1.struts2的Action实现了MVC中C层的作用 针对请求用户显示不同的信息 登录后段保存用户信息 ----session 保存当前在线人数等 ...

  7. 关于struts2中的相对路径与绝对路径

    从昨天开始复习了struts2的课程,之所以重新走上java的道路,是觉得写了一年的go程序,并没有感觉到学习了什么,反而把java给忘得干干净净的.想想我的计划,年后就要换工作了,至于要换到什么方向 ...

  8. struts2访问或添加几个属性(request/session/application属性)

    https://blog.csdn.net/hebiao100/article/details/7385055 struts2添加request.session.application属性 第一种方法 ...

  9. struct2访问或添加request/session/application

    访问或添加request/session/application 1 通过ActionContext //这样放置 public String execute()  {     ActionConte ...

随机推荐

  1. 关于ssh的一篇很好的文章

    源地址:http://www.w3hacker.com/?p=156   ssh-agent的manual写得倒是挺详细,可看了好几次都没怎么搞明白.08年在网上找到了非常好的一篇文章,An Illu ...

  2. UVa 11922 - Permutation Transformer 伸展树

    第一棵伸展树,各种调试模板……TVT 对于 1 n 这种查询我处理的不太好,之前序列前后没有添加冗余节点,一直Runtime Error. 后来加上冗余节点之后又出了别的状况,因为多了 0 和 n+1 ...

  3. ios 使用GCD 多线程 教程

    什么是GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4.0中.GCD ...

  4. 2014图灵技术图书最受欢迎TOP15

    来自:图灵社区昨晚给我发的邮件,感觉不错,和大家分享,mark下. [小编语] 回首2014,感谢小伙伴们一路相随.让我们2015一起更快乐地玩耍.今天小编为大家盘点一下过去2014年表现最给力的技术 ...

  5. UVa 10969 (圆与圆之间的覆盖问题) Sweet Dream

    题意: 有n个按先后顺序放置的不同大小不同位置的圆,求所有可见圆弧的长度. 分析: 这道题应该是大白书上例题 LA 2572 (求可见圆盘的数量) Kanazawa 的加强版,整体框架都差不多. 对于 ...

  6. bzoj1567: [JSOI2008]Blue Mary的战役地图

    将矩阵hash.s[0]忘了弄成0,输出中间过程发现了. hash.sort.判重.大概这样子的步骤吧. #include<cstdio> #include<cstring> ...

  7. Asp.Net时间戳与时间互转

    /// <summary> /// 时间戳转成时间类型 /// </summary> /// <param name="timeStamp">& ...

  8. 定义 androidlistview 滚动条位置

    1.找到每一页的最后一条数据的位置 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, ...

  9. Asp.Net Web Api 与 Andriod 接口对接开发经验,给小伙伴分享一下!

    最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用Asp.Net Web Api去跟 Andriod 那端做接口对接工作,自己也是第一次接触Web ...

  10. 我是红领巾,分享2014 google不能用的方法。

    那啥已经20天打不开了. 得爬qiang. 今天无意间发现一个好东东. 特记录一下.    360浏览器设置 1.   工具菜单==>选项==>高级设置==>管理搜索引擎 . 2. ...