MVC开发模式的流程分析

及得在之前的一片博客中,已经把MVC说的我认为比较清楚了,但是作为我本人来讲,绝不敢说自己这MVC这种模式是精通的,但是,自我的理解和认识我觉得还算是可以的,之前我由于中午太困,好像那一张图画的并不完整,深感歉意,MVC这种思想类的东西,无关语言,无关具体技术,我的理解是一种开发思想,至于思想类的东西,学的好不好,关键是思想的理解。

首先,在先简单的再说一下MVC的基础架构,我尽量以最通俗的方式来说:

我们有时候会听到web-service或者web-service-dao等,那么问题来了,这些都是什么鬼,其实说白了吧,这两者都是mvc的架构,为什么这样说呢,如前者web-service,这就好理解了,这一个应用架构中,主要分三种东西,1:Model模型,2:View视图,3:Controller:控制器,说到这,你应该就明白mvc的又来了吧,在View和Controller一块又被叫做web层,而在MOdel里面:这个也是mvc中最为复杂的,Model有,数据对象,对象的service,dao,持久层等,而在web-service中model只有数据对象与service构成业务层,业务逻辑由控制器调用service的方法操作数据库,所以叫做web-service的mvc开发模式,至于web-service-dao开发模式,这个无非就是在数据对象和service之间再抽出的一层,因为有的项目肯可能会操作复杂的数据关系,与其这样,不如把纯粹操作某一个表的方法放到dao中,我这里相信已经说的很清楚了,不再赘述,而service在去执行dao方法,同样的最后给控制器调用,

下面我做了一份简单的实例:

MVC开发模式的流程分析_第1张图片

下面是单一登录流程的代码:

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'login.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    

This is Login Index Page

Username:

Password:


LoginController.java

package Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Model.AdminModel;
import Model.AdminService;

public class LoginController extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String Username=request.getParameter("username");
		String Password=request.getParameter("password");
		out.print("

获取到的用户名:"+Username+"

"); out.print("

获取到的密码是:"+Password+"

"); AdminModel loginuser=new AdminModel(); loginuser.setUsername(Username); loginuser.setPassword(Password); if(AdminService.CheckLogin(loginuser)){ out.print("

登录成功

"); request.getRequestDispatcher("/WEB-INF/ShowMain.jsp").forward(request, response); }else{ out.print("

登录失败

"); request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); doGet(request, response); } }

数据对象:AdminModel.java

package Model;

public class AdminModel {
	/*
	 * This is Admin Object
	 */
	private String  Username;
	private String Password;
	public String getUsername() {
		return Username;
	}
	public void setUsername(String username) {
		Username = username;
	}
	public String getPassword() {
		return Password;
	}
	public void setPassword(String password) {
		Password = password;
	}
}
AdminService.java:

package Model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Tools.Md5Tools;
/*
 * 获取全部的用户资源-用户名和密码
 */
public class AdminService {
	private static ArrayList AdminArrayList=new ArrayList();
	private static Connection conn=null;
	private static PreparedStatement ps=null;
	private static ResultSet res=null;
	public AdminService(){
		//必须重新清空
		AdminArrayList=null;
	}
	public static ArrayList GetAdminSource(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("加载驱动错误");
		}
		try {
			
			conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/empmanage","root","toor");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("数据库连接错误");
		}
		try {
			ps=conn.prepareStatement("select * from admin");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("Sql语句预编译");
		}
		try {
			res=ps.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("执行sql语句错误");
		}
		try {
			
			while(res.next()){
				AdminModel obj=new AdminModel();
				obj.setUsername(res.getString("name"));
				obj.setPassword(res.getString("password"));
				AdminArrayList.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("关系对象转化错误");
		}
		try {
			res.close();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return AdminArrayList;
	}
	//验证登录的用户名密码是否正确
	public static Boolean CheckLogin(AdminModel obj){
		ArrayList p =new ArrayList();
		
		p=AdminService.GetAdminSource();
		for(AdminModel TemObj:p){
			//以MD5的方式验证密码
			String temp_password=Md5Tools.GetMd5String(obj.getPassword());
			
			if(obj.getUsername().equals(TemObj.getUsername())&&temp_password.equals(TemObj.getPassword())){
				return true;
			}else{
				continue;
			}
		}
		return false;
	}
}



你可能感兴趣的:(java,ee)