ValueStack、StackContext和request

2020-02-29  本文已影响0人  暮秋moco
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一样,值不一样的数据。

<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

上一篇 下一篇

猜你喜欢

热点阅读