Support jsp+html jump in the whole contract of springboot 2.0

Spring boot project creation tutorial https://blog.csdn.net/q18771811872/article/details/88126835

springboot2.0 jump to html tutorial https://blog.csdn.net/q18771811872/article/details/88312862

Spring boot 2.0 jump to jsp tutorial https://blog.csdn.net/q18771811872/article/details/88342298

Explain. Integration issues,

1. The return template of springboot will jump to html by default after the pom.xml file is put into the thymeleaf package and jsp support,

Even if you don't configure the properties of thmeleaf

Solution: use getRequestDispatcher method to jump to jsp page, and support html and jsp at the same time

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

2. In addition, when using getRequestDispatcher to jump to the html page, there may be problems with the receiving parameters of the thmeleaf template.

Solution 1: html abandons the use of thmeleaf template, and then actively requests interface data on the page (AJAX POST, etc.)

Solution 2: html continues to use the thmeleaf template, and return s the template to jump to the page

Code
 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("Entered test Method!");
        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("Entered testHtml Method!");
        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("Entered testJsp Method!");
        request.getRequestDispatcher("/WEB-INF/views/testJsp.jsp").forward(request, response);
    }
}

configuration file

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: #Available after new version 1.3
      suffix: .jsp
      prefix: /WEB-INF/
  view: #Old version 1.4 abandoned
    suffix: .jsp
    prefix: /WEB-INF/
  thymeleaf:
    #Clear cache
    cache: false
    mode: LEGACYHTML5 #Non strict mode
    prefix: /WEB-INF/ #Default classpath:/templates/
    suffix: .html
    servlet:
      content-type: text/html
mybatis:
  mapper-locations: classpath:com/example/demo/mapper/*Mapper.xml #Note: be sure to correspond to the path of the mapper mapping xml file
  type-aliases-package: com.example.demo.model # Note: the path of the corresponding entity class
  configuration:
    call-setters-on-nulls: true # Solve the problem that there is no null field when using map type to receive query results

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 Of druid Database connection pool monitoring dependency -->
        <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 Template-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--Avoid some under non strict mode html Compile error -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
        <!--tomcat Support-->
        <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 rely on.-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!--jsp Tag library-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <!--Solve mybatis File does not compile-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <!--The 404 error will be reported when the entity class starts and the jar starts the web page -- >
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

That's all.  

In addition, a return template java code configuration is attached. You can configure the priority of the return template. The following file format, of course, can only be skipped by getRequestDispatcher

In addition, the profile parameters and code can be repeated, but the code takes precedence over the profile.  

/**
* Add support for jsp
*
*/
@Bean
public ViewResolver getJspViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/WEB-INF/");//prefix
internalResourceViewResolver.setSuffix(".jsp");//Suffix
internalResourceViewResolver.setOrder(0);//priority
return internalResourceViewResolver;
}

/**
* Add support for Freemarker
*
*/
@Bean
public FreeMarkerViewResolver getFreeMarkerViewResolver() {
FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
freeMarkerViewResolver.setCache(false);
freeMarkerViewResolver.setPrefix("/WEB-INF/");//prefix
freeMarkerViewResolver.setSuffix(".html");//Suffix
freeMarkerViewResolver.setRequestContextAttribute("request");
freeMarkerViewResolver.setOrder(1);//priority
freeMarkerViewResolver.setContentType("text/html;charset=UTF-8");
return freeMarkerViewResolver;

}

 

 

Spring boot project creation tutorial https://blog.csdn.net/q18771811872/article/details/88126835

springboot2.0 jump to html tutorial https://blog.csdn.net/q18771811872/article/details/88312862

Spring boot 2.0 jump to jsp tutorial https://blog.csdn.net/q18771811872/article/details/88342298

Original address https://blog.csdn.net/q18771811872/article/details/88343672

Tags: Java Spring JSP MySQL

Posted on Tue, 03 Dec 2019 08:58:49 -0500 by Anxious