热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

springboot配置返回多个视图

自己测试成功!不喜忽喷。直接上代码。第一步:packagecom.example.demo.base.web;importjava.io.File;importjava.util.

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

第一步:

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";
}

}

 


推荐阅读
author-avatar
mobiledu2502924103
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有