利用jsp和Servlet實現自己的原生JavaEE MVC框架

        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);
                if (action == null) return;
		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>


測試:

 

發佈了24 篇原創文章 · 獲贊 16 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章