springboot项目创建教程 https://blog.csdn.net/q18771811872/article/details/88126835

springboot2.0 跳转html教程 https://blog.csdn.net/q18771811872/article/details/88312862

springboot2.0 跳转jsp教程 https://blog.csdn.net/q18771811872/article/details/88342298

说明一下 。整合会遇到的问题,

1、pom.xml文件同时放入thymeleaf 架包和jsp支持后,  springboot的return模版会默认跳转到html  ,

那怕是你并没有配置thymeleaf的属性

解决方案,  使用getRequestDispatcher方法来跳转到jsp页面, 就同时支持html和jsp了

request.getRequestDispatcher("/WEB-INF/views/testJsp.jsp").forward(request, response);

2、另外 使用getRequestDispatcher跳转到html页面的时候,thymeleaf 模版接收参数可能会出现问题。

解决方案1:html放弃使用thymeleaf 模版,然后在页面主动请求接口数据(AJAX POST等)

解决方案2:html继续使用thymeleaf 模版,用return模版 返回来跳转页面

代码
UserController.java

package com.example.demo.controller;

import com.example.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* @author chenlin
*/
@Controller
@RequestMapping("/usersDemo")
public class UserController {
private static Logger log = LoggerFactory.getLogger(UserController.class);
@Resource
UserService userService; @ResponseBody
@RequestMapping(value = "/test", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
public List<Map<String, Object>> test(){
log.info("进入了test方法!");
List<Map<String,Object>> list=userService.userQueryAll();
return list;
}
@RequestMapping(value = "/testHtml", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
public String testHtml(HttpServletRequest request, HttpServletResponse response){
List<Map<String,Object>> list=userService.userQueryAll();
request.setAttribute("list",list);
log.info("进入了testHtml方法!");
return "views/testHtml";
}
@RequestMapping(value = "/testJsp", produces = "application/json;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
public void testJsp( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Map<String,Object>> list=userService.userQueryAll();
request.setAttribute("list",list);
log.info("进入了testJsp方法!");
request.getRequestDispatcher("/WEB-INF/views/testJsp.jsp").forward(request, response);
}
}

配置文件

server:
port: 8080
tomcat:
uri-encoding: UTF-8
servlet:
context-path: /
spring:
dataSource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/db-test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&usessl=false
username: root
password: 123456
driverClassName: com.mysql.jdbc.Driver
mvc:
view: #新版本 1.3后可以使用
suffix: .jsp
prefix: /WEB-INF/
view: #老版本 1.4后被抛弃
suffix: .jsp
prefix: /WEB-INF/
thymeleaf:
#清除缓存
cache: false
mode: LEGACYHTML5 #非严格模式
prefix: /WEB-INF/ #默认 classpath:/templates/
suffix: .html
servlet:
content-type: text/html
mybatis:
mapper-locations: classpath:com/example/demo/mapper/*Mapper.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.example.demo.model # 注意:对应实体类的路径
configuration:
call-setters-on-nulls: true # 解决使用map类型接收查询结果的时候为null的字段会没有的情况

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<mysql.version>5.1.47</mysql.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- alibaba的druid数据库连接池监控依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.13</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--thymeleaf模版-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--非严格模式下 规避一些html编译错误 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<!--tomcat支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!--servlet依赖.-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--jsp标签库-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies> <build>
<resources>
<!--解决mybatis文件不编译问题-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<!--解决实体类启动和jar启动web页面会报404的错误-->
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
</project>

以上就完了。

另外附加一个return 模版的java代码配置, 可以配置return模版的优先级,后面的文件格式,当然只能用getRequestDispatcher来跳转了

在启动类中添加,另外,配置文件参数和代码可重复    但是代码优先于配置文件。

/**
* 添加对jsp支持
*
*/
@Bean
public ViewResolver getJspViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/WEB-INF/");//前缀
internalResourceViewResolver.setSuffix(".jsp");//后缀
internalResourceViewResolver.setOrder(0);//优先级
return internalResourceViewResolver;
} /**
* 添加对Freemarker支持
*
*/
@Bean
public FreeMarkerViewResolver getFreeMarkerViewResolver() {
FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
freeMarkerViewResolver.setCache(false);
freeMarkerViewResolver.setPrefix("/WEB-INF/");//前缀
freeMarkerViewResolver.setSuffix(".html");//后缀
freeMarkerViewResolver.setRequestContextAttribute("request");
freeMarkerViewResolver.setOrder(1);//优先级
freeMarkerViewResolver.setContentType("text/html;charset=UTF-8");
return freeMarkerViewResolver; }

springboot项目创建教程 https://blog.csdn.net/q18771811872/article/details/88126835

springboot2.0 跳转html教程 https://blog.csdn.net/q18771811872/article/details/88312862

springboot2.0 跳转jsp教程 https://blog.csdn.net/q18771811872/article/details/88342298

原文地址 https://blog.csdn.net/q18771811872/article/details/88343672

springboot 2.0 整合 同时支持jsp+html跳转的更多相关文章

  1. SpringBoot 2.0 整合sharding-jdbc中间件,实现数据分库分表

    一.水平分割 1.水平分库 1).概念: 以字段为依据,按照一定策略,将一个库中的数据拆分到多个库中. 2).结果 每个库的结构都一样:数据都不一样: 所有库的并集是全量数据: 2.水平分表 1).概 ...

  2. SpringBoot 2.0整合阿里云OSS,实现动静分离架构

    前言 相信大部分开发者对下面这张架构图并不陌生吧,现在很多网站/应用都采用了动静分离的架构进行部署.博主的博客也不例外,主机采用的是阿里云的 ECS,使用 CDN 做静态内容分发,不过静态文件还是存储 ...

  3. springboot 2.0 整合 RestTemplate

    首先导入springboot 的 web 包 <dependency> <groupId>org.springframework.boot</groupId> &l ...

  4. springboot 2.0+整合RabbitMQ

    基于spring-boot 2.* 作用: 1.异步处理 2.应用解耦 3.流量削峰   相关概念介绍: Broker:它提供一种传输服务,它的角色就是维护一条从生产者到消费者的路线,保证数据能按照指 ...

  5. idea创建 springboot工程(支持jsp)

    以前学springboot以前想搭建一个支持jsp的项目一直弄不上,现在发现用maven创建一个项目然后改成springboot效果一样的 https://blog.csdn.net/gisboygo ...

  6. springBoot整合mybatis、jsp 或 HTML

    springBoot整合mybatis.jsp Spring Boot的主要优点: 1:  为所有Spring开发者更快的入门: 2:  开箱即用,提供各种默认配置来简化项目配置: 3:  内嵌式容器 ...

  7. springboot 2.0.8 跳转jsp页面

    springboot项目创建教程 https://blog.csdn.net/q18771811872/article/details/88126835 springboot 2.0跳转 html教程 ...

  8. SpringBoot(二)-- 支持JSP

    SpringBoot虽然支持JSP,但是官方不推荐使用.看网上说,毕竟JSP是淘汰的技术了,泪奔,刚接触 就淘汰.. SpringBoot集成JSP的方法: 1.配置application.prope ...

  9. 玩转 SpringBoot 2 快速整合 | JSP 篇

    前言 JavaServer Pages(JSP)技术使Web开发人员和设计人员能够快速开发和轻松维护利用现有业务系统的信息丰富的动态Web页面. 作为Java技术系列的一部分,JSP技术可以快速开发独 ...

