一。struts2如何获取servletAPI
1.解耦和的方式获取封装后的Map对象,这种获取的map对象只有存取数据的功能.
Map request = (Map) ActionContext.getContext().get("request");
//获取session的map封装对象
Map session = ActionContext.getContext().getSession();
//获取application的map封装对象
Map application = ActionContext.getContext().getApplication();
2.非解耦和拿到实际的httpservlet的API,这样拿到的对象具有原来本身的功能.
//获取httpservletrequest
HttpServletRequest req = ServletActionContext.getRequest();
//HttpServletResponse
HttpServletResponse resp = ServletActionContext.getResponse();
//获取httpSession
HttpSession sess = req.getSession();
//获取Application
ServletContext app = ServletActionContext.getServletContext();
3.IOC(反转控制)的方式获取,实现特定接口(如RequestAware,SessionAware,ApplicationAware,ServletRequestAware,ServletResponseAware,ServletContextAware接口),然后给属性相应的set方法就能注入对象.
二。action中的重定向
就是把struts.xml中的result的type属性设置为:redirect
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="login" method="post"> 用户名:<input name="username"><br> 密码:<input type="password" name="pwd"><br> <input type="submit" value="提交"> </form> </body> </html>
LoginAction.java
package com.action; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.interceptor.SessionAware; import org.apache.struts2.util.ServletContextAware; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ModelDriven; import com.pojo.User; public class LoginAction implements ModelDriven<User>,RequestAware,SessionAware,ApplicationAware,ServletRequestAware,ServletResponseAware,ServletContextAware{ //1.用属性绑定参数 // String username; // String pwd; // // public String getUsername() { // return username; // } // // public void setUsername(String username) { // this.username = username; // } // // public String getPwd() { // return pwd; // } // // public void setPwd(String pwd) { // this.pwd = pwd; // } //2.用pojo对象绑定页面参数 // User user; // // public User getUser() { // return user; // } // // public void setUser(User user) { // this.user = user; // } //3.用pojo对象绑定,用ModelDriver接口来实现 User user = new User(); @Override public User getModel() { return user; } //三。注入servletAPI对象 Map request; Map session; Map application; HttpServletRequest servletRequest; HttpServletResponse servletResponse; ServletContext servletContext; public void setRequest(Map request) { this.request = request; } public void setSession(Map session) { this.session = session; } public void setApplication(Map application) { this.application = application; } public void setServletRequest(HttpServletRequest servletRequest) { this.servletRequest = servletRequest; } public void setServletResponse(HttpServletResponse servletResponse) { this.servletResponse = servletResponse; } public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } public String execute(){ System.out.println("username="+user.getUsername()+"pwd="+user.getPwd()); //一。获取被封装后的servletAPI //1.获取request被封装后的map对象 Map req = (Map) ActionContext.getContext().get("request"); req.put("user", user); //2.获取session本封装后的map对象 Map session = ActionContext.getContext().getSession(); session.put("user", user); //3.获取application被封装后的map对象 Map app = ActionContext.getContext().getApplication(); //二。获取原servletAPI HttpServletRequest req = ServletActionContext.getRequest(); HttpServletResponse resp = ServletActionContext.getResponse(); HttpSession sess = req.getSession(); ServletContext applicatipn = ServletActionContext.getServletContext(); System.out.println("请求的ip地址是:"+req.getRemoteAddr()+"||"+req.getRemoteHost()); return "success"; } }
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 欢迎${user.username }登录 </body> </html>
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="my" namespace="/" extends="struts-default"> <action name="login" class="com.action.LoginAction"> <result name="success" type="redirect">/jsp/success.jsp</result> </action> </package> </struts>