Struts2自定義過濾器的小例子-入門篇


創建web項目    實現的效果! 用戶點擊頁面不同的鏈接,后台調用不同的代碼!

創建兩個類實現共同的接口!

public interface Action { 

    String  execute();
}

 

復制代碼
public class LoginAction  implements Action{

    public  String  execute(){
        System.out.println("LoginAction......");
        return  "success";
    }
}
復制代碼

 

復制代碼
public class ListAction implements Action {

    public  String  execute(){
        System.out.println("ListAction......");
        return  "success";
    }
}
復制代碼

 

想讓用戶能訪問到我們的后台代碼,要么使用servlet  要么使用filter!

使用filter

創建一個filter用來攔截用戶的請求

復制代碼
public class DoFilter implements Filter {
    //全局的變量
     Map<String,String> map=new  HashMap<String, String>();
    
     //初始化操作
    @Override
    public void init(FilterConfig arg0) throws ServletException {
        System.out.println("DoFilter 初始化了.............................");
     //  key是用戶請求的路徑  value 是對應的全類名
        map.put("/login","cn.bdqn.action.LoginAction");
        map.put("/list","cn.bdqn.action.ListAction");
    }


    //真正的處理
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
         //向下轉型
        HttpServletRequest httpServletRequest=(HttpServletRequest) request;
        HttpServletResponse httpServletResponse=(HttpServletResponse) response;
        //看一下 各個路徑的區別
        System.out.println("getContextPath()==>"+httpServletRequest.getContextPath());//項目名
        System.out.println("getServletPath()==>"+httpServletRequest.getServletPath());//訪問的路徑
        System.out.println("getRequestURI()==>"+httpServletRequest.getRequestURI());//項目下面的路徑
        System.out.println("getRequestURL()==>"+httpServletRequest.getRequestURL());//帶協議的完整路徑
       //應該使用getServletPath
        String  path=httpServletRequest.getServletPath();
        try {
            if (path.equals("/index.jsp")) {
                chain.doFilter(request, response);  //放行
            }else{
                Action action=(Action) Class.forName(map.get(path)).newInstance();
                action.execute();
                //跳轉到成功界面
                httpServletRequest.getRequestDispatcher("/success.jsp").forward(request, response);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
        
    }
    @Override
    public void destroy() {

    }


}
復制代碼

 

 

前台頁面

復制代碼
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <a  href="login">登錄 </a>
    <a  href="list">詳情 </a>
  </body>
</html>
復制代碼

 

sucess.jsp頁面就是一個成功界面!!!省略掉!

 

我們使用xml文件來代替  map中 所保存的 鍵值對  信息!

key:用戶的請求

value:對應的后台實現類 全類名!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM