springboot 配置返回多个视图

自己测试成功!不喜忽喷。直接上代码。

第一步:

package com.example.demo.base.web;

import java.io.File;
import java.util.Locale;
import org.springframework.web.servlet.view.InternalResourceView;

public class HandleResourceViewExists extends InternalResourceView{
public boolean checkResource(Locale locale) {
File file = new File(this.getServletContext().getRealPath("/") + getUrl());
return file.exists(); //判断页面是否存在
}
}

第二步:

package com.example.demo.base.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan("com.example.demo")//扫描的包 
public class WebConfig {

@Bean
public InternalResourceViewResolver htmlViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/html/");
viewResolver.setViewClass(HandleResourceViewExists.class); //设置检查器
viewResolver.setSuffix(".html");
viewResolver.setOrder(0);
viewResolver.setContentType("text/html;charset=UTF-8");
return viewResolver;
}

@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(HandleResourceViewExists.class); //设置检查器
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
viewResolver.setOrder(0);
viewResolver.setContentType("text/html;charset=UTF-8");
return viewResolver;
}

@Bean
public InternalResourceViewResolver ftlViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(HandleResourceViewExists.class); //设置检查器
viewResolver.setPrefix("/ftl/");
viewResolver.setSuffix(".ftl");
viewResolver.setOrder(0);
viewResolver.setContentType("text/html;charset=UTF-8");
return viewResolver;
}

}

第三步:controller层

package com.example.demo.home.web.top;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* 首页导航栏
* @author zzh
*
*/
@Controller
@RequestMapping("home/top")
public class NavigationBarController {
private static final String jsp="jsp/home/top/";
private static final String html="html/home/top/";
private static final String ftl="ftl/home/top/";
/**
* 用户访问首页无登录状态显示
* @param m
* @return
*/
@RequestMapping("jsp")
public String NavigationBar(Model m) {
m.addAttribute("list","123");
String string = jsp+"index";
System.out.println(string);
return jsp+"index";
}

@RequestMapping("html")
public String html(Model m) {
m.addAttribute("ls","123");
return html+"index";
}

@RequestMapping("ftl")
public String ftl(Model m) {
m.addAttribute("ls","123");
return ftl+"index";
}

}

 

posted @ 2019-06-05 10:31  小新随手记2  阅读(1210)  评论(0编辑  收藏  举报