程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 層層遞進Struts1(一)之由Servlet引入Struts

層層遞進Struts1(一)之由Servlet引入Struts

編輯:關於JAVA

在說Struts1之前,我們先來使用Servlet編寫一個程序從而了解Struts產生原因和用途:一個簡單的增刪 改查,我們來看一下使用Servlet是如何實現的,為了形象說明,我們從最基本的開始寫,然後逐步遞進。

第一版

這一版我們使用最常規的做法,增刪改查每個功能都使用自己的Servlet,內容如下:

文件樹

index.jsp

<form 

action="servlet/AddUserServlet" method="post">  
    姓名:<input type="text" name="username" /><br/>  
    <input type="submit" value="submit"/>   
</form>

Servlet

為了方便說明此處的四個Servlet內容基本相同,此處以 AddUserServlet和DelServlet為例:

AddUserServlet

package com.tgb.struts1.servlet;  
      
import java.io.IOException;  
      
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
      
public class AddUserServlet extends HttpServlet {  
      
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
            String username=request.getParameter("username");  
            UserManager userManager=new UserManager();  
            userManager.add(username);  
            request.getRequestDispatcher("/add_success.jsp").forward(request, response);  
    }  
      
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
            doGet(request, response);  
    }  
}

DelUserServlet

package com.tgb.struts1.servlet;  
import java.io.IOException;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
      
public class DelUserServlet extends HttpServlet {  
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
            String username=request.getParameter("username");  
            UserManager userManager=new UserManager();  
            userManager.del(username);  
            request.getRequestDispatcher("/del_success.jsp").forward(request, response);  
    }  
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
            doGet(request, response);  
    }  
}

UserManager

模仿邏輯處理層和數據處理層

package com.tgb.struts1.servlet;  
      
import java.util.ArrayList;  
import java.util.List;  
      
public class UserManager {  
    public void add(String username){  
        System.out.println("userManager------->add,username="+username);  
    }  
    public void del(String username){  
        System.out.println("userManager------->del,username="+username);  
    }  
    public void modify(String username){  
        System.out.println("userManager------->modify,username="+username);  
    }  
    public List query(String username){  
        System.out.println("userManager------->query,username="+username);  
        List userList=new ArrayList();  
        userList .add("a");  
        userList.add("b");  
        return userList;  
    }  
}

配置文件

<servlet>  
  <servlet-name>AddUserServlet</servlet-name>  
  <servlet-class>com.tgb.struts1.servlet.AddUserServlet</servlet-class>  
</servlet>  
      
<servlet-mapping>  
  <servlet-name>AddUserServlet</servlet-name>  
  <url-pattern>/servlet/AddUserServlet</url-pattern>  
</servlet-mapping>  
<servlet>  
  <servlet-name>DelUserServlet</servlet-name>  
  <servlet-class>com.tgb.struts1.servlet.DelUserServlet</servlet-class>  
</servlet>  
<servlet-mapping>  
  <servlet-name>DelUserServlet</servlet-name>  
  <url-pattern>/servlet/DelUserServlet</url-pattern>  
</servlet-mapping>  
  <servlet>  
  <servlet-name>ModifyUserServlet</servlet-name>  
  <servlet-class>com.tgb.struts1.servlet.ModifyUserServlet</servlet-class>  
</servlet>  
<servlet-mapping>  
  <servlet-name>ModifyUserServlet</servlet-name>  
  <url-pattern>/servlet/ModifyUserServlet</url-pattern>  
</servlet-mapping>  
<servlet>  
    <servlet-name>QueryUserServlet</servlet-name>  
  <servlet-class>com.tgb.struts1.servlet.QueryUserServlet</servlet-class>  
</servlet>  
<servlet-mapping>  
  <servlet-name>QueryUserServlet</servlet-name>  
  <url-pattern>/servlet/QueryUserServlet</url-pattern>  
</servlet-mapping>

總結

使用這種Servlet方式實現,很容易發現,存在多個問題 :

配置文件冗余

Servlet文件冗余,隨著功能的增多,Servlet文件越來越多,越難以整理分類

代碼冗余,增刪改查四個功能,需要有相當多的重復代碼,難以維護

第二版

針對上面發現的問題我們繼續修改上面的程序:針對第一版的主要的Servlet問題,然後使 用截取url地址的方式,減少Servlet數量。

文件樹

index.jsp

<form 

action="servlet/addUser.do" method="post">  
    姓名:<input type="text" name="username" /><br/>  
    <input type="submit" value="submit"/>   
</form>

Servlet

package com.tgb.struts1.servlet;  
      
import java.io.IOException;  
import java.util.List;  
      
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.swing.text.html.ListView;  
      
import org.apache.taglibs.standard.extra.spath.Path;  
      
