---
title: "MVC_自定义框架"
author: "书安然"
date: "2014-10-31T18:20:47Z"
tags: ["MVC", "框架"]
categories: ["企业架构"]
slug: "mvc"
draft: false
lastmod: "2014-10-31T18:20:47Z"
id: 2150791
url: "https://amflytogether.iteye.com/blog/2150791"
---

\[color=red]\[size=large]实现MVC自定义框架（简单实现登录功能）\[/size]\[/color] \[color=red]一，模型层：\[/color] 实体类，接口（实体类接口，Action接口），接口实现类（实体类接口实现类，Action接口实现类） 代码： \[code="java"] package entity; /\** * 用户类 * @author Administrator \*/ public class User { private String username;//用户名 private String password;//密码 public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } package dao; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /\** * Action接口 * @author Administrator * \*/ public interface Action { public String execute(HttpServletRequest request,HttpServletResponse response); } package dao; import entity.User; /\** * 用户类接口 * @author Administrator \*/ public interface UserDao { //登录方法 public User login(String username,String password); } package dao.impl; import dao.UserDao; import entity.User; /\** * 用户接口的实现类 * @author Administrator \*/ public class UserDaoImpl implements UserDao { /\** * 登录 \*/ public User login(String username, String password) { User user=null; if(username.equals("admin")&&password.equals("admin")){ user=new User(); user.setPassword(password); user.setUsername(username); } return user; } } package dao.impl; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.Action; import entity.User; /\** * 登录Action类实现Action接口 * @author Administrator \*/ public class LoginAction implements Action { public String execute(HttpServletRequest request, HttpServletResponse response) { //接收参数 String username=request.getParameter("username"); String password=request.getParameter("password"); //创建UserDaoImpl对象 UserDaoImpl udi=new UserDaoImpl(); //调用登录方法 User user=udi.login(username, password); //判断 User对象是否为空 if(user!=null){ return "success.jsp"; }else{ return "error.jsp"; } } } \[/code] \[color=red]二、控制器层，ActionServlet类（添加一个获得Action的方法）\[/color] web.xml:配置如下 \[code="java"] ActionServlet servlet.ActionServlet ActionServlet \*.action \[/code] \[code="java"] package 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 dao.Action; import dao.impl.LoginAction; public class ActionServlet extends HttpServlet { /\** * The doGet method of the servlet.  
\* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred \*/ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /\** * The doPost method of the servlet.  
\* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred \*/ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到Action对象 Action action=getAction(request); //调用Action实现类方法 try { String result=action.execute(request, response); if(result!=null){ response.sendRedirect(result); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /\** * 创建一个获得Action的方法 * @param request \*/ public static Action getAction(HttpServletRequest request){ Action action=null; //截取action请求名(例如页面的登录：

) String uri=request.getRequestURI(); System.out.println("request.getRequestURI()---"+uri); //结果：request.getRequestURI()---/qifei\_mvc\_kuangjia\_zuoye/login.action StringBuffer url=request.getRequestURL(); System.out.println("request.getRequestURL()--"+url); //结果：request.getRequestURL()--http://localhost:8080/qifei\_mvc\_kuangjia\_zuoye/login.action String contextPath=request.getContextPath(); //结果：request.getContextPath()--/qifei\_mvc\_kuangjia\_zuoye System.out.println("request.getContextPath()--"+contextPath); String actionUrl=uri.substring(contextPath.length()); System.out.println("actionUrl--"+actionUrl); String actionName=actionUrl.substring(1,actionUrl.lastIndexOf(".")).trim(); //判断请求名 if(actionName.equals("login")){ action=new LoginAction(); } return action; } } \[/code] \[color=red]三、页面\[/color] \[code="java"] 用户名：  
密码：

\[/code]