springboot 2.0 整合 同时支持jsp+html跳转

2019-03-10 11:56:13来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

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


原文链接:https://www.cnblogs.com/cl133/p/10495663.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:JDK 和 JRE 有什么区别

下一篇:两张图彻底搞懂MyBatis的Mapper原理!