public class TestServlet extends HttpServlet {  
      
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
            //get uri and path  
            String uri=request.getRequestURI();  
            String path=uri.substring(uri.indexOf("/",1),uri.indexOf("."));  
            //get username  
            String username=request.getParameter("username");  
            System.out.println(path);  
            UserManager userManager=new UserManager();  
            //forward to path with path  
            if ("/servlet/addUser".equals(path)) {  
                userManager.add(username);  
                request.getRequestDispatcher("/add_success.jsp").forward(request, response);  
            }else if ("/servlet/delUser".equals(path)) {  
                userManager.del(username);  
                request.getRequestDispatcher("/del_success.jsp").forward(request, response);  
            }else if ("/servlet/modifyUser".equals(path)) {  
                userManager.modify(username);  
                request.getRequestDispatcher("/modify_success.jsp").forward(request, response);  
            }else if ("/servlet/queryUser".equals(path)) {  
            List userList=  userManager.query(username);  
            request.setAttribute("userList", userList);  
                request.getRequestDispatcher("/query_success.jsp").forward(request, response);  
            }else {  
                throw new RuntimeException("not found this order!");  
            }  
    }  
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
            doGet(request, response);  
    }  
}

UserManager

同第一版

配置文件

<servlet>  
  <servlet-name>TestServlet</servlet-name>  
  <servlet-class>com.tgb.struts1.servlet.TestServlet</servlet-class>  
</servlet>  
<servlet-mapping>  
  <servlet-name>TestServlet</servlet-name>  
  <url-pattern>*.do</url-pattern>  
</servlet-mapping>

總結

第二版克服了Servlet文件過多、配置冗余和代碼冗余的問 題,但是這個版本的問題也很明顯:TestServlet中代碼重復,最大的一個問題就是:失去了靈活性,在 TestServlet中,使用過多字符串,無法再靈活添加其它功能,已經被我們“寫死”到代碼中。

第三 版

這一版我們嘗試對第二版進行精簡,增加它的靈活性。

文件樹

index.jsp

與 第二版相同

Servlet

package com.tgb.struts1.servlet;  
      
import java.io.IOException;  
import java.util.List;  
      
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.swing.text.html.ListView;  
      
import org.apache.taglibs.standard.extra.spath.Path;  
      
public class TestServlet extends HttpServlet {  
      
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        //get uri  
            String uri=request.getRequestURI();  
            String path=uri.substring(uri.indexOf("/",1),uri.indexOf("."));  
                  
            String username=request.getParameter("username");  
            System.out.println(path);  
            UserManager userManager=new UserManager();  
            String forward=null;   
            Action action=null;  
            if ("/servlet/addUser".equals(path)) {  
                action=new AddUserAction();  
            }else if ("/servlet/delUser".equals(path)) {  
                action=new DelUserAction();  
            }else if ("/servlet/modifyUser".equals(path)) {  
                action=new ModifyUserAction();  
            }else if ("/servlet/queryUser".equals(path)) {  
                action=new QueryUserAction();  
            }else {  
                throw new RuntimeException("not found this order!");  
            }  
            forward=action.execute(request, response);  
            request.getRequestDispatcher(forward).forward(request, response);  
    }  
      
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
            doGet(request, response);  
    }  
      
}

Action

package com.tgb.struts1.servlet;  
      
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
      
public interface Action {  
    public String execute(HttpServletRequest request,HttpServletResponse response);   
}

AddUserAction

package com.tgb.struts1.servlet;  
      
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
      
public class AddUserAction implements Action {  
      
    public String execute(HttpServletRequest request,  
            HttpServletResponse response) {  
        String username=request.getParameter("username");  
        UserManager userManager=new UserManager();  
        userManager.add(username);  
        return "/add_success.jsp";  
    }  
      
}

UserManager

與第一版相同

配置文件    與第二版相同

總結

這一版使用接口和實現接口的類,在一定程度上精簡了Servlet的代碼重復問題,同時也在一定程度上解決了 Servlet中寫的代碼“太死”的問題,但是在擴展性上還是有很大的問題。

總結

從上面的三個 版本中,我們抽象一下,在控制頁面跳轉中,我們需要解決冗余和靈活問題,都需要做些什麼?很明顯根據第 二版,我們知道在控制轉向的方向問題上我們可以使用url截取;結合第三版,我們知道在精簡頁面轉向類中 ,接口可以讓代碼更精簡。再結合以前所學,要使程序更靈活,當然還要把轉向信息寫到配置文件中。

我們再來抽象一下轉向的過程:轉向請求截取,轉向請求分析比對,對請求的數據和邏輯處理,跳轉 到指定頁……好吧,這些基本上已經是Struts1框架的基本內容。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved