标签:username return get public except throw on() import ack
import com.opensymphony.xwork2.ActionContext; public String execute() throws Exception { if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){ //获取Map方式的request //ActionContext.getContext().put("msg", "登录成功"); //获取Map方式的Session //ActionContext.getContext().getSession().put("msg", "登录成功"); //获取Map方式的Application ActionContext.getContext().getApplication().put("msg", "登录成功"); return "success"; } return "error"; }
import org.apache.struts2.ServletActionContext; public String execute() throws Exception { if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){ ServletActionContext.getRequest().setAttribute("msg", "登录成功"); ServletActionContext.getResponse(); ServletActionContext.getPageContext().setAttribute("msg", "登录成功"); ServletActionContext.getRequest().getSession().setAttribute("msg", "登录成功"); return "success"; } return "error"; }
package com.djoker.struts2; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; public class LoginAction implements ServletRequestAware,ServletResponseAware{ private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } private HttpServletRequest request; private HttpServletResponse response; public String execute() throws Exception { if("admin".equals(user.getUsername()) && "admin".equals(user.getPassword())){ this.request.setAttribute("msg", "登录成功"); return "success"; } return "error"; } @Override public void setServletResponse(HttpServletResponse response) { this.response = response; } @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } }
struts2学习笔记之六:struts2的Action访问ServletAPI的几种方式
标签:username return get public except throw on() import ack
原文地址:http://www.cnblogs.com/djoker/p/6218587.html