最新消息: 新版網站上線了!!!

springboot同時支持jsp和html,默認情況下,springboot是不支持jsp的訪問的

關于springboot和jsp的說明

  1. springboot不建議使用jsp。建議使用模板引擎。

  2. 你的html可以 直接使用jsp渲染

  3. 你也可以使用訪問靜態資源的方式去訪問html

  4. 你也可以直接讀取本地的html資源,使用OutpuStream響應給客戶端


默認情況下,springboot是不支持jsp的訪問的。那么本項目提供以下支持:

  • SpringBoot同時訪問html和jsp

  • SpringBoot以jar包運行

  • SpringBoot支持thymeleaf

項目結構:
.
├── 1.txt
├── pom.xml
├── springboot-jsp-html.iml
└── src
    ├── main
    │   ├── java
    │   │   └── org
    │   │       └── dreams
    │   │           └── forepart
    │   │               ├── Application.java
    │   │               ├── ViewResolverConfiguration.java
    │   │               └── controller
    │   │                   └── IndexController.java
    │   ├── resources
    │   │   └── application.properties
    │   └── webapp
    │       ├── html
    │       │   └── index.html
    │       ├── img
    │       │   └── orcHome.png
    │       └── jsp
    │           └── index.jsp
    └── test
        └── java

pom參數

<?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>

    <groupId>org.dreams</groupId>
    <artifactId>springboot-jsp-html</artifactId>
    <version>1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- 熱部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <scope>test</scope>
        </dependency>

        <!--jsp頁面使用jstl標簽-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!--Provided  start-->
        <!--War包部署到外部的Tomcat中已經包含了這些,所以需要添加以下依賴 否則會和內嵌的Tomcat 容器發生沖突 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <!--用于編譯jsp-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.2.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <!-- 打包時將jsp文件拷貝到META-INF目錄下-->
            <resource>
                <!-- 指定resources插件處理哪個目錄下的資源文件 -->
                <directory>src/main/webapp</directory>
                <!--注意此次必須要放在此目錄下才能被訪問到-->
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build></project>

多視圖實現的視圖解析器

package org.dreams.forepart;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import org.springframework.web.servlet.view.InternalResourceViewResolver;import org.thymeleaf.spring5.SpringTemplateEngine;import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;import org.thymeleaf.spring5.view.ThymeleafViewResolver;import org.thymeleaf.templateresolver.ITemplateResolver;@Configurationpublic class ViewResolverConfiguration {    @Configuration//用來定義 DispatcherServlet 應用上下文中的 bean
    @EnableWebMvc
    @ComponentScan("org.dreams.forepart")    public class WebConfig implements WebMvcConfigurer {        @Bean
        public ViewResolver viewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/jsp/");
            resolver.setSuffix(".jsp");
            resolver.setViewNames("*");
            resolver.setOrder(2);            return resolver;
        }        @Bean
        public ITemplateResolver templateResolver() {
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setTemplateMode("HTML5");
            templateResolver.setPrefix("/");
            templateResolver.setSuffix(".html");
            templateResolver.setCharacterEncoding("utf-8");
            templateResolver.setCacheable(false);            return templateResolver;
        }        @Bean
        public SpringTemplateEngine templateEngine() {
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setTemplateResolver(templateResolver());            // templateEngine
            return templateEngine;
        }        @Bean
        public ThymeleafViewResolver viewResolverThymeLeaf() {
            ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
            viewResolver.setTemplateEngine(templateEngine());
            viewResolver.setCharacterEncoding("utf-8");
            viewResolver.setOrder(1);
            viewResolver.setViewNames(new String[]{"html/*", "vue/*"});            return viewResolver;
        }        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/img/**").addResourceLocations("/img/");
            registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/" + "/static/");
        }
    }
}

頁面跳轉邏輯層

package org.dreams.forepart.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class IndexController {    @RequestMapping("/jsp")    public String jsp(Model model) {
        model.addAttribute("message", "this is index jsp page!");        return "index";
    }    @GetMapping("/html")    public String testThemleaf(Model model) {
        model.addAttribute("message", "this is index html page!");        return "html/index";
    }
}

jsp頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>
    <title>Title</title></head><body><img src="/img/orcHome.png"/>${message}</body></html>

html頁面

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Title</title></head><body><span th:text="${message}"></span></body></html>

運行入口

package org.dreams.forepart;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

測試訪問

 
http://localhost:8080/html


來源鏈接:https://www.orchome.com/1091


轉載請注明:谷谷點程序 » springboot同時支持jsp和html,默認情況下,springboot是不支持jsp的訪問的

体彩25选5开奖号码