我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

SpringBoot同时整合thymeleafhtml、vuehtml和jsp-在线助手

问题描述
SpringBoot如何同时访问html和jsp
SpringBoot访问html页面可以,访问jsp页面报错
SpringBoot如何同时整合thymeleaf html、vue html和jsp
java web spring mvc项目如何同时访问html和jsp


解决办法:配置多视图实现的视图解析器

步骤一、新建一个ViewResolverConfiguration,下面是网站在线助手 的具体实现:

/**
 * 主要配置多视图实现的视图解析器相关bean实例
 *
 * http://www.it399.com/
 *
 * 其实关键点在于两个:
 * 1、配置order属性
 * 2、配置viewnames属性
 *
 * 注意:
 * return new ModelAndView("jsps/index");//或者return "jsps/index"
 * 对应 /WEB-INF/jsps/index.jsp
 * ==========================
 * 同理:
 * return "thymeleaf/index";//或者return “thymeleaf/index”
 * 对应 /WEB-INF/thymeleaf/index.html
 *
 *
 */
@Configuration
public class ViewResolverConfiguration {

    @Configuration//用来定义 DispatcherServlet 应用上下文中的 bean
    @EnableWebMvc
    @ComponentScan("com.csy.spring")
    public class WebConfig extends WebMvcConfigurerAdapter {
        @Bean
        public ViewResolver viewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
//            resolver.setPrefix("/WEB-INF/");
//            resolver.setSuffix(".jsp");
//            resolver.setViewNames("jsps/*");
            resolver.setPrefix("/");
            resolver.setSuffix(".jsp");
            resolver.setViewNames("*");
            resolver.setOrder(2);
            return resolver;
        }

        @Bean
        public ITemplateResolver templateResolver() {
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setTemplateMode("HTML5");
            templateResolver.setPrefix("/templates/");
            templateResolver.setSuffix(".html");
            templateResolver.setCharacterEncoding("utf-8");
            templateResolver.setCacheable(false);
            return templateResolver;
        }

        @Bean
        public SpringTemplateEngine templateEngine() {
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setTemplateResolver(templateResolver());
            // templateEngine
            return templateEngine;
        }

        @Bean
        public ThymeleafViewResolver viewResolverThymeLeaf() {
            ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
            viewResolver.setTemplateEngine(templateEngine());
            viewResolver.setCharacterEncoding("utf-8");
            viewResolver.setOrder(1);
            //viewResolver.setViewNames(new String[]{"thyme/*"});
            viewResolver.setViewNames(new String[]{"thymeleaf/*","vue/*"});
            return viewResolver;
        }

        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            super.addResourceHandlers(registry);
        }
    }


}

步骤二、测试类:

@Controller
public class IndexController {
    @GetMapping("/testJsp")
    public String testJsp(Model model) {
        model.addAttribute("message", "this is index jsp page");
        return "index";
    }

    @GetMapping("/testThemleaf")
    public String testThemleaf(Model model) {
        model.addAttribute("message", "this is index jsp page");
        return "thymeleaf/test";
    }

    @GetMapping("/testVue")
    public String testVue(Model model) {
        model.addAttribute("message", "this is index jsp page");
        return "vue/testVue";
    }


}


测试结果:访问 在线助手|在线工具|在线生成|在线制作,或者输入以下链接

jsp页面 http://www.it399.com/index.jsp
thymeleaf模板页面 http://www.it399.com/blog/web/201805031726
vue搭建的页面 http://www.it399.com/blog/web/Web201805041746

均正常显示

这样springboot就可以同时支持访问jsp页面和html页面了。


本文同步发布在 在线助手|在线工具|在线生成|在线制作,转载请注明来自 在线助手 博客频道【SpringBoot 同时整合thymeleaf html、vue html和jsp】,原文链接 http://www.it399.com/blog/web/201805041746
网站标题:SpringBoot同时整合thymeleafhtml、vuehtml和jsp-在线助手
网站URL:http://shouzuofang.com/article/jjdidc.html

其他资讯