ITeye博客

实例讲解MVC-Model2实现

mercyblitz · 2007-04-11 10:26 · Java EE · Markdown

<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. import java.sql.SQLException;
  3. import javax.servlet.http.HttpServletRequest;
  4. import com.eimhe.exception.FieldLengthException;
  5. public interface Query {
  6. public User query(HttpServletRequest request)throws SQLException,FieldLengthException;
  7. }

其中FieldLengthException的声明:

java 代码

 

  1. package com.eimhe.exception;
  2. public class FieldLengthException extends Exception {
  3. public FieldLengthException(String message)
  4. {
  5. super(message);
  6. }
  7. }

共享数据库链接池(享元模式)
    类名: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. public class ConnectionPool {
  6. public static Connection getConnection()throws SQLException
  7. {
  8. try
  9. {
  10. Class.forName("com.mysql.jdbc.Driver");
  11. return DriverManager.getConnection("jdbc:mysql://localhost:3306/company","root","123456");
  12. }catch(ClassNotFoundException e)
  13. {
  14. System.err.println(e);
  15. }
  16. throw new SQLException();
  17. }
  18. }

    产生抽象类型的对象(工厂模式)
    利用工厂方法来实例化抽象对象,我们采取内置类的方式来实现接口对象
    public class QueryBeanFactory {
      public static Query createQuery()
      {
     return new Query()
      {
       …….
      };
     }
}
实现代码:

java 代码

 

  1. package com.eimhe.model;
  2. import java.sql.CallableStatement;
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import javax.servlet.http.HttpServletRequest;
  7. import com.eimhe.exception.FieldLengthException;
  8. public class QueryBeanFactory {
  9. public static Query createQuery()
  10. {
  11. return new Query()
  12. {
  13. private Connection con;
  14. private CallableStatement call;
  15. private ResultSet resultSet;
  16. public User query(HttpServletRequest request)throws SQLException,FieldLengthException
  17. {
  18. //JDBC桥接模式
  19. con=ConnectionPool.getConnection();
  20. call=con.prepareCall("");
  21. resultSet=call.executeQuery();
  22. User user=new User();
  23. String username=request.getParameter("username");
  24. String password=request.getParameter("password");
  25. int uLen=username.length();
  26. int pLen=password.length();
  27. if(uLen==0 || uLen>20 || pLen==0 || pLen>20)
  28. {
  29. throw new FieldLengthException("Fields' length is error!");
  30. }
  31. if(!resultSet.next())
  32. {
  33. throw new SQLException("Sorry!query: non result!");
  34. }
  35. user.setUsername(username);
  36. user.setPassword(password);
  37. user.setPriority(resultSet.getInt("priority"));
  38. return user;
  39. }
  40. };
  41. }
  42. }

<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. import java.io.IOException;
  3. import java.sql.SQLException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import com.eimhe.exception.FieldLengthException;
  9. import com.eimhe.model.Query;
  10. import com.eimhe.model.QueryBeanFactory;
  11. import com.eimhe.model.User;
  12. public class LoginAction extends HttpServlet {
  13. /**
  14. * Constructor of the object.
  15. */
  16. public LoginAction() {
  17. super();
  18. }
  19. /**
  20. * Destruction of the servlet. <br>
  21. */
  22. public void destroy() {
  23. super.destroy(); // Just puts "destroy" string in log
  24. // Put your code here
  25. }
  26. /**
  27. * The doGet method of the servlet. <br>
  28. *
  29. * This method is called when a form has its tag value method equals to get.
  30. *
  31. * @param request the request send by the client to the server
  32. * @param response the response send by the server to the client
  33. * @throws ServletException if an error occurred
  34. * @throws IOException if an error occurred
  35. */
  36. public void doGet(HttpServletRequest request, HttpServletResponse response)
  37. throws ServletException, IOException {
  38. doPost(request,response);
  39. }
  40. /**
  41. * The doPost method of the servlet. <br>
  42. *
  43. * This method is called when a form has its tag value method equals to post.
  44. *
  45. * @param request the request send by the client to the server
  46. * @param response the response send by the server to the client
  47. * @throws ServletException if an error occurred
  48. * @throws IOException if an error occurred
  49. */
  50. public void doPost(HttpServletRequest request, HttpServletResponse response)
  51. throws ServletException, IOException {
  52. Query queryBean=QueryBeanFactory.createQuery();//工厂模式
  53. try
  54. {
  55. User user=queryBean.query(request);//生成器模式
  56. if(user.getPriority()==2)//策略模式
  57. {
  58. response.sendRedirect("./welcomeadmin.jsp");
  59. return;
  60. }
  61. else if(user.getPriority()==1)
  62. {
  63. response.sendRedirect("./welcomemanager.jsp");
  64. return;
  65. }
  66. response.sendRedirect("./welcome.jsp");
  67. }catch(FieldLengthException e)
  68. {
  69. System.err.println(e);
  70. response.sendRedirect("./nonresult.jsp");
  71. }catch(SQLException e)
  72. {
  73. System.err.println(e);
  74. response.sendRedirect("./nonresult.jsp");
  75. }
  76. }
  77. /**
  78. * Initialization of the servlet. <br>
  79. *
  80. * @throws ServletException if an error occure
  81. */
  82. public void init() throws ServletException {
  83. // Put your code here
  84. }
  85. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。