package com.y2118.theframework.servlet;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.y2118.theframework.action.IAction;
import com.y2118.theframework.config.ActionManager;
import com.y2118.theframework.config.ActionMapping;
import com.y2118.theframework.config.ActionMappingManager;
public class ActionServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private ActionMappingManager mappingManager;
private String method;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//根据Action的名称,获取一个actionMapping
ActionMapping mapping = this.getActionMapping(request, response);
//获取全类名
String Qclass = mapping.getClassNmae();
Class clazz = Class.forName(Qclass);
//发射生成具体的IAction实现类
IAction action = (IAction) clazz.newInstance();
//循环遍历自定义集合
Method[] ms = clazz.getDeclaredMethods();
for (Method m :ms) {
if(m.getName().equals(method)){
//提取该方法
@SuppressWarnings("unused")
Method me = clazz.getMethod(m.getName());
//执行该方法,获取跳转页面的标记
String ret = (String)m.invoke(action);
//获取resutl
String result = mapping.getResult(ret);
//跳转页面
response.sendRedirect(result);
}
}
//
// IAction action = ActionManager.createAction(mapping.getClassNmae());
// String resultName = action.execute(request, response);
// String result = mapping.getResult(resultName);
// if(result==null){
// response.sendError(404,"未配置Action的result元素");
// return ;
// }
//response.sendRedirect(result);
} catch (Exception e) {
// TODO: handle exception
response.sendError(500,"请联系管理员");
System.out.println("错误"+e);
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
public void init(ServletConfig config)throws ServletException{
//读取初始参数
String configStr = config.getInitParameter("config");
//可以配置多个配置文件
String [] fileNames = null;
//如果为设置配置文件名称,则设置配置文件为默认名称
if(configStr==null || configStr.isEmpty()){
fileNames = new String[]{"struts.xml"};
}
this.mappingManager = new ActionMappingManager(fileNames);
}
public ActionMapping getActionMapping(HttpServletRequest request,HttpServletResponse response)throws Exception{
//获取请求的uri
String uri = request.getRequestURI();
//获取上下文路径
String contextPath = request.getContextPath();
//截取上下文路径以后的部分
String actionPath = uri.substring(contextPath.length());
//获取方法名称
String methodName = actionPath.substring(actionPath.lastIndexOf("!")+1,actionPath.lastIndexOf("."));
System.out.println("方法名称"+methodName);
//获取action名称
String actionName = actionPath.substring(1,actionPath.lastIndexOf("!")).trim();
System.out.println("action名称:"+actionName);
method = methodName;
ActionMapping mapping = null;
try {
mapping = mappingManager.getActionMappingByName(actionName);
} catch (Exception e) {
// TODO: handle exception
System.out.println("错误"+e);
}
return mapping;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
TheFramework.zip (26个子文件)
TheFramework
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 330B
src
com
y2118
theframework
config
ActionMappingManager.java 2KB
ActionMapping.java 832B
ActionManager.java 742B
action
LoginAction.java 509B
IAction.java 396B
servlet
ActionServlet.java 4KB
struts.xml 472B
.project 1KB
WebRoot
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 828B
classes
com
y2118
theframework
config
ActionManager.class 1KB
ActionMapping.class 1KB
ActionMappingManager.class 4KB
action
LoginAction.class 1KB
IAction.class 356B
servlet
ActionServlet.class 4KB
struts.xml 472B
lib
dom4j-1.6.1.jar 307KB
index.jsp 906B
wel.jsp 840B
.mymetadata 309B
.classpath 436B
.myeclipse
共 26 条
- 1
资源评论
- woniu_5212013-03-16代码挺好,可是就是用myeclipese写的,在eclipse上无法直接运行
- 疯狂--石头2013-05-06还行,可以使用
a85250720
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功