• springboot 配置jsp支持


    springboot默认并不支持jsp模板,所以需要配置。

    下面是一个可以运行的例子:

    首先配置属性文件:

    复制代码
    spring.http.encoding.force=true
    spring.http.encoding.charset
    =UTF-8
    spring.http.encoding.enabled
    =true
    server.tomcat.uri
    -encoding=UTF-8

    server.port=8080

    spring.mvc.view.prefix: /WEB-INF/jsp/

    spring.mvc.view.suffix: .jsp

    application.message: Hello world

    复制代码

    然后配置启动类:

    复制代码
    package sample.jsp;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.boot.builder.SpringApplicationBuilder;

    import org.springframework.boot.web.support.SpringBootServletInitializer;

    @SpringBootApplication

    public class Application extends SpringBootServletInitializer {

    @Override</br>
    </span><span style="color: #0000ff;">protected</span><span style="color: #000000;"> SpringApplicationBuilder configure(SpringApplicationBuilder application) {</br>
        </span><span style="color: #0000ff;">return</span> application.sources(Application.<span style="color: #0000ff;">class</span><span style="color: #000000;">);</br>
    }</br></br>
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> main(String[] args) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception {</br>
        SpringApplication.run(Application.</span><span style="color: #0000ff;">class</span><span style="color: #000000;">, args);</br>
    }</br></br>
    

    }

    复制代码

    最后写一下控制层:

    复制代码
    package sample.jsp;

    import java.util.Date;

    import java.util.Map;

    import org.springframework.beans.factory.annotation.Value;

    import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.GetMapping;

    import org.springframework.web.bind.annotation.RequestMapping;

    @Controller

    public class WelcomeController {

    @Value(</span>"${application.message}"<span style="color: #000000;">)</br>
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String message;</br></br>
    
    
    @GetMapping(</span>"/welcome"<span style="color: #000000;">)</br>
    </span><span style="color: #0000ff;">public</span> String welcome(Map&lt;String, Object&gt;<span style="color: #000000;"> model) {</br>
        model.put(</span>"time", <span style="color: #0000ff;">new</span><span style="color: #000000;"> Date().getTime());</br>
        model.put(</span>"message", <span style="color: #0000ff;">this</span><span style="color: #000000;">.message);</br>
        </span><span style="color: #0000ff;">return</span> "welcome"<span style="color: #000000;">;</br>
    }</br></br>
    
    @GetMapping(</span>"/test"<span style="color: #000000;">)</br>
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String test(){</br>
        </span><span style="color: #0000ff;">return</span> "test"<span style="color: #000000;">;</br>
    }</br></br>
    

    }

    复制代码

    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>
    <groupId>spring-boot-sample-web-jsp</groupId>
    <artifactId>ming</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Spring Boot Web JSP Sample</name>
    <description>Spring Boot Web JSP Sample</description>
    <url>http://projects.spring.io/spring-boot/</url>
    <organization>
    <name>Pivotal Software, Inc.</name>
    <url>http://www.spring.io</url>
    </organization>
    <properties>
    <main.basedir>${basedir}/../..</main.basedir>
    <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
    <springboot.version>1.5.4.RELEASE</springboot.version>
    </properties>
    <dependencies>
    <!-- Compile -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>${springboot.version}</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>
    <!-- Provided -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <version>${springboot.version}</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <version>8.5.6</version>
    <scope>provided</scope>
    </dependency>
    <!-- Test -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>${springboot.version}</version>
    <scope>test</scope>
    </dependency>
    </dependencies>
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
    <useSystemClassLoader>false</useSystemClassLoader>
    </configuration>
    </plugin>
    </plugins>
    </build>
    </project>
    复制代码

    例子下载:http://pan.baidu.com/s/1pKNDJD5

  • 相关阅读:
    如何解决tensorflow报:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
    windows10下如何进行源码编译安装tensorflow
    windows10如何安装cpu版本tensorflow
    git:RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Reques t Entity Too Larg
    ubuntu 18.04 64bit如何安装GPU版本tensorflow
    pip安装tensorflow-gpu好慢怎么办
    linux下split切割的文件如何合并
    Java使用FileOutputStream写入文件
    springmvc文件上传 参数为MultipartFile 转换为File
    Java中FileOutputStream流的write方法
  • 原文地址:https://www.cnblogs.com/jpfss/p/9101062.html
Copyright © 2020-2023  润新知