实例讲解MVC-Model2实现

<p:colorscheme colors="#ffffff,#000000,#808080,#000000,#bbe0e3,#333399,#009999,#99cc00"> </p:colorscheme>
实例讲解MVC
我们以登录为例,讲解 MVC
模式的运用

<p:colorscheme colors="#ffffff,#000000,#808080,#000000,#bbe0e3,#333399,#009999,#99cc00"> </p:colorscheme>
文字说明
    客户进入登陆界面(视图),利用用户名和密码来登录系统,跳转到 Servlet (控 制器),通过 Servlet 传递参数来 JavaBean (模型), JavaBean 通过验证后,采 取跳转不同的界面,即没有查询到数据,则显示登录失败的页面(视图)。否则 通过权限值的不同跳转到不同的权限界面(视图)。


 分析设计


<p:colorscheme colors="#ffffff,#000000,#808080,#000000,#bbe0e3,#333399,#009999,#99cc00">
  1. 1.数据库设计
  2. 2.MVC模式的分离
  3. 设计模式的选用
  • 1.为不同的服务设计设计接口(接口模式)
  • 2.共享数据库链接池(享元模式)
  • 3. 产生抽象类型的对象(工厂模式)
  • 4. JDBC中的桥接模式
  • 5.产生特定的Bean(生成器模式)
  • 6.通过不同的异常和属性值控制导向不同的方向(策略模式)
<p:colorscheme colors="#ffffff,#000000,#808080,#000000,#bbe0e3,#333399,#009999,#99cc00"></p:colorscheme>


    数据库设计
      数据库名:company
      表名:user
      user的字段:
           username:varchar(20) primary key
           password:varchar(20)
           priority:int default 0 //优先级的最低为0

MVC模式的分离
控制器:com.eimhe.controler.LoginAction

模型:com.eimhe.model.User
            com.eimhe.model.Query
            com.eimhe.model.QueryBeanFactory   
           com,eimhe.model.ConnectionPool
视图:errors.jsp
           login.jsp
           nonresult.jsp
           welcome.jsp
           welcomeadmin.jsp
           welcomemanage.jsp


为不同的服务设计设计接口(接口模式)
    主要的设计业务方法:query(:HttpServletRequest):User
    接口:
java 代码
 
  1. package com.eimhe.model;  
  2.   
  3. import java.sql.SQLException;  
  4.   
  5. import javax.servlet.http.HttpServletRequest;  
  6.   
  7. import com.eimhe.exception.FieldLengthException;  
  8.   
  9. public interface Query {  
  10.     public User query(HttpServletRequest request)throws SQLException,FieldLengthException;  
  11. }  
其中 FieldLengthException的声明:
java 代码
 
  1. package com.eimhe.exception;  
  2.   
  3. public class FieldLengthException extends Exception {  
  4.     public FieldLengthException(String message)  
  5.     {  
  6.         super(message);  
  7.     }  
  8. }  



共享数据库链接池(享元模式)
    类名:com.eimhe.model.ConnectionPool
    方法:public static Connection getConnection() throws SQLException;
    代码:
java 代码
 
  1. package com.eimhe.model;  
  2. import java.sql.Connection;  
  3. import java.sql.DriverManager;  
  4. import java.sql.SQLException;  
  5.   
  6. public class ConnectionPool {  
  7.     public static Connection getConnection()throws SQLException  
  8.     {  
  9.         try  
  10.         {  
  11.             Class.forName("com.mysql.jdbc.Driver");  
  12.             return DriverManager.getConnection("jdbc:mysql://localhost:3306/company","root","123456");  
  13.         }catch(ClassNotFoundException e)  
  14.         {  
  15.             System.err.println(e);  
  16.         }  
  17.         throw new SQLException();  
  18.       
  19.     }  
  20. }  


    产生抽象类型的对象(工厂模式)
    利用工厂方法来实例化抽象对象,我们采取内置类的方式来实现接口对象
    public class QueryBeanFactory {
      public static Query createQuery()
      {
     return new Query()
      {
       …….
      };
     }
}
实现代码:
java 代码
 
  1. package com.eimhe.model;  
  2.   
  3. import java.sql.CallableStatement;  
  4. import java.sql.Connection;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7.   
  8. import javax.servlet.http.HttpServletRequest;  
  9.   
  10. import com.eimhe.exception.FieldLengthException;  
  11.   
  12. public class QueryBeanFactory {  
  13.     public static Query createQuery()  
  14.     {  
  15.         return new Query()  
  16.         {  
  17.             private Connection con;  
  18.             private CallableStatement call;  
  19.             private ResultSet resultSet;  
  20.             public User query(HttpServletRequest request)throws SQLException,FieldLengthException  
  21.             {  
  22.                 //JDBC桥接模式  
  23.                 con=ConnectionPool.getConnection();  
  24.                 call=con.prepareCall("");  
  25.                 resultSet=call.executeQuery();  
  26.                 User user=new User();  
  27.                 String username=request.getParameter("username");  
  28.                 String password=request.getParameter("password");  
  29.                 int uLen=username.length();  
  30.                 int pLen=password.length();  
  31.                 if(uLen==0 || uLen>20 || pLen==0 || pLen>20)  
  32.                 {  
  33.                     throw new FieldLengthException("Fields' length is error!");  
  34.                 }  
  35.                 if(!resultSet.next())  
  36.                 {  
  37.                     throw new SQLException("Sorry!query: non result!");  
  38.                 }  
  39.                 user.setUsername(username);  
  40.                 user.setPassword(password);  
  41.                 user.setPriority(resultSet.getInt("priority"));  
  42.                 return user;  
  43.             }  
  44.         };  
  45.     }  
  46. }  


