自定义 mvc

来源:互联网 发布:java.io.eofexception 编辑:程序博客网 时间:2024/04/29 18:44
实现MVC自定义框架(简单实现登录功能) 
一,模型层: 
实体类,接口(实体类接口,Action接口),接口实现类(实体类接口实现类,Action接口实现类) 
代码: 
Java代码  收藏代码
  1. package entity;  
  2. /** 
  3.  * 用户类 
  4.  * @author Administrator 
  5.  */  
  6. public class User {  
  7.     private String username;//用户名  
  8.     private String password;//密码  
  9.     public String getUsername() {  
  10.         return username;  
  11.     }  
  12.     public void setUsername(String username) {  
  13.         this.username = username;  
  14.     }  
  15.     public String getPassword() {  
  16.         return password;  
  17.     }  
  18.     public void setPassword(String password) {  
  19.         this.password = password;  
  20.     }  
  21.       
  22. }  
  23. package dao;  
  24.   
  25. import javax.servlet.http.HttpServletRequest;  
  26. import javax.servlet.http.HttpServletResponse;  
  27.   
  28. /** 
  29.  * Action接口 
  30.  * @author Administrator 
  31.  * 
  32.  */  
  33. public interface Action {  
  34.     public String execute(HttpServletRequest request,HttpServletResponse response);  
  35. }  
  36. package dao;  
  37.   
  38. import entity.User;  
  39.   
  40. /** 
  41.  * 用户类接口 
  42.  * @author Administrator 
  43.  */  
  44. public interface UserDao {  
  45.     //登录方法  
  46.     public User login(String username,String password);  
  47. }  
  48. package dao.impl;  
  49.   
  50. import dao.UserDao;  
  51. import entity.User;  
  52. /** 
  53.  * 用户接口的实现类 
  54.  * @author Administrator 
  55.  */  
  56. public class UserDaoImpl implements UserDao {  
  57.     /** 
  58.      * 登录 
  59.      */  
  60.     public User login(String username, String password) {  
  61.         User user=null;  
  62.         if(username.equals("admin")&&password.equals("admin")){  
  63.             user=new User();  
  64.             user.setPassword(password);  
  65.             user.setUsername(username);  
  66.         }  
  67.         return user;  
  68.     }  
  69.   
  70. }  
  71. package dao.impl;  
  72.   
  73. import javax.servlet.http.HttpServletRequest;  
  74. import javax.servlet.http.HttpServletResponse;  
  75.   
  76. import dao.Action;  
  77. import entity.User;  
  78. /** 
  79.  * 登录Action类实现Action接口 
  80.  * @author Administrator 
  81.  */  
  82. public class LoginAction implements Action {  
  83.   
  84.     public String execute(HttpServletRequest request,  
  85.             HttpServletResponse response) {  
  86.             //接收参数  
  87.         String username=request.getParameter("username");  
  88.         String password=request.getParameter("password");  
  89.         //创建UserDaoImpl对象  
  90.         UserDaoImpl udi=new UserDaoImpl();  
  91.         //调用登录方法  
  92.         User user=udi.login(username, password);  
  93.         //判断 User对象是否为空  
  94.         if(user!=null){  
  95.             return "success.jsp";  
  96.         }else{  
  97.             return "error.jsp";  
  98.               
  99.         }  
  100.     }  
  101.   
  102. }  

二、控制器层,ActionServlet类(添加一个获得Action的方法) 
web.xml:配置如下 
Java代码  收藏代码
  1.  <servlet-name>ActionServlet</servlet-name>  
  2.   <servlet-class>servlet.ActionServlet</servlet-class>  
  3. </servlet>  
  4.   
  5. <servlet-mapping>  
  6.   <servlet-name>ActionServlet</servlet-name>  
  7.   <url-pattern>*.action</url-pattern><!-- 过滤所有以.action结尾 -->  
  8. </servlet-mapping>  

Java代码  收藏代码
  1. package servlet;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.   
  10. import dao.Action;  
  11. import dao.impl.LoginAction;  
  12.   
  13. public class ActionServlet extends HttpServlet {  
  14.   
  15.     /** 
  16.      * The doGet method of the servlet. <br> 
  17.      * 
  18.      * This method is called when a form has its tag value method equals to get. 
  19.      *  
  20.      * @param request the request send by the client to the server 
  21.      * @param response the response send by the server to the client 
  22.      * @throws ServletException if an error occurred 
  23.      * @throws IOException if an error occurred 
  24.      */  
  25.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  26.             throws ServletException, IOException {  
  27.   
  28.         doPost(request,response);  
  29.     }  
  30.   
  31.     /** 
  32.      * The doPost method of the servlet. <br> 
  33.      * 
  34.      * This method is called when a form has its tag value method equals to post. 
  35.      *  
  36.      * @param request the request send by the client to the server 
  37.      * @param response the response send by the server to the client 
  38.      * @throws ServletException if an error occurred 
  39.      * @throws IOException if an error occurred 
  40.      */  
  41.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  42.             throws ServletException, IOException {  
  43.         //得到Action对象  
  44.         Action action=getAction(request);  
  45.         //调用Action实现类方法  
  46.         try {  
  47.             String result=action.execute(request, response);  
  48.             if(result!=null){  
  49.                 response.sendRedirect(result);  
  50.             }  
  51.         } catch (Exception e) {  
  52.             // TODO Auto-generated catch block  
  53.             e.printStackTrace();  
  54.         }  
  55.           
  56.           
  57.     }  
  58.     /** 
  59.      * 创建一个获得Action的方法 
  60.      * @param request 
  61.      */  
  62.     public  static Action getAction(HttpServletRequest request){  
  63.         Action action=null;  
  64.         //截取action请求名(例如页面的登录:<form action="login.action" method="post">)  
  65.         String uri=request.getRequestURI();  
  66.         System.out.println("request.getRequestURI()---"+uri);  
  67.         //结果:request.getRequestURI()---/qifei_mvc_kuangjia_zuoye/login.action  
  68.         StringBuffer url=request.getRequestURL();  
  69.         System.out.println("request.getRequestURL()--"+url);  
  70.         //结果:request.getRequestURL()--http://localhost:8080/qifei_mvc_kuangjia_zuoye/login.action  
  71.         String contextPath=request.getContextPath();  
  72.         //结果:request.getContextPath()--/qifei_mvc_kuangjia_zuoye  
  73.         System.out.println("request.getContextPath()--"+contextPath);  
  74.         String actionUrl=uri.substring(contextPath.length());  
  75.         System.out.println("actionUrl--"+actionUrl);  
  76.         String actionName=actionUrl.substring(1,actionUrl.lastIndexOf(".")).trim();  
  77.           
  78.         //判断请求名  
  79.     if(actionName.equals("login")){  
  80.             action=new LoginAction();  
  81.         }  
  82.         return action;  
  83.     }  
  84.   
  85. }  

三、页面 
Java代码  收藏代码
  1. <body>  
  2.    <form action="login.action" method="post">  
  3.     用户名:<input type="text" name="username"/><br/>  
  4.     密码:<input type="password" name="password"/><br/>  
  5.     <input type="submit" value="提交">  
  6.    </form>  
  7.   </body>  
0 0