Spring Boot 響應jsp

林堯彬發表於2020-04-04

新增依賴

<?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>wonder</groupId>
    <artifactId>skyRainbow</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </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-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <!--jsp 的依賴-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

建立啟動檔案

package lf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // SpingBoot 相關注解,等於@Configuration、@EnableAutoConfiguration、@ComponentScan三個註解一起的作用
public class SkyRainbowApplication {

    public static void main(String[] args) {
        /**
         * Spring boot 程式入口
         */
        SpringApplication.run(SkyRainbowApplication.class,args);
    }
}

建立相關Java檔案

package lf.controller;

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 java.util.Date;

@Controller
@RequestMapping("/lf/page")
public class PageController {

    /**
     * 進入公司主頁
     */
    @RequestMapping(value = "/company",method = RequestMethod.GET)
    public String gotoCompanyPage(Model model){
        model.addAttribute("time",new Date());
        return "main";
    }


}

新增字首和字尾(新增src/main/resources/application.properties內容如下)

# 頁面預設字首目錄
spring.mvc.view.prefix=/WEB-INF/jsp/
# 響應頁面預設字尾
spring.mvc.view.suffix=.jsp

 

新增jsp檔案

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>主頁</title>
</head>
<body>

    <h1>歡迎登陸甘雨路主頁</h1>
    <div>登陸時間:${time}</div>


</body>
</html>

注意:如果工具是idea,jsp路徑如下(/src/main/resources/META-INF/resources/WEB-INF/jsp)

如果是eclipse、myEclipse工具,放在在 src/main 下面建立 webapp/WEB-INF/jsp 目錄用來存放我們的jsp頁面。 

開啟啟動檔案,並執行

 

 在瀏覽器上輸入http://localhost:8080/lf/page/company 即可

 

轉載於:https://www.cnblogs.com/lantu1989/p/8502766.html

相關文章