随机推荐

  1. 批处理文件执行cmd命令

    @echo offstart "wumin" "C:\Windows\System32\cmd.exe" osk taskkill /f /im cmd.exe ...

  2. Redis实现之对象(三)

    集合对象 集合对象的编码可以是intset或者hashtable,intset编码的集合对象使用整数集合作为底层实现,集合对象包含的所有元素都被保存在整数集合里面.举个栗子,以下代码将创建一个图1-1 ...

  3. This application has request the Runtime to terminate it in an unusual way.

    Q: CertsMV.exe  gui popup two dialogs as follow. A: 测试发现是分配内存导致,频繁分配内存(大约6M) 可能是堆管理导致 分配大内存分配失败,程序未对 ...

  4. net user

    net user 编辑 Net User命令是一个DOS命令,必须在Windows nt以上系统的MS-DOS模式下运行,所以首先要进入MS-DOS模式:选择“开始”菜单的“附件”选项的子选项“命令提 ...

  5. Python学习之正则表达式初探

    正则表达式 正则表达式 (或 regexes ) 是通用的文本模式匹配的方法. Django URLconfs 允许你 使用任意的正则表达式来做强有力的URL映射,不过通常你实际上可能只需要使用很少的 ...

  6. 程序集链接器(AL.exe)

    AL.exe使用程序可以生成一个EXE文件或者DLL PE文件(其中只包含对其他模块中的类型进行描述的一个清单). 不要在普通的命令行窗口中编译,请先打开C:\ProgramData\Microsof ...

  7. CSU-1980 不堪重负的树

    CSU-1980 不堪重负的树 Description 小X非常喜欢树,然后他生成了一个大森林给自己玩. 玩着玩着,小X陷入了沉思. 一棵树由N个节点组成,编号为i的节点有一个价值Wi. 假设从树根出 ...

  8. caffe的python接口提取resnet101某层特征

    论文的caffemodel转化为tensorflow模型过程中越坑无数,最后索性直接用caffe提特征. caffe提取倒数第二层,pool5的输出,fc1000层的输入,2048维的特征 #codi ...

  9. 三种框架对比react vue 和Angular对比

    https://blog.csdn.net/runOnWay/article/details/80103880 angular 优点 背靠谷歌 使用typescript 便于后端人员开发上手 完整 不 ...

  10. 爬虫:Scrapy6 - Item Loaders

    Item Loaders 提供了一种便捷的方式填充抓取到的:Items.虽然 Items 可以使用自带的类字典形式的 API 填充,但是 Item Loaders 提供了更便捷的 API,可以分析原始 ...