最简单的一个mvc实现

本文通过一个简单的MVC框架实例,介绍了如何实现基本的请求分发和动作执行过程。作者强调了理解Struts、Hibernate、Spring等框架底层原理的重要性,并分享了一个基于Servlet的MVC实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

感谢阿仁(一个香港程序员)告诉了我这个非常棒的网站,让我有时间他塌下心来学习基本的servlet。包括我在内,虽然很多大陆的程序员都声称什么ssh\jsf\jdo其实很多人连最基本的servlet、multi-thread、transaction-level还搞不清楚就去跟风搞ssh实在是本末倒置,没有必要。我觉得甚至还不如下点功夫把基础好好补习一下,看看struts、tomcat的源代码学习一下,然后实现一些自己的框架,哪怕就是单纯的模仿。
根据我自己的分析:struts使用的底层技术:xml\reflection\servlet\multi-thread; spring的底层技术:xml\aop\reflection\multi-thread; hibernate的底层技术:reflection\multi-thread\xml\jdbc\数据库基础。所以,花点时间把基础仔细的学习学习还是很有必要的~j
使用、学习java确实是很幸福的,因为有太多现成的开源软件供你使用,但是同时也有一些问题:如果你只会用,而不能深入学习,那你一辈子只能跟着开源组织走,而不能自己开发一套框架。很高兴,自己能够意识到这些,希望在3、5年之内我能自己实现一些框架,当然一定要是有生产力里的框架!3、5年,应该没有什么问题,至少现在我就能够体会到struts\hibernate\spring的一些底层实现方法。
非话太多了,上代码,转自:Gossip@caterpillar。一个最简单的mvc实现,看看这段代码,对struts有一种豁然开朗的感觉,用了那么久,现在才意识到其中的具体实现方法,太神奇了~~其中对于invoker类我做了一点修改:以保证线程安全。

package com.cxz.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Action {
public void execute(HttpServletRequest request, HttpServletResponse response);
}

package com.cxz.mvc;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DispatcherServlet extends HttpServlet {

private static final long serialVersionUID = 4617894412082217211L;
private Invoker invoker;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
invoker.request(request, response);
}

public void init() throws ServletException {
invoker = new Invoker();
invoker.addCommand("/welcome.action", null);//a action
invoker.addCommand("/login.action", null);//a action
invoker.setDefaultPage("/welcome.action");
}

}

package com.cxz.mvc;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Invoker {
private String defaultPage;

private Map<String, Action> actions = null;

public Invoker() {
actions = Collections.synchronizedMap(new HashMap<String, Action>());
}

public void addCommand(String name, Action action) {
actions.put(name, action);
}

public void request(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Action action = actions.get(request.getServletPath());
if (action != null) {
action.execute(request, response);
} else {
request.getRequestDispatcher(defaultPage)
.forward(request, response);
}
}

synchronized void setDefaultPage(String defaultPage) {
this.defaultPage = defaultPage;
}

public String getDefaultPage() {
return defaultPage;
}

}

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>com.cxz.mvc.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值