码迷,mamicode.com
首页 > Web开发 > 详细

MVC模式

时间:2014-10-21 23:07:13      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   ar   使用   java   for   

bubuko.com,布布扣


模式1把业务代码从JSP页面中分离了出去,减少了JSP的Java代码量,但在JSP页面中还有一些处理控制的Java代码。如果项目规模大,业务复杂,可能JSP中的处理控制的Java代码就会很杂乱。为了解决这个问题,可以把Servlet和 JSP结合起来,用Servlet接收用户提交的请求,调用业务方法,再转发给JSP页面显示结果 。以上所说的结构称为模式2。

模式2是一种MVC模式。MVC模式分为3层:业务层(Model)是进行业务处理的,表示层(View)是用来与用户交互的,控制层(Control)是进行流程控制的。


任务:查询一个表的数据

1,建立一个数据库表team。[id:int,key,autoincrement;name:varchar:slogan:varcha]。安装MySql=>打开Navicat=>在test数据库中建立team表,任意输入几个记录。

2.安装Tomcat7=>打开Eclipse->建立动态Web工程=>在Eclipse内部配置Tomcat7=>试验一个jsp以证明开发环境正常。

3.由于网站要用到Mysql,所以要将MySql的Java驱动放到Tomcat的Lib目录中,还要导入servlet-api.jar包到开发环境中。

4.做一个数据库的连接类database.DataSource,它的功能是连接到数据库,返回连接对象。以后要使用连接数据库只需调用下面的getConnection即可。

package database;

import java.sql.Connection;

import java.sql.DriverManager;

public class DataSource {

    public static Connection getConnection() throws Exception{

       Class.forName("com.mysql.jdbc.Driver");

       String       url="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";

       String user="root";

       String password="11";

       Connection cn=DriverManager.getConnection(url,user,password);

       return cn;

    }

}

4.做一个JavaBean:Team用来对应数据库表team的每一条记录。放在bean.Team中。

package bean;

public class Team {

    private int id;

    private String name;

    private String slogan;

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public String getSlogan() {

       return slogan;

    }

    public void setSlogan(String slogan) {

       this.slogan = slogan;

    }

   

}

5,完成业务类business.TeamBusiness,其中写一个查询功能。

public static Collection<Team> allTeams() throws Exception{

//泛型,返回的是一个元素类型为Team的集合

         ArrayList<Team> teams=new ArrayList<Team>();

         Connection cn=null;

         Statement stmt=null;

         ResultSet rs=null;

         try{

                cn=DataSource.getConnection();

                stmt=cn.createStatement();

                rs=stmt.executeQuery("select * from team");

                while(rs.next()){

                       Team team=new Team();

                       team.setName(rs.getString("name"));

                       team.setSlogan(rs.getString("slogan"));

                       teams.add(team);

                }

         }catch(Exception e){

                e.printStackTrace();

         }

         return teams;

  }

6,建立并调试Servlet。ViewTeam。

package servlet;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * Servlet implementation class ViewTeam

 */

@WebServlet("/ViewTeam")

public class ViewTeam extends HttpServlet {

       private static final long serialVersionUID = 1L;

      

    /**

     * @see HttpServlet#HttpServlet()

     */

    public ViewTeam() {

        super();

        // TODO Auto-generated constructor stub

    }

 

       /**

        * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

        */

       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

              // TODO Auto-generated method stub

       }

       /**

        * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

        */

       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

              // TODO Auto-generated method stub

       }

 

}

在web.xml中的配置为:

<servlet>

    <servlet-name>ViewTeam</servlet-name>

    <servlet-class>servlet.ViewTeam</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>ViewTeam</servlet-name>

    <url-pattern>/servlet/viewTeam</url-pattern>

  </servlet-mapping>

7,修改ViewTeam类,完成调度和转发工作:

try{

           Collection<Team> teams=TeamBusiness.allTeams();

           request.setAttribute("teams", teams);

           RequestDispatcher rd=request.getRequestDispatcher("../mod2ViewTeams.jsp");

// RequestDispatcher是javax.servlet的转发处理对象

           rd.forward(request, response);

       }catch(Exception e){

           e.printStackTrace();

       }

8,完成jsp显示:

Collection<Team> teams=(Collection<Team>)request.getAttribute("teams");

    Iterator<Team> it=teams.iterator();

    while(it.hasNext()){

       Team team=it.next();

       %>

       <p><%=team.getName() %>:<%=team.getSlogan() %>

       <%

}

MVC模式

标签:style   blog   http   io   os   ar   使用   java   for   

原文地址:http://www.cnblogs.com/huchap/p/4041740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!