How to map requests to HTML file in Spring MVC?

前端 未结 7 1169
你的背包
你的背包 2020-11-29 20:44

Basic configuration files looks unintuitive.

If I create simple hello world example, and then rename home.jsp to home.html and edit

7条回答
  •  死守一世寂寞
    2020-11-29 21:13

    well, it seems you didn' t set the view' s order.

    for example, if your project has view like jsp, json, velocity, freemarker, etc. you can use all of them(maybe you need new version of spring, 3.1+), but only one view will be select to render to client, that depends on your view' s order, the lower the order, the prefer the view.

    for example, you set jsp view' s order is 1, and freemarker view' s order is 2, both of their view name is "home", the spring will choose view.jsp(if you set suffix to .jsp). Well, if your view name is "index", no index.jsp but index.ftl(suppose you set freemarker' s view to .ftl), spring will choose the later.

    the sample code using spring' s java config, you can easily convert to xml-style.

    @Bean
    public InternalResourceViewResolver jspViewResolver() {
        InternalResourceViewResolver jsp = new InternalResourceViewResolver();
        jsp.setOrder(4);
        jsp.setCache(true);
        jsp.setViewClass(org.springframework.web.servlet.view.JstlView.class);
        jsp.setPrefix("/WEB-INF/jsp/");
        jsp.setSuffix(".jsp");
        return jsp;
    }
    
    @Bean
    public FreeMarkerViewResolver freeMarkerViewResolver() {
        FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
        viewResolver.setCache(true);
        viewResolver.setPrefix("");
        viewResolver.setSuffix(".ftl");
        viewResolver.setContentType(ViewConstants.MEDIA_TYPE_HTML);
        viewResolver.setRequestContextAttribute("request");
        viewResolver.setExposeSpringMacroHelpers(true);
        viewResolver.setExposeRequestAttributes(true);
        viewResolver.setExposeSessionAttributes(true);
        viewResolver.setOrder(2);
        return viewResolver;
    }
    

    please see the setOrder() method!

    the json, jsonp and other type of views may use ontentNegotiation, and you can find it on spring' s docs.

    finally, the html view, I mean, totally static files, which is not support by spring default. I suppose the static file desn' t need render by java. you can use the static mapping using the code below:

    
    

    or use java config:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        int cachePeriod = 3600 * 24 * 15;
        registry.addResourceHandler("/static/**").addResourceLocations("/static/").setCachePeriod(cachePeriod);
        registry.addResourceHandler("/favicon.ico").addResourceLocations("/").setCachePeriod(cachePeriod);
        registry.addResourceHandler("/robots.txt").addResourceLocations("/").setCachePeriod(cachePeriod);
    }
    

    and in your @RequestMapping method, you should redirect it!

    well, if you don' t want redirection, just set the html view to an dynamic view (freemark, velecity, etc), which will be ok!

    hope it useful!

提交回复
热议问题