MVC是什么我就不多说了,我们平时做JavaWeb项目时,大都会用SSH框架的不同组合,那能不能不用SSH框架来实现一个原生的MVC框架呢?
下面就让我们来实现一个自己的javaWeb MVC框架。
项目结构如下:
Controler的实现:
package com.shu.controler; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.shu.action.Action; import com.shu.action.ActionFactory; /** * ControlerServlet,控制器类,所有的以.action结尾的请求都会被改控制器拦截, *例如:http://localhost:8080/mvc/LoginAction.action * @author Administrator * */ public class ControlerServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 4660044561652707132L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String pathName = req.getServletPath(); System.out.println("pathName:" + pathName); int index = pathName.indexOf("."); String actionName = pathName.substring(1,index); System.out.println("actionName:" + actionName); String actionClass = getInitParameter(actionName); System.out.println("actionClass:" + actionClass); Action action = ActionFactory.getAction(actionName); String viewUrl = action.execute(req, resp); if (viewUrl == null) { req.getRequestDispatcher("error.jsp").forward(req, resp); } else { req.getRequestDispatcher(viewUrl).forward(req, resp); } } }
Action接口:
package com.shu.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Action 接口,所有的业务action类都需要实现此接口 * execute方法返回view资源的引用字符串,比如“admin/login.jsp” * * @author Administrator * */ public interface Action { public String execute(HttpServletRequest request, HttpServletResponse response); }
Action工厂:
package com.shu.action; import com.shu.actions.LoginAction; /** * 该工厂根据传入的Action名字返回具体的业务action实例 * @author Administrator * */ public class ActionFactory { public static Action getAction(String actionName) { if (LoginAction.class.getSimpleName().equals(actionName)) { return new LoginAction(); } return null; } }
用户登录业务Action:
package com.shu.actions; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.shu.action.Action; /** * 处理用户登录的action类 * @author Administrator * */ public class LoginAction implements Action { @Override public String execute(HttpServletRequest request, HttpServletResponse response) { String username = request.getParameter("username"); String password = request.getParameter("password"); request.setAttribute("username", username); request.setAttribute("password", password); if (username != null && username.trim().length() != 0 && password != null && password.trim().length() != 0) { return "admin/success.jsp"; } return "admin/login.jsp"; } }
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>controlerServlet</servlet-name> <servlet-class>com.shu.controler.ControlerServlet</servlet-class> <init-param> <param-name>LoginAction</param-name> <param-value>com.shu.actions.LoginAction</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>controlerServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
视图View层:login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Login</title> </head> <body> <form action="<%=request.getContextPath() %>/LoginAction.action" method="post"> username:<input type="text" name="username"/><br> password:<input type="password" name="password"/><br> <input type="submit" name="Login"/><br> </form> </body> </html>
success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>Success!</h1> username:<%=request.getParameter("username") %><p> password:<%=request.getParameter("password") %><p> </body> </html>
测试:
时间: 2024-11-01 21:32:45