自己做MVC框架(以登录页面为例)

自己做MVC框架(以登录页面为例)
1,首先建立一个类名为Login的类文件,在类里建一个名为login的方法,要向这个方法里传俩个值,一个为用户名,另一个为密码。
Login.java类代码如下:(model层)


package com.biz;  //我的包为在com.biz
public class Login {
	public boolean login(String uname,String upass){
		boolean b = false;
		if("123".equals(uname) && "123".equals(upass)){
			b= true;
		}//在这里是做出判断
		return b;
	}
}




2,建好方法类后,接下来建立一个action(控制器),建立一个名为LoginAction的class文件,在这里主要功能是接收页面上传来的参数,然后进行处理,放到login(String uname,String upass)方法里。
然后进行判断,然后根据判断结果转入到不同的页面里。
LoginAction.java类代码如下:(控制器)


package com.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.action.imp.Action;
import com.biz.Login;


public class LoginAction{
	private Login login = new Login();
	public String excute(HttpServletRequest request,
			HttpServletResponse response) {
		String uname=request.getParameter("uname");
		String upass = request.getParameter("upass");
		boolean b = false;
		b=login.login(uname, upass);
		if(b){
			request.setAttribute("user", uname);
			return "login_success.jsp";
		}else{
			request.setAttribute("error", "用户名或密码错误!");
			return "index.jsp";
		}
	}
}




3,建立好控制层后然后建立view层,也就是视图层。就是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 'index.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">
</head>
<body>
	${null==requestScope.error?"":requestScope.error }   //主要用来返回给页面一个结果,显示登录成功或不成功
	<form action="login.do" method="post">
		用户名:<input type="text" name="uname"><br> 密 码:<input
			type="password" name="upass"><br> <input type="submit"
			value="登录">
	</form>
</body>
</html>





4,有这个登录页,还需要一个显示登录成功的页面
在这里简单的建立一个login_success.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_success.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">
  </head>
  <body>
   <h1>登录成功!</h1>亲,${requestScope.user }  //用EL表达式时,用requestScope调用变量显示结果
  </body>
</html>






5,有了这些是不是觉得就完了?如果你觉得完了的话就去把程序发布然后在浏览器访问,访问地址为:http://localhost:8080/项目名/login.do
即可访问到!
但是这必须出现问题,因为这仅仅不够!那么访问肯定会出现404错误!
必须要建立一个专门截获*.do文件的Servlet,是把login.do截获,然后进行处理
Controller.java代码如下:


package com.action;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.action.imp.Action;
/**
 * 自定义MVC框架:基于Servlet实现的控制器
 */
public class Controller extends HttpServlet {
	private HashMap actionMap = new HashMap();
	/**
	 * Servlet初始化方法。
	 */
	public void init() throws ServletException {
		actionMap = new HashMap();
		actionMap.put("login", new LoginAction());  //这里的"login"和form表单里的action="" 对应的login.do必须一致
		//new LoginAction()是对应刚才建立的LoginAction,也就是控制层
	} 
	/**
	 * 根据path判断由哪个action执行操作。
	 */
	private Action determinActionByPath(String path) {
		String actionName = 
			path.substring(path.lastIndexOf('/') + 1, path.length() - 3);  //这里是在截字符串,是要把地址里的login.do截获
		Action ret = (Action)actionMap.get(actionName); 
		return ret;
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 得到path,诸如:http://localhost:8080/epet/ShowBaby.do
		String path = request.getServletPath();	
		// 找出Action
		Action action = (Action)this.determinActionByPath(path);				
		// 执行操作
		String resultView = action.excute(request,response);	
		// 控制页面转向
		if (null!=resultView){
			request.getRequestDispatcher(resultView).forward(request, response);
		}		
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}
	private static final long serialVersionUID = -2269577614162154800L;
}



6,接下来在web.xml里进行配置


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>
  <servlet>
    <servlet-name>Controller</servlet-name>
    <servlet-class>com.action.Controller</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



web.xm里你要确定你的Controller类的地址正确!
7,弄完这些,现在就完成了!发布程序,然后访问地址http://localhost:8080/项目名/login.do
然后就成功了!

    
        

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2013-03-07 18:16  一直向北  阅读(624)  评论(0编辑  收藏  举报