码迷,mamicode.com
首页 > Web开发 > 详细

Struts2 Web 资源获取的四种方式

时间:2016-08-02 21:08:22      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

struts.xml

 <action name="FirstAction" class="Action.FirstAction">
            <result name="success">/success.jsp</result>
 </action>

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>

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>

index.jsp

  <form action="SecondAction.action" method="post">
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>

index.jsp

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

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>

index.jsp

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

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;
    }
}

技术分享
技术分享


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


登陆小实例
struts.xml

  <action name="LoginAction" class="Action.LoginAction">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>

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;
    }
}

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;
    }
}

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>

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>

技术分享
技术分享
Success页面获取到了登陆的用户名 说明存到了session中。

Struts2 Web 资源获取的四种方式

标签:

原文地址:http://blog.csdn.net/yen_csdn/article/details/52097513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!