码迷,mamicode.com
首页 > 其他好文 > 详细

struts2

时间:2018-09-29 17:44:48      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:cti   shm   反射   ret   getattr   xmlns   实现   split   info   

servlet的缺点

 

servlet重构

目的

1、  在web.xml文件中只写一个过滤器

2、  用action处理业务逻辑

3、  在过滤器中动态的调用action中的方法处理业务逻辑

设计

技术分享图片

 

1、  监听器

1、  准备一个map

2、  把所有的action的key,value放入到map中

3、   把map放入到application域中

2、  过滤器

1、  获取application域中的map

2、  解析url

3、  根据解析的url从map中把value提取出来

4、  根据java的反射机制动态调用action

5、  根据action返回的方法跳转到相应的页面

    3、执行action 的execute方法,该方法返回一个字符串

实现

技术分享图片
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <listener>
        <listener-class>com.itheima09.servlet.listener.ServletListener</listener-class>
    </listener>
    
    <filter>
        <filter-name>actionFilter</filter-name>
        <filter-class>com.itheima09.servlet.filter.DispatcherFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>actionFilter</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>
web.xml
技术分享图片
package com.itheima09.servlet.listener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ServletListener implements ServletContextListener{
    /**
     * 在tomcat销毁的时候执行
     */
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        arg0.getServletContext().setAttribute("mappings", null);
    }
    /**
     *    在tomcat启动的时候执行 
     */
    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("userAction", "com.itheima09.action.UserAction");
        arg0.getServletContext().setAttribute("mappings", map);
    }
}
listener
技术分享图片
package com.itheima09.servlet.filter;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima09.servlet.utils.ServletUtils;

public class DispatcherFilter implements Filter{
    
    private ServletContext servletContext;

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        /**
         * 1、从 application域中获取map
         */
        HttpServletRequest request = (HttpServletRequest)arg0;
        HttpServletResponse response = (HttpServletResponse)arg1;
        Map<String, String> map = (HashMap<String, String>)this.servletContext.getAttribute("mappings");
        /**
         * 2、获取浏览器中的url,把url解析出来
         *     http://localhost:8080/itheima09_servlet_super/userAction.action
         *     ---->userAction
         */
        //mapping = userAction
        String mapping = ServletUtils.parse(request.getRequestURI());
        String value = map.get(mapping); //value就是action的类的全名
        try {
            Class class1 = Class.forName(value);
            Method method = class1.getMethod("execute", HttpServletRequest.class,HttpServletResponse.class);
            //调用了action中的方法
            String jspName = (String)method.invoke(class1.newInstance(), request,response);
            request.getRequestDispatcher(jspName).forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        this.servletContext = arg0.getServletContext();
    }
    
}
filter
技术分享图片
package com.itheima09.servlet.utils;

import org.junit.Test;

public class ServletUtils {
    /**
     * http://localhost:8080/itheima09_servlet_super/userAction.action
     * @param url
     * @return
     */
    public static String parse(String url){
        String[] array = url.split("/");
        String mapping = array[array.length-1].substring(0,array[array.length-1].indexOf("."));
        return mapping;
    }
    
    @Test
    public void test(){
        System.out.println(ServletUtils.parse("http://localhost:8080/itheima09_servlet_super/userAction.action"));
    }
}
utils_urlparse

 

struts2

标签:cti   shm   反射   ret   getattr   xmlns   实现   split   info   

原文地址:https://www.cnblogs.com/hellowq/p/9724754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!