Struts2-学习笔记系列(4)-访问servlet api
2016-06-04 22:19
639 查看
5.1通过actioncontext:
取数据:注意写在HTML页面的OGNL表达式语法
5.2实现servletcontextaware、servletrequestaware、servletresponseaware
实现ServletResponseAware 设置cookie
5.3使用servletactioncontext
public String execute() throws Exception { ActionContext ctx = ActionContext.getContext(); // 通过ActionContext访问application范围的属性值 Integer counter = (Integer) ctx.getApplication().get("counter"); if (counter == null) { counter = 1; } else { counter = counter + 1; } // 通过ActionContext设置application范围的属性 ctx.getApplication().put("counter", counter); // 通过ActionContext设置session范围的属性 ctx.getSession().put("user", getUser()); if (getUser().equals("zcx")) { // 通过ActionContext设置request范围的属性 ctx.put("tip", "欢迎登录"); return SUCCESS; } // 通过ActionContext设置request范围的属性 ctx.put("tip", "登录失败"); return ERROR; }
取数据:注意写在HTML页面的OGNL表达式语法
${applicationScope.counter} ${sessionScope.user} ${requestScope.tip}
5.2实现servletcontextaware、servletrequestaware、servletresponseaware
实现ServletResponseAware 设置cookie
public class LoginAction implements Action, ServletResponseAware { private String user; private String pwd; private String tip; private HttpServletResponse response; public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String execute() throws Exception { ActionContext ctx = ActionContext.getContext(); // 通过ActionContext访问application范围的属性值 Integer counter = (Integer) ctx.getApplication().get("counter"); if (counter == null) { counter = 1; } else { counter = counter + 1; } // 通过ActionContext设置application范围的属性 ctx.getApplication().put("counter", counter); // 通过ActionContext设置session范围的属性 ctx.getSession().put("user", getUser()); if (getUser().equals("zcx")) { // 通过response添加Cookie Cookie c = new Cookie("user", getUser()); c.setMaxAge(60 * 60); response.addCookie(c); // 通过ActionContext设置request范围的属性 ctx.put("tip", "服务器提示:您已经成功的登录"); return SUCCESS; } // 通过ActionContext设置request范围的属性 ctx.put("tip", "登录失败"); return ERROR; } public String getTip() { return tip; } public void setTip(String tip) { this.tip = tip; } @Override public void setServletResponse(HttpServletResponse httpServletResponse) { this.response = response; } }
5.3使用servletactioncontext
public class LoginAction implements Action { private String user; private String pwd; private String tip; private HttpServletResponse response; public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String execute() throws Exception { ActionContext ctx = ActionContext.getContext(); // 通过ActionContext访问application范围的属性值 Integer counter = (Integer) ctx.getApplication().get("counter"); if (counter == null) { counter = 1; } else { counter = counter + 1; } // 通过ActionContext设置application范围的属性 ctx.getApplication().put("counter", counter); // 通过ActionContext设置session范围的属性 ctx.getSession().put("user", getUser()); if (getUser().equals("zcx")) { // 通过response添加Cookie Cookie c = new Cookie("user", getUser()); c.setMaxAge(60 * 60); ServletActionContext.getResponse().addCookie(c); // 通过ActionContext设置request范围的属性 ctx.put("tip", "服务器提示:您已经成功的登录"); return SUCCESS; } // 通过ActionContext设置request范围的属性 ctx.put("tip", "登录失败"); return ERROR; } public String getTip() { return tip; } public void setTip(String tip) { this.tip = tip; } }
相关文章推荐
- java常用工具类【2】
- Struts2-学习笔记系列(3)-返回视图
- Struts2-学习笔记系列(2)-常量配置和实现action
- java中如何将byte数组内容转换为字符串?
- JavaScrip学习笔记(五)---定时器(一)实现图片无缝滚动
- SpringMVC入门程序(二)
- Struts2的Action的通配符
- java集合
- java 普里姆(Prim)算法求图的最小生成树
- 2、struts2_action详解
- java I/O库中设计模式的应用
- java.util.BitSet使用与分析
- leetcode-Java-70. Climbing Stairs
- JAVA-WEB开发第五讲[2016-06-04]SPRING-MVC环境搭建
- java上转型的多态
- 1、struts2的入门
- Spring mvc 原理浅析
- Spring事务传播机制
- MyEclipse/Eclipse 乱码 解决方案总结(UTF8 -- GBK)
- java 线程中join的使用