简单MVC框架

这是一个简单的MVC框架!
第一步,先创建一个ActionServlet来充当中心控制器,servlet在web.xml中的配置如下

<servlet>
<servlet-name>ActionServlet</servlet-name>
<servlet-class>org.wq.framework.ActionServlet</servlet-class>
<init-param>
<param-name>mymvcConfig</param-name>
<param-value>/config.properties</param-value>
</init-param>
<load-on-startup>0</load-on-startup><!-- 当tomcat启动时,该servlet就被实例化 -->
</servlet>

<servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

第二步,建立属性文件config.properties,在这里属性文件的作用就相当于strust中的配置文件,通过属性文件来获取action的路径

#如果请求的是/login.do,则servlet会自己去寻找org.wq.framework.LoginAction
/login.do=org.wq.framework.LoginAction


第三步,建立ActionForward,主要作用是来存放跳转路径和跳转的方式

package org.wq.framework;

public class ActionForward {
//跳转的路径
private String forwardPath;

private boolean isRedirect;

public ActionForward(){}

public ActionForward(String forwardPath,boolean isRedirect){
this.forwardPath = forwardPath;
this.isRedirect = isRedirect;
}


public String getForwardPath() {
return forwardPath;
}

public void setForwardPath(String forwardPath) {
this.forwardPath = forwardPath;
}

public boolean isRedirect() {
return isRedirect;
}

public void setRedirect(boolean isRedirect) {
this.isRedirect = isRedirect;
}
}

第四步:创建Action接口

package org.wq.framework;

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

public interface Action {
public ActionForward excute(HttpServletRequest request,HttpServletResponse response);
}


第五步,也是最重要的一步,编写ActionServlet!

package org.wq.framework;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

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

public class ActionServlet extends HttpServlet {
private Properties configure = null;

public void init(ServletConfig config) throws ServletException {
try{
configure = new Properties();
String configFile = config.getInitParameter("mymvcConfig");
if(configFile!=null){
configure.load(this.getClass().getResourceAsStream(configFile));
}
}catch(Exception e){
e.printStackTrace();
}
}
public ActionServlet() {
super();
}

public void destroy() {
super.destroy();
}

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

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//取得上下文的路径,如:/mymvc
String contextPath = request.getContextPath();
//取得请求的URL,如: /mymvc/login.do
String uri = request.getRequestURI();
//获得action的请求路径,如/login.do
String actionPath = uri.substring(contextPath.length(),uri.length());
//根据请求路径从配置文件中找到要执行的action
String actionClass = (String) configure.getProperty("actionPath");
//获得action的实例
try{
//通过反射获取对象
Action action = (Action)Class.forName(actionClass).newInstance();
//调用excute()方法,
ActionForward actionForward = action.excute(request, response);
//重定向
if(actionForward.isRedirect()){
response.sendRedirect(contextPath+actionForward.getForwardPath());
}else{//转发
request.getRequestDispatcher(actionForward.getForwardPath()).forward(request, response);
}
}catch(Exception e){
e.printStackTrace();
}
}
}


到了这里,一个简单的MVC框架就做好了,这个框架的不好之处没有相对应的ActionForm,还有就是跳转必须通过属性文件来配置,顶顶大名的struts框架可是通过xml文件来配置的!本来自己做了一个以struts框架为模型的mvc,可是频频报错,只能去依靠老师修改,然后再来发表了!当然啦,如果哪为网友有更好的自己制作的MVC框架的话,记得E-mail我哦 javaeeboy@qq.com 大家一起共同努力,java就是讨论出来的!
好了,在这里感谢一下吴青吴老师!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值