struts2中访问和添加Application、session以及request属性
一、访问或添加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属性的更多相关文章
- Struts2中访问web元素的四种方式
Struts2中访问web元素的四种方式如下: 通过ActionContext来访问Map类型的request.session.application对象. 通过实现RequestAware.Sess ...
- struts2中访问servlet API
Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...
- Struts2中 ValueStack、ActionContext、ServletContext、request、session等 表格解析
变量 从ActionContext中获得 生命周期 用Ongl来读取值 使用ServletConfigInterceptor来注入 ActionContext类 静态方法ActionContext. ...
- struts2中的使用BaseAction获取Session
package com.owen.ma; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; impor ...
- Struts2中的ModelDriven机制及其运用、refreshModelBeforeResult属性解决的问题
1.为什么需要ModelDriven? 所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象.比如,有实体类User如下: package cn.com.leadfar.struts2 ...
- Struts2(八)访问Servlet API
一.Struts2中的Servlet API 1.1.struts2的Action实现了MVC中C层的作用 针对请求用户显示不同的信息 登录后段保存用户信息 ----session 保存当前在线人数等 ...
- 关于struts2中的相对路径与绝对路径
从昨天开始复习了struts2的课程,之所以重新走上java的道路,是觉得写了一年的go程序,并没有感觉到学习了什么,反而把java给忘得干干净净的.想想我的计划,年后就要换工作了,至于要换到什么方向 ...
- struts2访问或添加几个属性(request/session/application属性)
https://blog.csdn.net/hebiao100/article/details/7385055 struts2添加request.session.application属性 第一种方法 ...
- struct2访问或添加request/session/application
访问或添加request/session/application 1 通过ActionContext //这样放置 public String execute() { ActionConte ...
随机推荐
- Android yyyymmdd转成yyyy-MM-dd格式
//把yyyymmdd转成yyyy-MM-dd格式 public static String formatDate(String str){ SimpleDateFormat sf1 = new Si ...
- java.util.zip.ZipOutputStream压缩无乱码(原创)
package io; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileI ...
- linux协议栈部分资料
socket实现:http://blog.csdn.net/zhangskd(含有tcp/ip专栏) http://blog.chinaunix.net/uid-20528014-id-90573.h ...
- HDU 4604 deque 最长上升子序列
枚举每个位置,求以num[i]为起点的最长不下降子序列和以num[i]为结尾的最长不递增子序列. 并且把相同值的个数统计一下,最后要减去算重复了的. 比如: 1 9 4 4 2 2 2 3 3 3 7 ...
- linux下文件夹的创建、复制、剪切、重命名、清空和删除命令
在home目录下有wwwroot目录,wwwroot下有sinozzz目录,即/home/wwwroot/sinozzz 一.目录创建 在/home/wwwroot目录下新建一个sinozzz123的 ...
- NDK(16)Jni中GetStaticFieldID和GetMethodID 中的类型标识串
env在GetStaticFieldID和GetMethodID 时,函数参数和返回值的类型要指定类型标识串,如: jmethodID init = env->GetMethodID(clz,& ...
- BZOJ 2440 完全平方数(莫比乌斯-容斥原理)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2440 题意:给定K.求不是完全平方数(这里1不算完全平方数)的倍数的数字组成的数字集合S ...
- Java知识积累——单元测试和JUnit(一)
说起单元测试,刚毕业或者没毕业的人可能大多停留在课本讲述的定义阶段,至于具体是怎么定义的,估计也不会有太多人记得.我们的教育总是这样让人“欣 慰”.那么什么是单元测试呢?具体科学的定义咱就不去关心了, ...
- WebBrowser自动点击链接 广告自动点击 Ads Auto Click
点击付费广告, 是目前比较流行的一种网络营销模式, 但是,如果你单纯的靠手工一个一个去点, 总觉得不划算 , 能不能实现自动的去点击呢? 答案是肯定的 .NET 里面的 WebBrowser, 可以 ...
- MacOS安装phpMyAdmin几点问题
1. 登录时出现“#2002 无法登录 MySQL 服务器”. 原因: phpMyAdmin为PHP编写,MacOS默认安装的php配置,设置mysql监听socket默认为/var/mysql/my ...