[SpringMVC]通过视图解析器自定义请求转发前缀后缀

正常使用Controller进行请求转发时,要return页面路径名,操作繁琐,可以通过配置文件实现更改默认前缀后缀。

@Controller
public class QuickStartController {

    @RequestMapping("/quick")
    public String QuickStart(){
        System.out.println("QuickStart Working!");
        return "/jsp/QuickTest.jsp";
        //return "QuickTest";
    }

}

1.从DispatcherServlet.properties中找到默认ViewResolver

properties路径如下:

\org\springframework\spring-webmvc\5.3.8\spring-webmvc-5.3.8.jar!\org\springframework\web\servlet\DispatcherServlet.properties

DispatcherServlet.properties:

# Default implementation classes for DispatcherServlet'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.method.annotation.RequestMappingHandlerMapping,\
	org.springframework.web.servlet.function.support.RouterFunctionMapping

org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter,\
	org.springframework.web.servlet.function.support.HandlerFunctionAdapter


org.springframework.web.servlet.HandlerExceptionResolver=org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver,\
	org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver,\
	org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver

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

其中倒数第二行的:

org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver

就是我们要找的ViewResolver,内容如下
InternalResourceViewResolver:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.web.servlet.view;

import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;

public class InternalResourceViewResolver extends UrlBasedViewResolver {
    private static final boolean jstlPresent = ClassUtils.isPresent("javax.servlet.jsp.jstl.core.Config", InternalResourceViewResolver.class.getClassLoader());
    @Nullable
    private Boolean alwaysInclude;

    public InternalResourceViewResolver() {
        Class<?> viewClass = this.requiredViewClass();
        if (InternalResourceView.class == viewClass && jstlPresent) {
            viewClass = JstlView.class;
        }

        this.setViewClass(viewClass);
    }

    public InternalResourceViewResolver(String prefix, String suffix) {
        this();
        this.setPrefix(prefix);
        this.setSuffix(suffix);
    }

    public void setAlwaysInclude(boolean alwaysInclude) {
        this.alwaysInclude = alwaysInclude;
    }

    protected Class<?> requiredViewClass() {
        return InternalResourceView.class;
    }

    protected AbstractUrlBasedView instantiateView() {
        return (AbstractUrlBasedView)(this.getViewClass() == InternalResourceView.class ? new InternalResourceView() : (this.getViewClass() == JstlView.class ? new JstlView() : super.instantiateView()));
    }

    protected AbstractUrlBasedView buildView(String viewName) throws Exception {
        InternalResourceView view = (InternalResourceView)super.buildView(viewName);
        if (this.alwaysInclude != null) {
            view.setAlwaysInclude(this.alwaysInclude);
        }

        view.setPreventDispatchLoop(true);
        return view;
    }
}

2.Ctrl+左键 点入该类的父类UrlBasedViewResolver:

成员列表如下:

	public static final String REDIRECT_URL_PREFIX = "redirect:";
    public static final String FORWARD_URL_PREFIX = "forward:";
    @Nullable
    private Class<?> viewClass;
    private String prefix = "";
    private String suffix = "";
    @Nullable
    private String contentType;
    private boolean redirectContextRelative = true;
    private boolean redirectHttp10Compatible = true;
    @Nullable
    private String[] redirectHosts;
    @Nullable
    private String requestContextAttribute;
    private final Map<String, Object> staticAttributes = new HashMap();
    @Nullable
    private Boolean exposePathVariables;
    @Nullable
    private Boolean exposeContextBeansAsAttributes;
    @Nullable
    private String[] exposedContextBeanNames;
    @Nullable
    private String[] viewNames;
    private int order = 2147483647;

这里的:

    private String prefix = "";
    private String suffix = "";

就是我们要设置的请求转发前后缀。

3.写SpringMVC配置文件

	<bean id="InternalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

设置路径前缀:

name="prefix" value="/jsp/" 

设置路径后缀:

name="suffix" value=".jsp"

通过 return "QuickTest" 即可实现请求转发

@Controller
public class QuickStartController {

    @RequestMapping("/quick")
    public String QuickStart(){
        System.out.println("QuickStart Working!");
        //return "/jsp/QuickTest.jsp";
        return "QuickTest";
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值