package com.handsome.struts;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.w3c.dom.Element;
import com.handsome.xmlmanager.XmlManager;
public class ActionServlet extends HttpServlet {
public ActionServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void init() throws ServletException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
try{
//通过配置文件找到ActionForm
String configFile = this.getConfigFile();
//获取请求uri
String uri = this.getRequestUri( request );
Element root = XmlManager.getRoot( configFile );
Element actionElement = XmlManager.getChildElement( root, "action", "path", uri );
String formName = XmlManager.getAttribute( actionElement, "name");
Element actionFormElement = XmlManager.getChildElement( root, "form-bean", "name", formName);
String className = XmlManager.getAttribute( actionFormElement, "type" );
Class formClass = Class.forName( className );
ActionForm actionForm = this.dataPackage( request, formClass );
//创建action实例
String actionTypeValue = XmlManager.getAttribute(actionElement, "type");
Action actionClass = (Action)Class.forName( actionTypeValue ).newInstance();
ActionMapping mapping = new ActionMapping( actionElement );
ActionForward af = actionClass.execute( mapping, actionForm, request, response );
String forwardUrl = af.getForwardUrl();
if( null == af || forwardUrl == null ){
return;
}
RequestDispatcher rd = request.getRequestDispatcher( forwardUrl );
rd.forward( request, response );
System.out.println( actionTypeValue );
}catch( Exception e){
e.printStackTrace();
}
}
private ActionForm dataPackage(HttpServletRequest request, Class formClass) throws Exception {
ActionForm actionForm = (ActionForm)formClass.newInstance();
Field [] list = formClass.getDeclaredFields();
for( Field field : list ){
String fieldName = field.getName();
Class fieldType = field.getType();
String setterMethodName = this.createSetterMethodName( fieldName );
Method setterMethod = formClass.getMethod(setterMethodName, fieldType);
String paramValue = request.getParameter( fieldName );
if( null == paramValue ){
continue;
}
if( fieldType.getSimpleName().equals("String")){
setterMethod.invoke( actionForm, paramValue );
}else if(fieldType.getSimpleName().equals("int")){
setterMethod.invoke( actionForm, Integer.valueOf(paramValue) );
}
}
return actionForm ;
}
private String createSetterMethodName(String fieldName) {
String first = fieldName.substring(0, 1);
String supperChar = first.toUpperCase();
fieldName = fieldName.replaceFirst( first, supperChar );
return "set" + fieldName;
}
private String getRequestUri(HttpServletRequest request) {
String uri = request.getRequestURI();
String [] list = uri.split("/");
String realUri = "";
for( int i = 2; i < list.length; i++ ){
realUri = "/" + list[i];
}
int end = realUri.lastIndexOf(".");
realUri = realUri.substring( 0, end );
System.out.println( realUri );
return realUri;
}
private String getConfigFile() {
// 获取Servlet的配置中名字叫config的初始化参数
String configFile = this.getInitParameter("config");
//获取路径
String path = this.getServletContext().getRealPath("");
String file = path + configFile ;
//System.out.println( file );
return file;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Struts1.x 的核心源码(研究下框架怎样构成)
共24个文件
java:7个
class:7个
jsp:2个
3星 · 超过75%的资源 需积分: 10 12 下载量 142 浏览量
2009-02-07
10:26:16
上传
评论
收藏 17KB RAR 举报
温馨提示
经过老师讲解,猜写的Struts1.x 的核心源码 不含国际化,标签库,和数据验证,很简单
资源推荐
资源详情
资源评论
收起资源包目录
Handsome-Struts1.rar (24个子文件)
Handsome-Struts1
.project 1KB
.settings
org.eclipse.jdt.core.prefs 330B
.mymetadata 321B
WebRoot
WEB-INF
web.xml 2KB
struts-config.xml 771B
lib
handsome-xmlmanager.jar 3KB
classes
com
handsome
struts
test
User.class 769B
LoginCtrl.class 2KB
ActionForm.class 295B
Action.class 779B
ActionServlet.class 6KB
ActionMapping.class 1KB
ActionForward.class 572B
index.jsp 434B
META-INF
MANIFEST.MF 39B
ok.jsp 239B
.myeclipse
src
com
handsome
struts
ActionForm.java 64B
Action.java 322B
test
LoginCtrl.java 737B
User.java 476B
ActionMapping.java 690B
ActionForward.java 250B
ActionServlet.java 4KB
.classpath 444B
共 24 条
- 1
资源评论
- 快乐小熊熊2015-06-30谢谢 虽然最后还是没有研究。
yanghamming
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- IMG_0694.GIF
- 基于图像的三维模型重建C++源代码+文档说明(高分课程设计)
- 基于聚焦法的工件立体测量方案,根据数据进行三维重建 使用HALCON处理图像,MATLAB拟合数据+源代码+数据集+效果图
- 锄战三国村 修改:货币使用不减 v1.10(2) 原创 (中文).apk
- 基于python实现的单目双目视觉三维重建+源代码+图像图片(高分课程设计)
- 基于C+++OPENCV的全景图像拼接源码(课程设计)
- 基于Python+OpenCV对多张图片进行全景图像拼接,消除鬼影,消除裂缝+源代码+文档说明+界面截图(高分课程设计)
- 基于C++实现的全景图像拼接源码(课程设计)
- 基于SIFT特征点提取和RASIC算法实现全景图像拼接python源码+文档说明+界面截图+详细注释(95分以上课程大作业)
- 基于matlab实现眼部判别的疲劳检测系统+源代码+全部数据+文档说明+详细注释+使用说明+截图(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功