custom struts framework
1. Difference between stucts1 and struts2
struts1 : Servlet used as Controller , you can visit the Servlet directly, code like if---else if--else
struts2 : Filter used as Controller , you can only visit it passively, though setting the condition like "/*"
2. Imitate the core Filter of Struts2 (StrutsPrepareAndExecuterFilter.java)
version 1.0(like struts1)
package com.cdv.web.filter; import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet Filter implementation class StrutsPreparedAndExecuterFilter
*/
public class StrutsPreparedAndExecuterFilter implements Filter { /**
* Default constructor.
*/
public StrutsPreparedAndExecuterFilter() {
// TODO Auto-generated constructor stub
} /**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
} /**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String uri = request.getRequestURI();
boolean isDo = uri.endsWith(".do");
if(isDo){
//get the operation
String op = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
if(op.equals("login")){
request.setAttribute("username", "zhangsan");
}else{
//to do something
}
}else{
//
chain.doFilter(request, response);
}
} /**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
} }
version 2.0
we need create the interface , for creating different instance aim at different params condition, and use the map instead of if--else
package com.cdv.web.action; import javax.servlet.http.HttpServletRequest; public interface Action { String execute(HttpServletRequest request);
}
package com.cdv.web.action; import javax.servlet.http.HttpServletRequest; public class LoginAction implements Action { @Override
public String execute(HttpServletRequest request) {
String username = request.getParameter("username");
if(username.equals("zhangsan")){
return "ok";
}
return null;
} }
package com.cdv.web.filter; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.cdv.web.action.Action;
import com.cdv.web.action.LoginAction; /**
* Servlet Filter implementation class StrutsPreparedAndExecuterFilter
*/
public class StrutsPreparedAndExecuterFilter2 implements Filter { private Map<String, Action> actionMap = new HashMap<String, Action>();
/**
* Default constructor.
*/
public StrutsPreparedAndExecuterFilter2() {
// TODO Auto-generated constructor stub
} /**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
} /**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String uri = request.getRequestURI();
boolean isDo = uri.endsWith(".do");
if(isDo){
String op = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
Action instance = actionMap.get(op);
String result = instance.execute(request);
if(result.equals("ok")){
request.setAttribute("user", "zhangsan");
}
}else{
chain.doFilter(request, response);
}
} /**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
actionMap.put("login", new LoginAction());
//actionMap.put("logout", new LogoutAction());
//...
} }
version 3.0
put the value of instance into xml file , use dom4j to parse it, and use reflect technology to create instance dynamic.
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<package name="mypackage">
<action name="loginAction" class="com.cdv.web.action.LoginAction"></action>
<action name="logoutAction" class="com.cdv.web.action.LogoutAction"></action>
</package>
package com.cdv.web.filter; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.cdv.web.action.Action;
import com.cdv.web.action.LoginAction; /**
* Servlet Filter implementation class StrutsPreparedAndExecuterFilter
*/
public class StrutsPreparedAndExecuterFilter3 implements Filter { private Map<String, Action> actionMap = new HashMap<String, Action>();
/**
* Default constructor.
*/
public StrutsPreparedAndExecuterFilter3() {
// TODO Auto-generated constructor stub
} /**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
} /**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String uri = request.getRequestURI();
boolean isDo = uri.endsWith(".do");
if(isDo){
String op = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
Action instance = actionMap.get(op);
String result = instance.execute(request);
if(result.equals("ok")){
request.setAttribute("user", "zhangsan");
}
}else{
chain.doFilter(request, response);
}
} /**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
//进行dom4j解析,将struts.xml中的name 和 value取出,
//并利用反射将class的值转化为对象
/* Document document = Dom4jUtil.getDocument();
List<Node> list = document.selectNodes("//action");
for(Node node : list){
//遍历action节点,取出值
String key = node.valueOf("@name");
String value = node.valueOf("@class");
actionMap.put(key, (Action) Class.forName(value).newInstance());
}*/
} }
custom struts framework的更多相关文章
- Struts framework
Struts功能详解——ActionMapping对象 Struts旅程(一)Struts简介和原理 实例讲解DispatchAction和LookupDispatchAction DispatchA ...
- Struts 2 Learning
目录 . J2EE简介 . JAVA EE应用的分层模型 . 搭建Struts2 Demo应用 . struts2流程 . struts2的常规配置 . 实现Action . 配置Action . 配 ...
- Configure the Struts Tag Libraries
In Struts framework, you always need to configure the Struts tag libraries in order to access it in ...
- Spring+Struts+Hibernate 简介(转)
http://blog.csdn.net/slnqnd/article/details/1772910/ Struts2.0 +Hibernate 3.2 +Spring 2.0 一. ...
- Struts和SpringMVC两种MVC框架比较
基于Web的MVC framework在J2EE的世界内已是空前繁荣.TTS网站上几乎每隔一两个星期就会有新的MVC框架发布.目前比较好的MVC,老牌的有Struts.Webwork.新兴的MVC框架 ...
- Struts知识问答 分类: 面试 2015-07-10 22:01 4人阅读 评论(0) 收藏
1. 简述Struts框架的初始化流程. 答案: 对于采用Struts框架的Web应用,在Web应用启动时就会加载并初始化控制器ActionServlet ActionServlet从struts-c ...
- struts工作原理不错的解释___
Struts 使用 Model 2 架构.Struts 的ActionServlet 控制导航流.其他Struts 类,比如Action, 用来访问业务逻辑类.当 ActionServlet 从容器接 ...
- Struts 2 Overview
Struts2 is popular and mature web application framework based on the MVC design pattern. Struts2 is ...
- 概述struts,以及struts如何实现MVC架构的
概述MVC体系结构? 答:MVC包括三类对象,model是应用对象,view是视图,controller是控制器,它定义用户界面对用户输入的响应方式. 在MVC体系中,模型通常被称为”业务逻辑”,是真 ...
随机推荐
- OpenGL es3.0 初始化及渲染
class FOpenglEs { public: /** * 初始化 OpenGLES3.0 */ bool initOpenGLES30(HWND hwnd) { EGLConfig config ...
- Mac OSX 安装nvm(node.js版本管理器)
我的系统 1.打开github官网https://github.com/,输入nvm搜索,选择creationix/nvm,打开 2.找到Install script,复制 curl -o- http ...
- Ranorex 5 发布,支持SAP、Oracle Forms、MS Dynamics等
Ranorex 5 发布,支持SAP.Oracle Forms.MS Dynamics等 http://blog.csdn.net/testing_is_believing/article/detai ...
- git Please move or remove them before you can merge. 错误解决方案
git pull 时 往往会遇到各种各样的问题 ,下面是常遇到的一种状况 Updating 7c9e086..936acacerror: The following untracked working ...
- ubuntu下搭建lamp
一.使用apt-get方式为Ubuntu安装PHP+MYSQL+Apache 分别执行如下命令: (1)安装MYSQL sudo apt-get install mysql-server ...
- Mac终端常用命令收集
删除非空目录 rm -rf 目录名字 -r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思 终端修改hosts文件 sudo vi /etc/hosts 切换到su ...
- java之接口interface
接口 1.多个无关的类可以实现同一个接口 2.一个类可以实现多个无关的接口 3.与继承关系类似,接口与实现类之间存在多态性 4.定义java类的语法格式 < modifier> class ...
- net组件转化成COM组件
第一步:生成秘钥文件 强名称工具 (Sn.exe) 有助于使用强名称对程序集进行签名.Sn.exe 提供了用于密钥管理.签名生成和签名验证的选项. 1.使用Visual Studio 命令 Visua ...
- 基于jQuery HTML5人物介绍卡片特效
基于jQuery HTML5人物介绍卡片特效.这是一款基于jquery.material-cards插件实现的人物介绍卡片形式特效代码.效果图如下: 在线预览 源码下载 实现的代码. html代码 ...
- VS2008简体中文正式版序列号-试用到期解决
VS2008简体中文正式版序列号 VS2008简体中文正式版序列号 1.Visual Studio 2008 Professional Edition:XMQ2Y-4T3V6-XJ48Y-D3K2V- ...