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体系中,模型通常被称为”业务逻辑”,是真 ...
随机推荐
- JAVA本地远程连接linux程序监控状态
环境: 1.本地window 2.程序部署在centos 一,启动访问权限安全守护程序 新建文件:jstatd.all.policy ,注意路径 grant codebase "$JA ...
- Eclipse中集成Tomcat
问题: 很多时候在Eclipse中启动Tmocat后,不能访问本机的localhost:8080主页,并且其他项目也不能访问. 原因: 打开Tomcat下的webapp后也找补到项目目录,这是因为Ec ...
- saiku 展示优化第二步(要诀和技巧)
经历了上几篇博客的分享,可以无密码登录 : http://www.cnblogs.com/liqiu/p/5246015.html 随着使用的深入,公司需要将现有的报表平台与saiku整合,其中最便捷 ...
- wamp2.5 不能运行在win2003的解决方法
安装时提示 httpd.exe 不是有效的 win32程序 之后就启动不了,连小icon都不显示了 经查发现 wampserver 2.5用 vc11编译,并使用了他的类库 vc11是不支持 xp和 ...
- 泊松回归(Poisson Regression)
本博客已经迁往http://www.kemaswill.com/, 博客园这边也会继续更新, 欢迎关注~ Linear Regression预测的目标\(Y\)是连续值, Logistic Regre ...
- delphi7的新生,参与分布式应用开发,调用RESTful API,Json的应用
前言: 1.公司delphi7开发的传统软件还活得好好的,但是大家都知道delphi早已经日落西山了,现在成了后进.追随者.细细算了已经6.7不用了.新的delphixe7呢,没有时间成本去适应和研究 ...
- CCF推荐国际学术期刊
中国计算机学会推荐国际学术期刊 (计算机系统与高性能计算) 一.A类 序号 刊物简称 刊物全称 出版社 网址 1 TOCS ACM Transactions on Computer Systems A ...
- 【Android学习】数据传递三种方式
1.Application 注意在清单文件中的Application节点下注册android:name属性, 继承Application类,重写onCreate方法, 使用数据时,实例化自定义类时需要 ...
- php添加数据到xml文件的例子
php添加数据到xml文件中 时间:2015-12-17 06:30:37来源:网络 导读:php添加数据到xml文件中 xml文件:stu.xml: 复制代码代码如下: <?xml ver ...
- JavaScript的9个陷阱及评点
1. 最后一个逗号 如这段代码,注意最后一个逗号,按语言学角度来说应该是不错的(python的类似数据类型辞典dictionary就允许如此).IE会报语法错误,但语焉不详,你只能用人眼从几千行代码中 ...