Struts2 Web 资源获取的4种方式

来源:互联网 发布:centos7 yum安装wget 编辑:程序博客网 时间:2024/05/29 15:16

Struts2 Web 资源获取一个有四种方式,两个大类

  • 拦截器获取 Web 资源模式
  • 静态对象获取 Web 资源模式

第一种:实现ServletRequestAware、ServletResponseAware、ServletContextAware接口

struts.xml

 <action name="FirstAction" class="Action.FirstAction">            <result name="success">/success.jsp</result> </action>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="s" uri="/struts-tags" %><html>  <head>    <title>$Title$</title>  </head>  <body>  <h2>用户登陆</h2>    <form action="FirstAction.action" method="post">    <ul>      <li>姓名:<input type="text" name="username"></li>      <li>密码:<input type="text" name="password"></li>      <li><input type="submit" value="提交"></li>    </ul>    </form>  </body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

FirstAction.Java

package Action;import com.opensymphony.xwork2.ActionSupport;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.util.ServletContextAware;import javax.servlet.ServletContext;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Created with IntelliJ IDEA. * User: YEN * Date: 2016/8/2 * Time: 19:31 */public class FirstAction  extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{    private ServletRequest request;    private ServletResponse response;    private ServletContext context;    @Override    public void setServletRequest(HttpServletRequest httpServletRequest) {        request=httpServletRequest;    }    @Override    public void setServletResponse(HttpServletResponse httpServletResponse) {response=httpServletResponse;}    @Override    public void setServletContext(ServletContext servletContext) { context=servletContext; }    @Override    public String execute() throws Exception {        //验证表单数据是否提交过来了        String username=request.getParameter("username");        String password=request.getParameter("password");        System.out.println("第一种方式:"+username+"........"+password);        return SUCCESS;    }

}

第二种方式:实现RequestAware接口 
struts.xml

        <action name="SecondAction" class="Action.SecondAction">            <result name="success">/success.jsp</result>        </action>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

index.jsp

  <form action="SecondAction.action" method="post">
  • 1
  • 1
package Action;import com.opensymphony.xwork2.ActionSupport;import org.apache.struts2.StrutsStatics;import org.apache.struts2.interceptor.RequestAware;import javax.servlet.ServletContext;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import java.util.Map;/** * Created with IntelliJ IDEA. * User: YEN * Date: 2016/8/2 * Time: 19:42 *//** * 使用RequestAware拦截器 */public class SecondAction extends ActionSupport implements RequestAware {    private ServletRequest request;    private ServletResponse response;    private ServletContext context;    @Override    public void setRequest(Map<String, Object> map) {        request=(ServletRequest)map.get(StrutsStatics.HTTP_REQUEST);        response=(ServletResponse)map.get(StrutsStatics.HTTP_RESPONSE);        context=(ServletContext)map.get(StrutsStatics.SERVLET_CONTEXT);    }    @Override    public String execute() throws Exception {        System.out.println("第二种方式:"+request.getParameter("username")+"..............."+request.getParameter("password"));        return SUCCESS;    }}

第三种方式:静态对象获取方式1 
struts.xml

  <action name="ThirdAction" class="Action.ThirdAction">            <result name="success">/success.jsp</result>        </action>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

index.jsp

<form action="ThirdAction.action" method="post">
  • 1
  • 1

ThirdAction

package Action;/** * Created with IntelliJ IDEA. * User: YEN * Date: 2016/8/2 * Time: 19:58 */import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import org.apache.struts2.ServletActionContext;import javax.servlet.ServletContext;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;/** * 静态对象获取web资源 */public class ThirdAction extends ActionSupport{    @Override    public String execute() throws Exception {        ActionContext ac=ActionContext.getContext();        ServletRequest request=(ServletRequest)ac.get(ServletActionContext.HTTP_REQUEST);        ServletResponse response=(ServletResponse)ac.get(ServletActionContext.HTTP_RESPONSE);        ServletContext context=(ServletContext)ac.get(ServletActionContext.SERVLET_CONTEXT);        String username=request.getParameter("username");        String password=request.getParameter("password");        System.out.println(username+"............"+password);        return super.execute();    }}

第四种方式:静态对象获取方式2 
struts.xml

  <action name="FourAction" class="Action.FourAction">            <result name="success">/success.jsp</result>        </action>
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

index.jsp

 <form action="FourAction.action" method="post">
  • 1
  • 1

FourAction

package Action;import com.opensymphony.xwork2.ActionSupport;import org.apache.struts2.ServletActionContext;import javax.servlet.ServletRequest;/** * Created with IntelliJ IDEA. * User: YEN * Date: 2016/8/2 * Time: 20:04 */public class FourAction extends ActionSupport {    @Override    public String execute() throws Exception {        ServletRequest request= ServletActionContext.getRequest();        System.out.println("第四种方式"+request.getParameter("username")+".............."+request.getParameter("password"));        return SUCCESS;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22


四种方式中第四种最简单,在一般开发中最常用的也是第四种。


登陆小实例 
struts.xml

  <action name="LoginAction" class="Action.LoginAction">            <result name="success">/success.jsp</result>            <result name="error">/error.jsp</result>        </action>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

Users

package Action;/** * Created with IntelliJ IDEA. * User: YEN * Date: 2016/8/2 * Time: 13:37 */public class Users {    private String username;    private String password;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

LoginAction

package Action;import org.apache.struts2.ServletActionContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;/** * Created with IntelliJ IDEA. * User: YEN * Date: 2016/8/2 * Time: 12:55 */public class LoginAction {    private String username;    private String password;    /**属性驱动方式     * 并且将获取到的信息存到session中 供其它页面使用     * @return     */    public String execute(){        if("admin".equals(username) && "admin".equals(password)){            Users user=new Users();            user.setUsername(username);            user.setPassword(password);            HttpServletRequest request= ServletActionContext.getRequest();            HttpSession session=request.getSession();            session.setAttribute("user",user);            return "success";        }else return "error";    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="s" uri="/struts-tags" %><html>  <head>    <title>$Title$</title>  </head>  <body>  <h2>用户登陆</h2>    <form action="LoginAction.action" method="post">    <ul>      <li>姓名:<input type="text" name="username"></li>      <li>密码:<input type="text" name="password"></li>      <li><input type="submit" value="提交"></li>    </ul>    </form>  </body></html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Title</title></head><body>success!!<%    Users user=(Users)session.getAttribute("user");%>欢迎您:<%=user.getUsername()%></body></html>

                                             
0 0
原创粉丝点击