91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot2.0整合同時支持jsp+html跳轉的案例

發布時間:2020-10-28 13:53:38 來源:億速云 閱讀:206 作者:小新 欄目:編程語言

小編給大家分享一下springboot2.0整合同時支持jsp+html跳轉的案例,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

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;

}

看完了這篇文章,相信你對springboot2.0整合同時支持jsp+html跳轉的案例有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

莱阳市| 淮南市| 海丰县| 林芝县| 西乌珠穆沁旗| 巴中市| 金阳县| 沙洋县| 大名县| 海南省| 鹿泉市| 沾化县| 霍城县| 进贤县| 万荣县| 松溪县| 璧山县| 鄂托克旗| 淅川县| 疏勒县| 视频| 玉林市| 砚山县| 同心县| 榆中县| 柏乡县| 奎屯市| 类乌齐县| 色达县| 镇巴县| 遵义县| 利川市| 衢州市| 平顺县| 顺义区| 岐山县| 青浦区| 交口县| 驻马店市| 霸州市| 界首市|