<p:colorscheme colors="#ffffff,#000000,#808080,#000000,#bbe0e3,#333399,#009999,#99cc00"> </p:colorscheme>
JDBC中的桥接模式
  JDBC通过方法桥接对象:
DriverManager.getConnection() -> Connection.prepareCall()->CallableStatement.executeQuery()->ResultSet

产生特定的Bean(生成器模式)
    我们这里通过是用户名和密码的验证的方式来产生User对象,通过实现Query接口的query方法来实现User对象的生成。

通过不同的异常和属性值控制导向不同的方向
    若验证成功的话,通过优先数的高低定向到不同的页面。
    若验证有问题的话,没有结果的,抛出一个SQLException的异常,并传递信息。若是字符长度不符合的话,那么也会抛出一个FieldLengthException的异常。同样是说不同的异常有不同的页面给出!
LoginAction(控制器):
java 代码
 
  1. package com.eimhe.controler;  
  2.   
  3. import java.io.IOException;  
  4. import java.sql.SQLException;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import com.eimhe.exception.FieldLengthException;  
  12. import com.eimhe.model.Query;  
  13. import com.eimhe.model.QueryBeanFactory;  
  14. import com.eimhe.model.User;  
  15.   
  16. public class LoginAction extends HttpServlet {  
  17.   
  18.     /** 
  19.      * Constructor of the object. 
  20.      */  
  21.     public LoginAction() {  
  22.         super();  
  23.     }  
  24.   
  25.     /** 
  26.      * Destruction of the servlet. <br> 
  27.      */  
  28.     public void destroy() {  
  29.         super.destroy(); // Just puts "destroy" string in log  
  30.         // Put your code here  
  31.     }  
  32.   
  33.     /** 
  34.      * The doGet method of the servlet. <br> 
  35.      * 
  36.      * This method is called when a form has its tag value method equals to get. 
  37.      *  
  38.      * @param request the request send by the client to the server 
  39.      * @param response the response send by the server to the client 
  40.      * @throws ServletException if an error occurred 
  41.      * @throws IOException if an error occurred 
  42.      */  
  43.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  44.             throws ServletException, IOException {  
  45.   
  46.         doPost(request,response);  
  47.     }  
  48.   
  49.     /** 
  50.      * The doPost method of the servlet. <br> 
  51.      * 
  52.      * This method is called when a form has its tag value method equals to post. 
  53.      *  
  54.      * @param request the request send by the client to the server 
  55.      * @param response the response send by the server to the client 
  56.      * @throws ServletException if an error occurred 
  57.      * @throws IOException if an error occurred 
  58.      */  
  59.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  60.             throws ServletException, IOException {  
  61.         Query queryBean=QueryBeanFactory.createQuery();//工厂模式  
  62.         try  
  63.         {  
  64.             User user=queryBean.query(request);//生成器模式  
  65.             if(user.getPriority()==2)//策略模式  
  66.             {  
  67.                 response.sendRedirect("./welcomeadmin.jsp");  
  68.                 return;  
  69.             }  
  70.             else if(user.getPriority()==1)  
  71.             {  
  72.                 response.sendRedirect("./welcomemanager.jsp");  
  73.                 return;  
  74.             }  
  75.             response.sendRedirect("./welcome.jsp");  
  76.         }catch(FieldLengthException e)  
  77.         {  
  78.             System.err.println(e);  
  79.             response.sendRedirect("./nonresult.jsp");  
  80.         }catch(SQLException e)  
  81.         {  
  82.             System.err.println(e);  
  83.             response.sendRedirect("./nonresult.jsp");  
  84.         }  
  85.           
  86.     }  
  87.   
  88.     /** 
  89.      * Initialization of the servlet. <br> 
  90.      * 
  91.      * @throws ServletException if an error occure 
  92.      */  
  93.     public void init() throws ServletException {  
  94.         // Put your code here  
  95.     }  
  96.   
  97. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值