`
- 浏览:
220282 次
- 性别:
- 来自:
大连
-
java 代码
- package com.viwo.web.acion;
-
- import java.io.File;
- import java.io.IOException;
- import java.lang.reflect.Method;
- import java.util.Map;
-
- 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.apache.commons.digester.Digester;
- import org.apache.log4j.Logger;
- import org.xml.sax.SAXException;
-
- import com.viwo.web.config.ConfigAction;
- import com.viwo.web.config.ConfigForward;
- import com.viwo.web.config.ViwoConfig;
-
-
-
-
-
-
-
- public class ViwoServlet extends HttpServlet {
-
- private final static Logger logger = Logger.getLogger(ViwoServlet.class);
- public void doGet(HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException {
-
- process(request, response);
-
- }
-
- public void doPost(HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException {
-
- process(request, response);
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
- protected void process(HttpServletRequest request,
- HttpServletResponse response)
- throws IOException, ServletException {
- String config = getServletConfig().getInitParameter("config");
- String template = getServletConfig().getInitParameter("template");
- logger.info("[viwo]-->[enter process]-->[config:]"+config);
-
- String forward = "";
- Object returnValue = null;
- String url = request.getRealPath(config);
- logger.debug("[viwo]-->[process]-->[url:]"+url);
-
- String requestURI = request.getRequestURI();
- int lastdot = requestURI.lastIndexOf(".");
- int lastbias = requestURI.lastIndexOf("/");
- int lastbiasbutone = requestURI.lastIndexOf("/",lastbias-1);
- String method = requestURI.substring(lastbias+1,lastdot);
- String action = requestURI.substring(lastbiasbutone+1,lastbias);
- Object actionObject = null;
- try
- {
- ViwoConfig viwoConfig = getViwoConfig(url);
- Map cfgActionMap = viwoConfig.getActionMap();
- Map cfgForwardMap = viwoConfig.getForwardMap();
-
- if(cfgActionMap.get(action)!=null)
- {
- ConfigAction ca = (ConfigAction)cfgActionMap.get(action);
- action = ca.getType();
- }
-
- actionObject = Class.forName(action).newInstance();
- Method m = getMethod(actionObject,method);
- if(m != null)
- {
- returnValue = m.invoke(actionObject,new Object[]{request,response});
-
- if(returnValue==null)
- return;
-
- if(template!=null)
- forward = "/"+returnValue+"."+template;
- else
- forward = "/"+returnValue+".jsp";
-
- if(cfgForwardMap.get(returnValue)!=null)
- {
- ConfigForward cf = (ConfigForward)cfgForwardMap.get(returnValue);
- forward = cf.getPath();
- }
-
- logger.debug("[viwo]-->[process]-->[forward:]"+forward);
- RequestDispatcher rd = getServletContext().getRequestDispatcher(forward);
- rd.forward(request, response);
- }
- else
- {
- logger.error("[viwo]-->[process]-->"+"There haven't the "+method+" Method,Please check URL!");
- response.getWriter().print("There haven't the "+method+" Method,Please check URL!");
- }
-
- }
- catch (ClassNotFoundException e)
- {
- logger.error("[viwo]-->[process]-->"+"There haven't the "+action+" Class,Please check URL!");
- response.getWriter().print("There haven't the "+action+" Class,Please check URL!");
- }
- catch (NoClassDefFoundError e)
- {
- logger.error("[viwo]-->[process]-->"+"There haven't the "+action+" Class,Please check URL!");
- response.getWriter().print("There haven't the "+action+" Class,Please check URL!");
- }
- catch (Exception e)
- {
- System.out.println(e.getMessage());
- logger.error("[viwo]-->[process]-->"+e.getMessage());
- }
-
- }
-
-
-
-
-
-
-
-
-
- private ViwoConfig getViwoConfig(String url) throws IOException, SAXException
- {
- Digester digester = new Digester();
- digester.setValidating(false);
- digester.addObjectCreate("viwo-config", "com.viwo.web.config.ViwoConfig");
-
- digester.addObjectCreate("viwo-config/action", "com.viwo.web.config.ConfigAction");
- digester.addSetProperties("viwo-config/action");
- digester.addSetNext("viwo-config/action", "addAction", "com.viwo.web.config.ConfigAction");
-
- digester.addObjectCreate("viwo-config/forward", "com.viwo.web.config.ConfigForward");
- digester.addSetProperties("viwo-config/forward");
- digester.addSetNext("viwo-config/forward", "addForward", "com.viwo.web.config.ConfigForward");
- ViwoConfig viwoConfig = (ViwoConfig) digester.parse(new File(url));
- return viwoConfig;
-
- }
-
-
-
-
-
-
-
-
- private Method getMethod(Object actionObject,String method)
- throws IOException
- {
-
- Method[] methods = actionObject.getClass().getMethods();
- if(methods==null||methods.length==0)
- {
- return null;
- }
- for(int i=0;i {
- if(methods[i].getName().equals(method))
- return methods[i];
- }
- return null;
-
- }
-
- }
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
struts加mybatis实现的简易个人博客平台用到了少量css,javascript仅供初学者参考
struts版本的简易留言板 学习参考 含项目源码
struts+SmartUpload实现的一个简易上传下载系统,此系统的主要代码已经放到俺博客上了,欢迎访问 “http://blog.csdn.net/zxingchao2009/archive/2010/09/08/5872106.aspx”
初学struts框架时,老师的作业,我写完了,上传一下,希望能对像我一样初学的人,有所帮助!
1.能够通过配置定义Action,能够把客户端的参数传递给Action并从Action中取出结果值。 2.JSP页面能获取Action中的结果值。 3.能够实现拦截器。 4.支持文件上传下载
struts2技术是非常流行的MVC模型解决方案的一种,本资源从strut2技术出发,实现了一个简单的计算器
NULL 博文链接:https://jasinz.iteye.com/blog/1164945
基于Struts框架的简易考勤管理系统的实现.docx
一个银行的简易系统能实现登陆注册,及简单的增删改查,界面还可以
mystruts简易MVC框架实现,实现mvc思想,部分代码参考struts1.x
简易教学管理系统(jsp+spring+struts+hibernate+mysql)
Struts2+Spring+Hibernate框架实现的简易银行系统 适合使用SSH 框架的入门学习。
使用java ssh框架(struts2+hibernate+spring)开发的简易电子商城系统,有用户登录注册功能、商城购物功能,有前端页面,开发环境:编译器为eclipse,数据库为mysql5.7(mysql8.0用不了,需要重新修改配置文件),...
基于struts+tomcat+jdbc的简易BBS,基本功能都实现了,用netbeans开发的!
简单的使用Struts2+hibernate框架合并的学生信息管理小项目,适合初学者参考使用,大神请绕道
这是我用struts开发的一个简易的学生信息管理系统,该系统有良好的可扩展性,它能和mysql数据库交互,将学生的相关信息写入到数据.压缩包里有实现它的所有源代码及相关说明文件,你可以轻松的扩展其功能,如查询或修改...
sturts2微博系统,实现发心情功能,采用struts2+mysql+css+struts2标签+javascript技术,大三的struts2课程设计,附带源代码,mysql备份文件,课程设计报告。初学者和开发者都可以拿来参考和借鉴,不坑。
简易的网上购书系统:用户必须先注册,登录之后到达我的网上书城,选择自己喜欢的书籍并加入购物车,在购物车中选择支付。
struts2+spring+hibernate3的简易图书管理系统,特别适合初学者集成! 实现了简单的分页和模糊查询。(基本上涵盖了开发ssh2的包),值得拥有哦! 注:数据库sql文件(mysql 5.5)在里面,要自己配好!