zoukankan      html  css  js  c++  java
  • spring mvc convention over configuration 之 RequestToViewNameTranslator

      

    1. RequestToViewNameTranslator简介

    在springmvc中很多地方都是约定优于配置的,比如这种写法:

    @Controller
    public class IndexAction {
    
        @RequestMapping("/index.htm")
        public void index(){
    
            System.out.println("首页");
            
        }
        
    }

    这个Handler方法并没有返回视图名,这个时候该怎么办呢,springmvc提供了一个RequestToViewNameTranslator接口就是专门为了解决这种情况的,在没有指定视图名字的时候会调用这个接口的实现来得到要使用的视图名。

    RequestToViewNameTranslator : 用于处理没有返回视图名时的情况下如何得到一个默认的视图名。

    2. RequestToViewNameTranslator原理分析

    那么这个接口是在什么时候被调用的呢,来看一下DispatcherServlet里面的代码:

    在DispatcherServlet中有一个变量用来存储当没有返回视图名时要使用的RequestToViewNameTranslator的:

    初始化代码:

     

     REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME是一个常量,我们如果要自定义的话beanName一定要和这个值一致:

    上面被调用的getDefaultStrategy(ApplicationContext context, Class<T> strategyInterface)方法:

    /**
     * 
     * 策略模式,根据传入的策略接口返回默认的策略对象,即根据传入的一个Interface的class类得到其默认的实现类。
     * 
     * 当然啦,默认的实现应该只有一个,那这里为什么还要进行检查呢?继续耐心往下看...
     * 
     * Return the default strategy object for the given strategy interface.
     * <p>The default implementation delegates to {@link #getDefaultStrategies},
     * expecting a single object in the list.
     * @param context the current WebApplicationContext
     * @param strategyInterface the strategy interface
     * @return the corresponding strategy object
     * @see #getDefaultStrategies
     */
    protected <T> T getDefaultStrategy(ApplicationContext context, Class<T> strategyInterface) {
        List<T> strategies = getDefaultStrategies(context, strategyInterface);
        if (strategies.size() != 1) {
            throw new BeanInitializationException(
                    "DispatcherServlet needs exactly 1 strategy for interface [" + strategyInterface.getName() + "]");
        }
        return strategies.get(0);
    }

     在getDefaultStrategies获取并实例化策略对象返回:

        /**
         * 
         *  根据传入的策略接口来创建一个策略对象列表,即根据传入的一个接口可以得到一大波的对象,但是它是怎么知道这两个怎么对应起来的呢?
         *  这是因为在同一个包(org.springframework.web.servlet)下有一个叫做DispatcherServlet.properties的文件记录着接口和对象的映射关系。
         * 
         * Create a List of default strategy objects for the given strategy interface.
         * &lt;p&gt;The default implementation uses the &quot;DispatcherServlet.properties&quot; file (in the same
         * package as the DispatcherServlet class) to determine the class names. It instantiates
         * the strategy objects through the context&#x27;s BeanFactory.
         * @param context the current WebApplicationContext
         * @param strategyInterface the strategy interface
         * @return the List of corresponding strategy objects
         */
        @SuppressWarnings(&quot;unchecked&quot;)
        protected &lt;T&gt; List&lt;T&gt; getDefaultStrategies(ApplicationContext context, Class&lt;T&gt; strategyInterface) {
            //获取接口的全路径类名
            String key = strategyInterface.getName();
            //以接口的全路径类名为key,得到其对应的策略对象
            String value = defaultStrategies.getProperty(key);
            if (value != null) {
                //上面的策略对象如果有多个的话,是以逗号来进行分割,所以这里就相当于按逗号split
                String[] classNames = StringUtils.commaDelimitedListToStringArray(value);
                //用于装返回结果的
                List&lt;T&gt; strategies = new ArrayList&lt;T&gt;(classNames.length);
                //然后将上面的分割出的String(这个String其实是实现类的全路径类名)依次遍历进行实例化传入装入strategies以便返回
                for (String className : classNames) {
                    try {
                        Class&lt;?&gt; clazz = ClassUtils.forName(className, DispatcherServlet.class.getClassLoader());
                        Object strategy = createDefaultStrategy(context, clazz);
                        strategies.add((T) strategy);
                    }
                    catch (ClassNotFoundException ex) {
                        throw new BeanInitializationException(
                                &quot;Could not find DispatcherServlet&#x27;s default strategy class [&quot; + className +
                                        &quot;] for interface [&quot; + key + &quot;]&quot;, ex);
                    }
                    catch (LinkageError err) {
                        throw new BeanInitializationException(
                                &quot;Error loading DispatcherServlet&#x27;s default strategy class [&quot; + className +
                                        &quot;] for interface [&quot; + key + &quot;]: problem with class file or dependent class&quot;, err);
                    }
                }
                return strategies;
            }
            else {
                return new LinkedList&lt;T&gt;();
            }
        }

     这个defaultStrategies是个什么鬼呢:

        /**
         * 
         * 这个是相对于DispatcherServlet为basePath的资源路径:DispatcherServlet.properties
         * 
         * Name of the class path resource (relative to the DispatcherServlet class)
         * that defines DispatcherServlet&#x27;s default strategy names.
         */
        private static final String DEFAULT_STRATEGIES_PATH = &quot;DispatcherServlet.properties&quot;;
    
        //配置文件加载到内存中
        private static final Properties defaultStrategies;
    
        static {
            /*
             * 这下面啰里啰嗦一大堆的意思就是这个存储策略映射的文件是程序内部使用的,并不提供开发人员自定义。
             */
            // Load default strategy implementations from properties file.
            // This is currently strictly internal and not meant to be customized
            // by application developers.
            try {
                //初始化defaultStrategies
                ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);
                defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
            }
            catch (IOException ex) {
                throw new IllegalStateException(&quot;Could not load &#x27;DispatcherServlet.properties&#x27;: &quot; + ex.getMessage());
            }
        }
    OK,现在已经很明朗了,去看看这个DispatcherServlet.properties究竟长啥样。

    位置:

    内容: 

    # Default implementation classes for DispatcherServlet&#x27;s strategy interfaces.
    # Used as fallback when no matching beans are found in the DispatcherServlet context.
    # Not meant to be customized by application developers.
    
    org.springframework.web.servlet.LocaleResolver=org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver
    
    org.springframework.web.servlet.ThemeResolver=org.springframework.web.servlet.theme.FixedThemeResolver
    
    org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,
    	org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
    
    org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,
    	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,
    	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
    
    org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver,
    	org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,
    	org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
    
    # 就是这一行定义了策略接口到策略对象的映射,一直没太搞明白策略模式到底是个什么鬼,现在看起来感觉也就那样吧... o(╯□╰)o
    org.springframework.web.servlet.RequestToViewNameTranslator=org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
    
    org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
    
    org.springframework.web.servlet.FlashMapManager=org.springframework.web.servlet.support.SessionFlashMapManager

     OK,我们分析完了这个东西究竟是怎么来的,再来分析一下它是怎么被调用的:

    /**
     * 
     * 将viewNameTranslator又封装了一层方法,根据提供的HttpServletRequest得到一个默认的视图名字
     * 
     * Translate the supplied request into a default view name.
     * @param request current HTTP servlet request
     * @return the view name (or {@code null} if no default found)
     * @throws Exception if view name translation failed
     */
    protected String getDefaultViewName(HttpServletRequest request) throws Exception {
        return this.viewNameTranslator.getViewName(request); 
    }

    这个getDefaultViewName(HttpServletRequest request)方法又在什么情况下会被调用呢,大概有两种情况,一个是正常的处理:

    在DispatcherServlet的doDispatch方法中有一句话:

    applyDefaultViewName(processedRequest, mv);

    这个方法的实现:

    /**
     * 
     * 当没有视图名的时候,使用viewNameTranslator得到一个视图名设置进去
     * 
     * Do we need view name translation?
     */
    private void applyDefaultViewName(HttpServletRequest request, ModelAndView mv) throws Exception {
        if (mv != null &amp;&amp; !mv.hasView()) {
            mv.setViewName(getDefaultViewName(request)); 
        }
    }

    总结一下:

    在正常的处理流程时会使用到viewNameTranslator来防止视图为空。

     

    第二种情况是在HandlerExceptionResolver处理异常的情况下:

    /**
     * 
     * 根据配置的HandlerExceptionResolvers来得到一个ModelAndView以决定异常发生时应该如何处理
     * 
     * Determine an error ModelAndView via the registered HandlerExceptionResolvers.
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler the executed handler, or {@code null} if none chosen at the time of the exception
     * (for example, if multipart resolution failed)
     * @param ex the exception that got thrown during handler execution
     * @return a corresponding ModelAndView to forward to
     * @throws Exception if no error ModelAndView found
     */
    protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
            Object handler, Exception ex) throws Exception {
    
        // Check registered HandlerExceptionResolvers...
        ModelAndView exMv = null;
        // 调用所有的异常处理器,知道有人能处理
        for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
            exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
            if (exMv != null) {
                break;
            }
        }
        
        if (exMv != null) {
            if (exMv.isEmpty()) {
                request.setAttribute(EXCEPTION_ATTRIBUTE, ex); 
                return null;
            }
            // 当异常没有明确的指定返回的视图名字的时候就要借助于RequestToViewNameTranslator来得到一个默认的视图名字
            // We might still need view name translation for a plain error model...
            if (!exMv.hasView()) {
                exMv.setViewName(getDefaultViewName(request));
            }
            if (logger.isDebugEnabled()) {
                logger.debug(&quot;Handler execution resulted in exception - forwarding to resolved error view: &quot; + exMv, ex);
            }
            WebUtils.exposeErrorRequestAttributes(request, ex, getServletName());
            return exMv;
        }
    
        // 如果没有HandlerExceptionResolver能够处理,就将异常继续往上抛
        throw ex;
    }

     OK,分析完了这个东西是如何被调用的,再来看一下它的代码实现:

    RequestToViewNameTranslator策略接口的代码分析:
    package org.springframework.web.servlet;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * 
     *  当处理的handler方法没有明确的返回视图名的时候,就会采用这个接口来得到视图名。
     *  这个接口采用了策略模式,会根据不同的情况使用不同的实现。
     * 
     * Strategy interface for translating an incoming
     * {@link javax.servlet.http.HttpServletRequest} into a
     * logical view name when no view name is explicitly supplied.
     *
     * @author Rob Harrop
     * @author Juergen Hoeller
     * @since 2.0
     */
    public interface RequestToViewNameTranslator {
    
        /**
         * 
         * 将HttpServletRequest转换为一个String类型的视图名字。
         * 
         * Translate the given {@link HttpServletRequest} into a view name.
         * @param request the incoming {@link HttpServletRequest} providing
         * the context from which a view name is to be resolved
         * @return the view name (or {@code null} if no default found)
         * @throws Exception if view name translation fails
         */
        String getViewName(HttpServletRequest request) throws Exception;
    
    }
    默认实现类(即策略对象)的代码实现:
    package org.springframework.web.servlet.view;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.util.Assert;
    import org.springframework.util.StringUtils;
    import org.springframework.web.servlet.RequestToViewNameTranslator;
    import org.springframework.web.util.UrlPathHelper;
    
    /**
     * 
     * 
     * RequestToViewNameTranslator类只是最简单的将请求的URI转换为视图名字。
     * 
     * 我们可以指定一个简单的viewNameTranslator,在没有明确指定返回的视图名字的时候就会使用默认的实现,
     * 默认的实现就是这个类...
     * 
     * 这个默认的转换规则是将头部和尾部的斜线以及扩展名去掉,然后可以配置前缀和后缀,这样子在处理完视图名字返回的时候会加上头尾的。
     * 
     * 使用到的一些参数都可以使用setter来进行设置,具体的方法是在配置文件中配置的时候传入即可。
     * 
     *  栗子:
     *  
     *  http://localhost:8080/gamecast/display.html --&gt; display
     *  http://localhost:8080/gamecast/displayShoppingCart.html --&gt; displayShoppingCart
     *  http://localhost:8080/gamecast/admin/index.html --&gt; admin/index
     * 
     *  规则:
     *      1. 取contextPath后面的requestURI
     *      2. 去掉头尾的斜线和.后面的东西
     *      3. 加上prefix和suffix返回作为视图名字
     * 
     * {@link RequestToViewNameTranslator} that simply transforms the URI of
     * the incoming request into a view name.
     *
     * &lt;p&gt;Can be explicitly defined as the {@code viewNameTranslator} bean in a
     * {@link org.springframework.web.servlet.DispatcherServlet} context.
     * Otherwise, a plain default instance will be used.
     *
     * &lt;p&gt;The default transformation simply strips leading and trailing slashes
     * as well as the file extension of the URI, and returns the result as the
     * view name with the configured {@link #setPrefix prefix} and a
     * {@link #setSuffix suffix} added as appropriate.
     *
     * &lt;p&gt;The stripping of the leading slash and file extension can be disabled
     * using the {@link #setStripLeadingSlash stripLeadingSlash} and
     * {@link #setStripExtension stripExtension} properties, respectively.
     *
     * &lt;p&gt;Find below some examples of request to view name translation.
     * &lt;ul&gt;
     * &lt;li&gt;{@code http://localhost:8080/gamecast/display.html} &amp;raquo; {@code display}&lt;/li&gt;
     * &lt;li&gt;{@code http://localhost:8080/gamecast/displayShoppingCart.html} &amp;raquo; {@code displayShoppingCart}&lt;/li&gt;
     * &lt;li&gt;{@code http://localhost:8080/gamecast/admin/index.html} &amp;raquo; {@code admin/index}&lt;/li&gt;
     * &lt;/ul&gt;
     *
     * @author Rob Harrop
     * @author Juergen Hoeller
     * @since 2.0
     * @see org.springframework.web.servlet.RequestToViewNameTranslator
     * @see org.springframework.web.servlet.ViewResolver
     */
    public class DefaultRequestToViewNameTranslator implements RequestToViewNameTranslator {
    
        private static final String SLASH = &quot;/&quot;;
    
        //在转换完后要加上的前缀
        private String prefix = &quot;&quot;;
    
        //在转换完后要加上的后缀
        private String suffix = &quot;&quot;;
    
        //转换完后的要使用的分隔符
        private String separator = SLASH;
    
        //是否去掉前面头部的斜线
        private boolean stripLeadingSlash = true;
    
        //是否去掉尾部的斜线
        private boolean stripTrailingSlash = true;
    
        //是否要去掉扩展名
        private boolean stripExtension = true;
    
        //工具类,在这里用来取出request URI
        private UrlPathHelper urlPathHelper = new UrlPathHelper();
    
    
        /**
         * Set the prefix to prepend to generated view names.
         * @param prefix the prefix to prepend to generated view names
         */
        public void setPrefix(String prefix) {
            this.prefix = (prefix != null ? prefix : &quot;&quot;);
        }
    
        /**
         * Set the suffix to append to generated view names.
         * @param suffix the suffix to append to generated view names
         */
        public void setSuffix(String suffix) {
            this.suffix = (suffix != null ? suffix : &quot;&quot;);
        }
    
        /**
         * Set the value that will replace &#x27;{@code /}&#x27; as the separator
         * in the view name. The default behavior simply leaves &#x27;{@code /}&#x27;
         * as the separator.
         */
        public void setSeparator(String separator) {
            this.separator = separator;
        }
    
        /**
         * Set whether or not leading slashes should be stripped from the URI when
         * generating the view name. Default is &quot;true&quot;.
         */
        public void setStripLeadingSlash(boolean stripLeadingSlash) {
            this.stripLeadingSlash = stripLeadingSlash;
        }
    
        /**
         * Set whether or not trailing slashes should be stripped from the URI when
         * generating the view name. Default is &quot;true&quot;.
         */
        public void setStripTrailingSlash(boolean stripTrailingSlash) {
            this.stripTrailingSlash = stripTrailingSlash;
        }
    
        /**
         * Set whether or not file extensions should be stripped from the URI when
         * generating the view name. Default is &quot;true&quot;.
         */
        public void setStripExtension(boolean stripExtension) {
            this.stripExtension = stripExtension;
        }
    
        /**
         * Set if URL lookup should always use the full path within the current servlet
         * context. Else, the path within the current servlet mapping is used
         * if applicable (i.e. in the case of a &quot;.../*&quot; servlet mapping in web.xml).
         * Default is &quot;false&quot;.
         * @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
         */
        public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
            this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
        }
    
        /**
         * Set if the context path and request URI should be URL-decoded.
         * Both are returned &lt;i&gt;undecoded&lt;/i&gt; by the Servlet API,
         * in contrast to the servlet path.
         * &lt;p&gt;Uses either the request encoding or the default encoding according
         * to the Servlet spec (ISO-8859-1).
         * @see org.springframework.web.util.UrlPathHelper#setUrlDecode
         */
        public void setUrlDecode(boolean urlDecode) {
            this.urlPathHelper.setUrlDecode(urlDecode);
        }
    
        /**
         * Set if &quot;;&quot; (semicolon) content should be stripped from the request URI.
         * @see org.springframework.web.util.UrlPathHelper#setRemoveSemicolonContent(boolean)
         */
        public void setRemoveSemicolonContent(boolean removeSemicolonContent) {
            this.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
        }
    
        /**
         * Set the {@link org.springframework.web.util.UrlPathHelper} to use for
         * the resolution of lookup paths.
         * &lt;p&gt;Use this to override the default UrlPathHelper with a custom subclass,
         * or to share common UrlPathHelper settings across multiple web components.
         */
        public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
            Assert.notNull(urlPathHelper, &quot;UrlPathHelper must not be null&quot;);
            this.urlPathHelper = urlPathHelper;
        }
    
    
        /**
         * 
         * 传入一个HttpServletRequest,根据这个请求的URI计算出返回的视图名字.
         * 
         * Translates the request URI of the incoming {@link HttpServletRequest}
         * into the view name based on the configured parameters.
         * @see org.springframework.web.util.UrlPathHelper#getLookupPathForRequest
         * @see #transformPath
         */
        @Override
        public String getViewName(HttpServletRequest request) {
            //得到request URI
            String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
            //然后转换路径并加入可自定义的prefix和suffix
            return (this.prefix + transformPath(lookupPath) + this.suffix);
        }
    
        /**
         * 
         * 将这个请求的URI的斜线和扩展名干掉,比如传入/index.htm,会将头部的斜线和.之后的htm干掉,返回的是index
         * 
         * Transform the request URI (in the context of the webapp) stripping
         * slashes and extensions, and replacing the separator as required.
         * @param lookupPath the lookup path for the current request,
         * as determined by the UrlPathHelper
         * @return the transformed path, with slashes and extensions stripped
         * if desired
         */
        protected String transformPath(String lookupPath) {
            String path = lookupPath;
            // 干掉头部的斜线分隔符,默认是要干掉的
            if (this.stripLeadingSlash &amp;&amp; path.startsWith(SLASH)) {
                path = path.substring(1);
            }
            // 干掉尾部的斜线分隔符,默认是干掉
            if (this.stripTrailingSlash &amp;&amp; path.endsWith(SLASH)) {
                path = path.substring(0, path.length() - 1);
            }
            //干掉扩展名,默认是干掉
            if (this.stripExtension) {
                path = StringUtils.stripFilenameExtension(path);
            }
            /* 如果分隔符不是斜线的话,就将所有的斜线转换为分隔符,上面对separator进行初始的时候是直接separator=SLASH的,
             * 所以如果不使用setSeparator(String separator)来自定义分隔符的话这一句是永远不会被执行的
             * * */
            if (!SLASH.equals(this.separator)) {
                path = StringUtils.replace(path, SLASH, this.separator);
            }
            return path;
        }
    
    }

    3. 自定义RequestToViewNameTranslator

    当没有明确指定返回视图名时使用我们自己的RequestToViewNameTranslator来进行处理。

    新建一个Class实现RequestToViewNameTranslator接口:

    package org.cc1100100.springmvc.study_001;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.web.servlet.RequestToViewNameTranslator;
    
    /**
     * 
     * 自定义的RequestToViewNameTranslator
     * 
     * @author chenjc20326
     *
     */
    public class FooRequestToViewNameTranslator implements RequestToViewNameTranslator{
    
        public String getViewName(HttpServletRequest request) throws Exception {
            //凡是没有明确返回视图名的一律跳转到defaultPage页面
            return &quot;defaultPage&quot;;
        }
    
    }

    然后将其在springmvc的配置文件中声明:

        <!-- 配置自定义的RequestToViewNameTranslator -->
        <bean name="viewNameTranslator" class="org.cc1100100.springmvc.study_001.FooRequestToViewNameTranslator" />

    然后就可以用啦,再当handler方法没有返回视图名的时候就会调用FooRequestToViewNameTranslator来进行处理。

      

     

    参考资料:

    1. spring-webmvc-4.3.2源代码。

     

  • 相关阅读:
    根据访问ip的地区跳转到指定地址
    js生成vCard,以及格式参数详细说明
    min_to_split_multiprocessing多进程,用于平时快速补充数据
    min_to_split.py按日存储到按个股存储
    readzip_minute_data 多进程处理数据
    打包成7zfile,to7zfile
    baostock_multiprocessing 多进程取数据
    终止阻塞线程(有共享锁的线程无效)
    readzip_add_maxL3多线程
    readzip_add_maxL2
  • 原文地址:https://www.cnblogs.com/cc11001100/p/6445428.html
Copyright © 2011-2022 走看看