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(注意:doxxx为自定义后缀,这样访问时必须加上)

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后没有直接跟方法名executeaddUI等,那么默认执行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";
    }

 

再WebRootWEB-INFlib目录下引入如下两个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组

 

 

原文地址:https://www.cnblogs.com/ylgl/p/3829280.html