您的位置:首页 > 编程语言 > Java开发

Struts2-学习笔记系列(4)-访问servlet api

2016-06-04 22:19 639 查看
5.1通过actioncontext:

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;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: