美文网首页
ValueStack、StackContext和request

ValueStack、StackContext和request

作者: 暮秋moco | 来源:发表于2020-02-29 12:26 被阅读0次
  • action的实例保存在ValueStack中。

  • ActionContext.getContext.put(String,Object)是把对象放到了StackContext中,这个对象跟request,session等一样,它们平起平坐,但这些都不是根对象,所以要通过#访问。

  • request.setAttribute(String,Object)就是把值放到request范围,而StackConext里含有request对象,所以可以通过#request.*来访问

  • 创建testAcrion.java

public class TestAction extends ActionSupport implements ServletRequestAware{
 
        private String tip;
        private HttpServletRequest request;
 
        public String execute()throws Exception{
                setTip("tip来源ValueStack");
                ActionContext.getContext().put("tip", "tip来源StackContext");
                request.setAttribute("tip", "tip来源Request");
 
                return SUCCESS;
        }
        @Override
        public void setServletRequest(HttpServletRequest request) {
                this.request = request;
        }
 
        public String getTip() {
                return tip;
        }
        public void setTip(String tip) {
                this.tip = tip;
        }
}

分别往ValueStack,StackContext和request这3个范围里放入了key一样,值不一样的数据。

  • 在jsp中访问3个范围的tip:
<body>
        <s:debug/>
        <table>
                <tr>
                        <td>访问ValueStack</td>
                        <td><s:property value="tip"/></td>
                </tr>
                <tr>
                        <td>访问StackContext</td>
                        <td><s:property value="#tip"/></td>
                </tr>
                <tr>
                        <td>访问Request</td>
                        <td><s:property value="#request.tip"/></td>
                </tr>
        </table>
</body>

第一行访问ValueStack里的tip

第二行访问StackContext里的tip,相当于ActionContext.getContext.get("tip");

第三行访问request范围的tip,相当于request.getAttribute("tip");

  • 结果为
Value String
访问ValueStack tip来源ValueStack
访问StackContext tip来源StackContext
访问Request tip来源Request

参考

https://blog.csdn.net/qq_36513927/article/details/54836062?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

相关文章

网友评论

      本文标题:ValueStack、StackContext和request

      本文链接:https://www.haomeiwen.com/subject/plfphhtx.html