zoukankan      html  css  js  c++  java
  • 用JSP+Servlet+JavaBean实现MVC设计模式的流程

    MVC是三个单词的缩写:M:Model(模型);V:View( 视图 ),C:Controller(控制器)。

    MVC模式的目的就是实现Web系统的职能分工,
    Model层:实现系统的业务逻辑,即javaBean部分
    View层:负责与用户交互,即在界面上展示数据对象给用户,即html,jsp
    Controller层:Model与View之间沟通的桥梁,它可以分派用户的请求并选择恰当的视图以用于显示,同时它也可以解释用户的输入并将它们映射为模型层可执行的操作,当然就是Servlet的职责了

    下面用MVC设计模式来实现 简单的用户登录过程
    1、控制器Servlet的实现

    系统中只有一个servlet即ControlServlet,所有页面发起的以” *.do “的请求,都被web.xml配置给ControlServlet进行处理,在ControlServlet中根据‘ * ’的字符串(即解析用户请求的路径),调用ActionFactory生成的制定Action对象,在将处理后的URL转发给用户。

    package cn.netjava.servlet;   
    import java.io.IOException;  
    import javax.servlet.ServletException;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
     
    import cn.netjava.action.Action;  
    import cn.netjava.action.ActionFactory;  
     
    /** 
     * Servlet implementation class ControlServlet 
     */  
    public class ControlServlet extends HttpServlet {  
        private static final long serialVersionUID = 1L;  
     
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
            //得到当前Servlet的请求路径  
            String pathName =request.getServletPath();  
            // request.getContextPath();得到项目名字  
            System.out.println("pathName:"+pathName);  
            //得到请求的Action名字  
            int index = pathName.indexOf(".");  
            String ActionName = pathName.substring(1, index);  
            System.out.println(ActionName);  
            //获取运行时参数  
            String ActionClassName = this.getInitParameter(ActionName);  
            //得到Action对象  
            Action action = ActionFactory.getActionFactory().getAction(ActionClassName);  
            //执行Action的execute得到要返回的URL路径  
            String url = action.execute(request, response);  
            if(url==null){  
                request.getRequestDispatcher("error.jsp").forward(request, response);     
            }else{  
                request.getRequestDispatcher(url).forward(request, response);  
            }  
        }  
     
    }

    2、Action对象工厂类实现:

    ActionFactory是一个单实例类(整个系统只需要使用其一个对象),它只提供一个Action对象,通过getAction(String ActionClassName) 的方法调用创建一个Action对象。这个方法在Control中被调用。代码如下:

    package cn.netjava.action;  
    /** 
     * 根据Action名字,创建Action对象 
     * @author Administrator 
     * 
     */  
    public class ActionFactory {  
     
        //单例模式:不需要创建对象  
        private ActionFactory(){  
        }  
        //单实例访问方法,得到ActionFactory对象  
        public static ActionFactory getActionFactory(){  
            if(af == null){  
                af = new ActionFactory();  
            }  
            return af;  
        }  
        /** 
         * 根据具体的Action类名字创建Action对象 
         * @param ActionClassName :具体的Action类全名 
         * @return:Action类型对象 
         */  
        public Action getAction(String ActionClassName){  
            Action action = null;  
            try{  
                action = (Action) Class.forName(ActionClassName).newInstance();  
            }catch(Exception e){  
                e.printStackTrace();  
            }  
            return action;  
        }  
     
        private static ActionFactory af;  
     
     
    }

    3、Action接口类定义:
    所有的事件处理(即Action)类都必须实现这个接口

    package cn.netjava.action;  
     
    public interface Action {  
        /** 
         * 所有的具体Action实现这个接口 
         * @param request 请求对象 
         * @param response 应答对象 
         * @return :结果页面 
         */  
        public String execute(javax.servlet.http.HttpServletRequest request,javax.servlet.http.HttpServletResponse response);  
     
    }

    4、web.xml中配置请求发送给控制器Servlet

    最后,只需要在wex.xml中对MVC结构的配置:
    视图页面中的请求都是以<动作名字>.do结尾,当这个请求到达web服务器后,会被服务器转向给控制器处理,控制器在根据解析出的动作名,调用对应的Action对象,处理结果,并输出结果页面,所以web.xml中必须有如下配置:

    <servlet>  
        <servlet-name>controlServlet</servlet-name>  
        <servlet-class>cn.netjava.servlet.ControlServlet</servlet-class>  
        <init-param>  
            <param-name>loginAction</param-name>  
            <param-value>cn.netjava.action.LoginAction</param-value>  
        </init-param>  
      </servlet>  
      <servlet-mapping>  
        <servlet-name>controlServlet</servlet-name>  
        <url-pattern>*.do</url-pattern>  
      </servlet-mapping>

    4、具体的Action类实现(即对登录动作进行处理的类)

    package cn.netjava.action;  
     
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
     
    public class LoginAction implements Action {  
     
        public String execute(HttpServletRequest request,  
                HttpServletResponse response) {  
            // 得到用户名和密码  
            String userName = request.getParameter("userName");  
            String userPwd = request.getParameter("userPwd");  
            if (userName.equals("netjava") && userPwd.equals("netjava")) {  
                request.setAttribute("userName", userName);  
                return "main.jsp";  
            } else {  
                return "login.jsp";  
            }  
        }  
     
    }

    如果登录成功,跳转到 main.jsp页面,否则,返回login,jsp页面

    以下是main.jsp和login.jsp页面:
    main.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"  
        pageEncoding="utf-8"%>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    <title>Insert title here</title>  
    </head>  
    <body>  
    <h1 style="color:red"><%=request.getAttribute("userName") %>登录成功</h1>  
    </body>  
    </html>

    login.jsp

    <%@ page language="java" contentType="text/html; charset=utf-8"  
        pageEncoding="utf-8"%>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    <title>用户登录</title>  
    </head>  
    <body>  
    <form action="loginAction.do" method="post">  
        用户名:<input type="text" name="userName" id="userName"><br>  
        密码:<input type="password" name="userPwd" id="userPwd"><br>  
        <input type="submit" value="登录"/>  
    </form>  
    </body>  
    </html>

    最后做个总结吧:以前我们与服务器进行交互,可能jsp页面和servlet中都将html和java代码参杂在一起,这会导致系统的系统维护困难、分工不清;例如在加有jsp代码段的网页中,程序员与美工之间的配合就非常困难!MVC结构的系统会从根本上强制我们将web系统中的数据对象、业务逻辑、用户界面三者分离,使得程序员(Java开发人员)集中精力于业务逻辑,界面程序员(HTML和JSP开发人员)集中精力于表现形式上。

    注:文章非博主原创,仅为学习备用

  • 相关阅读:
    SP笔记:交叉实现七行并成一行
    HTML tag 学习
    操作哈希表
    Efficient bipedal robots based on passivedynamic walkers
    Pushing People Around
    ZEROMOMENT PONTTHIRTY FIVE YEARS OF ITS LIFE

    Active Learning for RealTime Motion Controllers
    Accelerometerbased User Interfaces for the Control of a Physically Simulated Character
    Dynamic Response for Motion Capture Animation
  • 原文地址:https://www.cnblogs.com/liuxgnu/p/3535458.html
Copyright © 2011-2022 走看看