碼迷,www.tparu.icu
吉利平特名人堂 > 編程語言 > 詳細

平特一肖王中王四字成语:springboot 2.0 整合 同時支持jsp+html跳轉

時間:2019-05-23 18:23:34      閱讀:14      評論:0      收藏:0      [點我收藏+]

吉利平特名人堂 www.tparu.icu 標簽:com   string   res   注意   code   factor   監控   jstl   ams   

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="//maven.apache.org/POM/4.0.0" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="//maven.apache.org/POM/4.0.0 //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

springboot 2.0 整合 同時支持jsp+html跳轉

標簽:com   string   res   注意   code   factor   監控   jstl   ams   

原文地址:https://www.cnblogs.com/zhaoyan001/p/10913538.html

(0)
(0)
   
舉報
評論 一句話評論(0
0條  
登錄后才能評論!
? 2014 吉利平特名人堂 版權所有 京ICP備13008772號-2
迷上了代碼!
三分pk10官方网站 时时彩和值走势图 新疆时时走势图下载 销售单打印软件 压龙虎稳赢法 捕鱼达人2有卡片的版本下载 北京pk赛海盗计划 pk10最牛稳赚模式8码怎么打 时时彩大小单双玩法 北京pk106码如何倍投 时时彩历史开奖结果查询 新时时任选 彩神计划软件破解版赚钱 彩票代投注挣佣金 福彩欢乐生肖走势图 双色球预测专家最准确