模拟Struts功能----ActionServlet

由于这段时间学习struts,对struts原理有一些了解,为了巩固自己的学习,所以自己手写了模拟struts功能的这样一个框架,写的不好还忘大家见谅....但是我可以拍拍胸脯的给大家说...这绝对是自己原创,写的不好还忘大家纠正,希望大家能一起学习...

以下是ActionServlet源码

package com.chusiyou.struts.core;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

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

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ActionServlet extends HttpServlet {
// private HashMap<String, String> formBeans = new HashMap<String,
// String>();
// private List<ActionMappings> actions = new ArrayList<ActionMappings>();
// private String actionName;

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 得到请求的路径
// String str1 = request.getServlet().getInitParameter("XXXX");
String str1 = request.getRealPath("WEB-INF/struts.config.xml");
HashMap<String, String> formBeans = new HashMap<String, String>();
List<ActionMappings> actions = new ArrayList<ActionMappings>();
String actionName = null;
this.readConfiguration(str1, formBeans, actions);
String name = request.getServletPath();
String str = name.substring(0, name.lastIndexOf("."));
System.out.println("请求的路径为:" + str);
ActionForm actionForm = null;
ActionMappings actionMappings = null;
Iterator<ActionMappings> iter = actions.iterator();
while (iter.hasNext()) {
ActionMappings action = iter.next();
if (action.getPath().equals(str)) {
for (String beanName : formBeans.keySet()) {
//判断当前的action 是否配置了name属性
if (action.getName() != null) {
if (action.getName().equals(beanName)) {
//填充formBean并且把它放进session范围之中
FormFullUtil.full(request, formBeans.get(beanName),
beanName);
//得到FormBean
actionForm = (ActionForm) request.getSession()
.getAttribute(beanName);

}
}
}
actionName = action.getType();
actionMappings = action;
}
}
try {
//得到要调用对象的字节码
Class clazz = Class.forName(actionName);

Action action = (Action) clazz.newInstance();
//得到execute方法的字节码
Method method = action.getClass().getMethod("execute",
ActionMapping.class, ActionForm.class,
HttpServletRequest.class, HttpServletResponse.class);
//调用action 的execute方法
ActionForward actionForward = (ActionForward) method.invoke(action,
new ActionMapping(request, response, actionMappings),
actionForm, request, response);
} catch (Exception e) {
e.printStackTrace();
}
}

// 读取struts配置文件
private void readConfiguration(String configurationName,
HashMap<String, String> formBeans, List<ActionMappings> actions) {
SAXReader ready = new SAXReader();

// 获得解析的XML文档对象
try {
Document doc = ready.read(configurationName);
Element root = doc.getRootElement();
Element rootBean = root.element("form-beans");
for (Iterator i = rootBean.elementIterator("form-bean"); i
.hasNext();) {
Element beanNode = (Element) i.next();
//将循环到的formBean放进map集合
formBeans.put(beanNode.attributeValue("name"), beanNode
.attributeValue("type"));
}
Element rootAction = root.element("action-mappings");
for (Iterator i = rootAction.elementIterator("action"); i.hasNext();) {
Element actionNode = (Element) i.next();
ActionMappings action = new ActionMappings();
//得到path
action.setPath(actionNode.attributeValue("path"));
//得到name
action.setName(actionNode.attributeValue("name"));
//得到type
action.setType(actionNode.attributeValue("type"));
//判断该action是否有forward节点
if (actionNode.isTextOnly()) {

} else {
for (Iterator j = actionNode.elementIterator("forward"); j
.hasNext();) {
Element forwardNode = (Element) j.next();
action.getForwards().put(
forwardNode.attributeValue("name"),
forwardNode.attributeValue("path"));
}
}

actions.add(action);

}

} catch (Exception e) {
e.printStackTrace();
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值