Download 自定义MVC框架实例

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
文档下载 免费文档下载
http://doc.xuehai.net/
自定义 MVC 框架实例
本文档下载自文档下载网,内容可能不完整,您可以复制以下网址继续阅读或下载:
http://doc.xuehai.net/bfb34cf8994da588eff46da53.html
index.jsp 页面">My JSP 'login.jsp' starting page
-->name:password:web.xml 文件
ActionServletframework.ActionServletconfigmystruts.xml
0ActionServlet*.actionindex.jspAction.javapackage
framework;import
javax.servlet.http.HttpServletRequest;import
javax.servlet.http.HttpServletResponse;public
interface
Action
String
SUCCESS="success";String ERROR="error";public String execute(HttpServletRequest
request,HttpServletResponse response);ActionManager.javapackage framework;public
class ActionManager /*** 获 取 Action 实 例 * @param className 的 权 限 定 类 名 *
@return*/public
static
Action
createAction(String
className)try
return
(Action)loadClass(className).newInstance(); catch (Exception e) // TODO: handle
exceptione.printStackTrace();return null;public static Class loadClass(String
className)System.out.println("className:"
clhttp://doc.xuehai.net/bfb34cf8994da588eff46da53.htmlassName);Class
clazz=null;try
clazz=Thread.currentThread().getContextClassLoader().loadClass(className); catch
(Exception
e)
//
TODO:
handle
exceptione.printStackTrace();System.out.println("clazz:"
clazz);if(clazz==null)try clazz=Class.forName(className); catch (Exception e)
// TODO: handle exceptione.printStackTrace();return clazz;
ActionMapping.javapackage
framework;import
java.util.HashMap;import
java.util.Map;public class ActionMapping private String name;private String
className;private Map resultMap=new HashMap();public String getName() return
文档下载 免费文档下载
http://doc.xuehai.net/
name;public void setName(String name) this.name = name;public String getClassName()
return className;public void setClassName(String className) this.className =
className;public Result getResult(String name)return resultMap.get(name);public
void
addResult(Shttp://doc.xuehai.net/bfb34cf8994da588eff46da53.htmltring
name,Result
result)this.resultMap.put(name,
result);ActionMappingManager.javapackage
java.io.InputStream;import
java.util.Map;import
framework;import
java.util.HashMap;import
java.util.Iterator;import
org.dom4j.Document;import
org.dom4j.io.SAXReader;public
class
org.dom4j.Element;import
ActionMappingManager
private
Map
actionMappings=new HashMap();/*** init 方 法 用 来 加 载 Action 配 置 文 件 * @param
configureFileName
配 置 文 件 名 */public void init(String configureFileName)try
if(configureFileName==null||configureFileName.isEmpty())throw new Exception("配置
文 件 的 名 称 不 能 为 空 ");InputStream is=this.getClass().getResourceAsStream("/"
configureFileName);// 使 用
dom4j
读 取 配 置 文 件 信 息
SAXReader().read(is);Element
Document
doc=new
root=doc.getRootElement();Element
actions=root.element("actions");for(Iterator
actionIt=actions.elements("action").iterator();actionIt.hasNext();)Element
action=actionIt.next();http://doc.xuehai.net/bfb34cf8994da588eff46da53.html// 将
action 元 素 的 属 性 保 存 在 ActionMapping 类 中 ActionMapping mapping=new
ActionMapping();mapping.setName(action.attributeValue("name"));mapping.setClassN
ame(action.attributeValue("class"));//
遍
历
result
节
点
for(Iterator
resultIt=action.elements("result").iterator();resultIt.hasNext();)Element
resultElement=resultIt.next();//把 result 元素中的属性封装到 Result 对象 Result
result=new
Result();String
name=resultElement.attributeValue("name");if(name==null||"".equals(name))name="s
uccess";result.setName(name);//redirect--
表
示
是
否
重
定
向
String
isRedirect=resultElement.attributeValue("redirect");result.setRedirect(Boolean.v
alueOf(isRedirect));result.setValue(resultElement.getText());// 把 每 个 封 装 好 的
result
添
加
到
ActionMapping
中
mapping.addResult(name,
result);actionMappings.put(mapping.getName(),mapping); catch (Exception e) // TODO:
文档下载 免费文档下载
http://doc.xuehai.net/
handle
exceptione.printStackTrace();public
ActionMappingManager(String[]
configureFileNames)for(Shttp://doc.xuehai.net/bfb34cf8994da588eff46da53.htmltri
ng
configureFileName:configureFileNames)init(configureFileName);public
ActionMapping
getActionMappingByName(String
actionName)
Exceptionif(actionName==null||actionName.isEmpty())return
throws
null;ActionMapping
mapping=this.actionMappings.get(actionName);if(mapping==null)throw
new
Exception(" 系 统 中 找 不 到 : [" actionName "], 请 查 看 配 置 文 件 ");return
mapping;ActionServlet.javapackage
framework;import
java.io.PrintWriter;import
java.io.IOException;import
javax.servlet.ServletConfig;import
javax.servlet.ServletException;import
javax.servlet.http.HttpServlet;import
javax.servlet.http.HttpServletRequest;import
javax.servlet.http.HttpServletResponse;import
action.LoginAction;public
class
ActionServlet extends HttpServlet private ActionMappingManager mappingManager;/***
Constructor of the object.*/public ActionServlet() super();/*** Destruction of
thttp://doc.xuehai.net/bfb34cf8994da588eff46da53.htmlhe servlet. */public void
destroy() super.destroy(); // Just puts "destroy" string in log// Put your code
here/*** The doGet method of the servlet. ** 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
occurred*/public
void
error
occurred*
@throws
doGet(HttpServletRequest
response)throws ServletException, IOException //
action=this.getAction(request);//
response);//
mapping
实
String
IOException
request,
if
an
error
HttpServletResponse
Action
result=action.execute(request,
response.sendRedirect(result);try // 根 据 action 名 称 获 取 一 个
例
ActionMapping
mapping=this.getActionMapping(request);Action
action=ActionManager.createAction(mapping.getClassName());String
resultName=action.execute(request,
response);Result
rhttp://doc.xuehai.net/bfb34cf8994da588eff46da53.htmlesult=mapping.getResult(res
ultName);if(result==null)response.sendError(404," 未 配 置 Action 的 result 元 素
文档下载 免费文档下载
http://doc.xuehai.net/
");return;if(result.isRedirect())response.sendRedirect(result.getValue());elsere
quest.getRequestDispatcher(result.getValue()).forward(request, response); catch
(Exception e) // TODO: handle exceptionresponse.sendError(500,"异常,请联系管理员
");e.printStackTrace();/*** The doPost method of the servlet. ** 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 d
oGet(request,
response);/***
Initialization
of
servlethttp://doc.xuehai.net/bfb34cf8994da588eff46da53.html.
the
**
@throws
ServletException if an error occurs*/public void init(ServletConfig config) throws
ServletException
//
读
取
初
始
化
参
数
String
configStr=config.getInitParameter("config");// 可 以 配 置 多 个 配 置 文 件 String[]
fileNames=null;// 如 果 未 设 置 配 置 文 件 的 名 字 , 则 设 置 配 置 文 件 的 名 字 为 默 认 名 称
if(configStr==null||configStr.isEmpty())fileNames=new
String[]"mystruts.xml";fileNames=configStr.split(",");this.mappingManager=new
ActionMappingManager(fileNames);// public
request)//
Action
getAction(HttpServletRequest
String uri=request.getRequestURI();//
contextPath=request.getContextPath();//
String
String
actionPath=uri.substring(contextPath.length());//
String
actionName=actionPath.substring(1, actionPath.lastIndexOf("."));//
action=null;//
if("login".equals(actionName))//
LoginAction();//
//
return action;// public
getActionMapping(HttpServletRequest
Action
action=new
ActionMapping
request)String
uri=request.getReqhttp://doc.xuehai.net/bfb34cf8994da588eff46da53.htmluestURI();
String
contextPath=request.getContextPath();String
actionPath=uri.substring(contextPath.length());String
actionName=actionPath.substring(1,
actionPath.lastIndexOf("."));ActionMapping
文档下载 免费文档下载
http://doc.xuehai.net/
mapping=null;try mapping=mappingManager.getActionMappingByName(actionName); catch
(Exception
e)
//
TODO:
mapping;Result.javapackage
handle
exceptione.printStackTrace();return
framework;public
class
Result
private
String
name;private String value;private boolean isRedirect;public String getName() return
name;public void setName(String name) this.name = name;public String getValue()
return value;public void setValue(String value) this.value = value;public boolean
isRedirect()
return
isRedirect;public
void
setRedirect(boolean
isRedirect)
this.isRedirect = isRedirect;
文档下载网是专业的免费文档搜索与下载网站,提供行业资料,考试资料,教
学课件,学术论文,技术资料,研究报告,工作范文,资格考试,word 文档,
专业文献,应用文书,行业论文等文档搜索与文档下载,是您文档写作和查找
参考资料的必备网站。
文档下载 http://doc.wendoc.com/
亿万文档资料,等你来免费下载
Related documents