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方法,同样的最后给控制器调用,

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


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

login.jsp

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h3>This is Login Index Page</h3>
    <form action="/EmpManagerSystem/LoginController" method="post">
	    <h4>Username:<input type="text" name="username" ></h4>
	    <h4>Password:<input type="text" name="password"></h4>
	    <h4><input type="submit" value="Login"></h4>
	</form>
  </body>
</html>

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("<h3>获取到的用户名:"+Username+"</h3>");
		out.print("<h3>获取到的密码是:"+Password+"</h3>");
		
		AdminModel loginuser=new AdminModel();
		loginuser.setUsername(Username);
		loginuser.setPassword(Password);
		if(AdminService.CheckLogin(loginuser)){
			out.print("<h3>登录成功</h3>");
			request.getRequestDispatcher("/WEB-INF/ShowMain.jsp").forward(request, response);
		}else{
			out.print("<h3>登录失败</h3>");
			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<AdminModel> AdminArrayList=new ArrayList<AdminModel>();
	private static Connection conn=null;
	private static PreparedStatement ps=null;
	private static ResultSet res=null;
	public AdminService(){
		//必须重新清空
		AdminArrayList=null;
	}
	public static ArrayList<AdminModel> 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<AdminModel> p =new ArrayList<AdminModel>();
		
		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;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值