spring boot 开发--第二篇加入对jsp的支持

1、构建项目

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>com.vesus</groupId>
    <artifactId>springboot-helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>springboot-helloworld</name>
    <description>Demo project for Spring Boot</description>
    
    <parent>
        <groupId>com.vesus</groupId>
        <artifactId>spring-boot-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3、创建控制器HelloController

package com.vesus.springbootjsp.controller;

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

@Controller
public class HelloWorldController {

    @RequestMapping(value = "/hello")
    public String hello(){

        return "hello";
    }
}

4、配置application.properties

spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

5、在 src/main 下面创建 webapp/WEB-INF/文件夹,并添加hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>My first Spring boot web demo</title>
</head>
<body>
<h2>Hello world</h2>
</body>
</html>

6、多模块下访问jsp汇报404,需要设置一下工作目录,设置为模块的目录

7、访问localhost:8080/hello,显示

hello world

源码 :https://gitee.com/vesus198/springboot-demo/tree/master/springboot-jsp

猜你喜欢

转载自blog.csdn.net/liuweilong07/article/details/80216461