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());
        }*/
    }    

}
时间: 2024-10-26 07:30:53

custom struts framework的相关文章

Struts Framework介绍

http://zh.wikipedia.org/zh-cn/Struts Struts是Apache软件基金会(ASF)赞助的一个开源项目.它最初是Jakarta项目中的一个子项目,并在2004年3月成为ASF的顶级项目.它通过采用Java Servlet/JSP技术,实现了基于Java EE Web应用的Model-View-Controller[MVC]设计模式的应用框架[Web Framework],是MVC经典设计模式中的一个经典产品. 目录 1 MVC结构 2 发展历程 3 优缺点 4

Spring+Struts+Hibernate 简介(转)

http://blog.csdn.net/slnqnd/article/details/1772910/ Struts2.0 +Hibernate 3.2 +Spring 2.0 一.        Struts 1.定义 它是使用 servlet 和 JavaServer Pages 技术的一种 Model-View-Controller 实现, 可帮助您控制Web 项目中的变化并提高专业化水平.“模型-视图-控制 器”(MVC) 就是用来帮助您控制变化的一种设计模式.MVC 减弱了业务逻辑接

Struts和SpringMVC两种MVC框架比较

基于Web的MVC framework在J2EE的世界内已是空前繁荣.TTS网站上几乎每隔一两个星期就会有新的MVC框架发布.目前比较好的MVC,老牌的有Struts.Webwork.新兴的MVC框架有Spring MVC.Tapestry.JSF等.这些大多是著名团队的作品,另外还有一些边缘团队的作品,也相当出色,如Dinamica.VRaptor等.这些框架都提供了较好的层次分隔能力.在实现良好的MVC 分隔的基础上,通过提供一些现成的辅助类库,同时也促进了生产效率的提高. 如何选择一个好的

javaEE面试重点

Hibernate工作原理及为什么要用? 原理: 1. 读取并解析配置文件 2. 读取并解析映射信息.创建SessionFactory 3. 打开Sesssion 4. 创建事务Transation 5. 持久化操作 6. 提交事务 7. 关闭Session 8. 关闭SesstionFactory 为什么要用: * 对JDBC訪问数据库的代码做了封装.大大简化了数据訪问层繁琐的反复性代码. * Hibernate是一个基于JDBC的主流持久化框架,是一个优秀的ORM实现. 他非常大程度的简化D

111个知名Java项目集锦,包括url和描述

转:http://www.cnblogs.com/wangs/p/3282183.html 项目名称   项目描述 ASM Java bytecode manipulation framework AspectWerkz AspectWerkz - Dynamic AOP for Java Axis Axis - an implementation of the SOAP (Simple Object Access Protocol) submission to W3C Batik Batik

struts2的分页标签

1.准备tld文件 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:s

SSH框架(1)

一. SSH框架相关的面试题: 1.  BeanFactory的作用是什么?   [中] BeanFactory是配置.创建.管理bean的容器,有时候也称为bean上下文.Bean与bean的依赖关系,也是由BeanFactory负责维护的. 2.  bean在spring的容器中两种基本行为是什么? [中] Singleton:单态   Non-singleton或prototype:原型 3.  spring配置文件中的ref元素的作用是什么?它的两个基本属性是什么?  [中] 用于指定属

MANIFEST.MF 文件内容完全详解(转)

打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录, 这个目录下会有一些文件,其中必有一个MANIFEST.MF,这个文件描述了该Jar文件的很多信息,下面将详细介绍MANIFEST.MF文件的内 容,先来看struts.jar中包含的MANIFEST.MF文件内容: Manifest-Version: 1.0Created-By: Apache Ant 1.5.1Extension-Name: Struts FrameworkSpecification-Title:

搭建SSH

搭建SSH详细步骤及相关说明 因为手里已有相关jar,为方便我搭建的是:Struts2.0+Hibernate3.3+Spring3.0,数据库:MySQL 如果想搭建最新的,在官网上下载最新jar即可. 这里会对比MyEclipse自动配置和手动配置,直接说搭建步骤,过程中说明一下一些重要的jar的作用. 1.新建web项目SSH 2.Struts2搭建(http://struts.apache.org/download) 通过MyEclipse搭建: 选择需要用到的jar, finish后,