springboot2.0整合同时支持jsp+html跳转(代码片段)

zhaoyan001 zhaoyan001     2022-12-12     387

关键词:

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

springboot2.0之整合elasticsearch

就类比数据库到时候去实现服务器端配置集群名字 与yml名字一致pom:<projectxmlns="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.0ht 查看详情

springboot2.0之整合rabbitmq

案例: Springboot对RabbitMQ的支持公共的pom: <projectxmlns="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 查看详情

springboot2.0整合fastjson的正确姿势(代码片段)

    SpringBoot2.0如何集成fastjson?在网上查了一堆资料,但是各文章的说法不一,有些还是错的,可能只是简单测试一下就认为ok了,最后有没生效都不知道。恰逢公司项目需要将JackSon换成fastjson,因此自己来实践一... 查看详情

springboot2:springboot2.0新特性

SpringBoot2(一):SpringBoot2.0新特性SpringBoot依赖于Spring,而SpringCloud又依赖于SpringBoot,因此SpringBoot2.0的发布正式整合了Spring5.0的很多特性,同样后面SpringCloud最新版本的发布也需要整合最新的SpringBoot2.0内容。一、新版本特性1,基于J... 查看详情

springboot2.0整合freemarker快速入门

目录1.快速入门1.1创建工程pom.xml文件如下1.2编辑application.yml1.3创建模型类1.4创建模板1.5创建controller1.6测试2.FreeMarker基础2.1数据模型2.2List指令2.3遍历Map数据2.4if指令2.5运算符2.6空值处理2.7内建函数freemarker是一个用Java开发的模板引... 查看详情

springboot2.0应用:springboot2.0整合rabbitmq(代码片段)

如何整合RabbitMQ1、添加spring-boot-starter-amqp<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>2、添加配置s 查看详情

springboot2.0.4整合springdatajpa和druid,双数据源

最近Team开始尝试使用SpringBoot+SpringDataJPA作为数据层的解决方案,在网上逛了几圈之后发现大家并不待见JPA,理由是(1)MyBatis简单直观够用,(2)以Hibernate为底层的SpringDataJPA复杂且性能一般。但是当我们来到SpringBoot的世界后发现,... 查看详情

springboot2.0.8跳转html页面

springboot项目创建链接 https://blog.csdn.net/q18771811872/article/details/88126835springboot2.0跳转jsp教程 https://blog.csdn.net/q18771811872/article/details/88342298jsp+html跳转整合 https://blog.csd 查看详情

springboot2.0源码分析:整合activemq分析(代码片段)

SpringBoot具体整合ActiveMQ可参考:SpringBoot2.0应用(二):SpringBoot2.0整合ActiveMQActiveMQ自动注入当项目中存在javax.jms.Message和org.springframework.jms.core.JmsTemplate着两个类时,SpringBoot将ActiveMQ需要使用到的对象注册为Bean,供项目注入使用... 查看详情

springboot2.0整合jpa

在整合的遇到各种坑,以下是我整合的流程1、pom.xml文件<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency& 查看详情

springboot2.0整合mybatis

pom.xml<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </paren 查看详情

springboot2.0之整合redis

需要的maven依赖jar包,是对Jedis的封装maven依赖:<projectxmlns="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.0http://ma 查看详情

springboot2.0整合es的异常总结

异常:availableProcessorsisalreadysetto[4],rejecting[4]在启动类中加入System.setProperty("es.set.netty.runtime.available.processors","false");异常:org.elasticsearch.client.transport.NoNodeAvailableException:Noneof 查看详情

springboot2.0实战(23)整合redis之集成缓存springdatacache

SpringBoot2.0实战(23)整合Redis之集成缓存SpringDataCache来源:https://www.toutiao.com/a6803168397090619908/?timestamp=1584450221&app=news_article_lite&group_id=6803168397090619908&req_id=20200317210341 查看详情

springboot2.0+shiro+jwt整合(代码片段)

SpringBoot2.0+Shiro+JWT整合JSONWebToken(JWT)是一个非常轻巧的规范。这个规范允许我们使用JWT在用户和服务器之间传递安全可靠的信息。我们利用一定的编码生成Token,并在Token中加入一些非敏感信息,将其传递。安装环境开发工具:... 查看详情

学习整合springboot2.0和mybatis,实现基本的crud

前言:本文是在自己整合springboot2.0和mybatis时的过程和踩得坑。先附上github地址:https://github.com/yclxt/springboot-mybatis.git环境/版本:工具:IntellijIDEA2018.3JDK:1.8Springboot:2.0.4.RELEASEMybatis:1.3.2由于本人是初学者,对druid和handlePage不太熟 查看详情

springboot2.0之整合activemq(点对点模式)

<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xs 查看详情

springboot2.0最新版相关技术

https://gitee.com/hejr.cn.com/SpringBoot2.0_2019/tree/master SpringBoot2.0最新版相关技术学习,该项目主要是提供给正在学习SpringBoot的小伙伴,包括整合Freemarker,JSP等web页面,以及数据访问,事务管理,日志管理,缓存(Ehcache,Redis)技术... 查看详情