springboot返回jsp頁面

1、
隨着Spring 4新版本的發佈,Spring Boot這個新的子項目得到了廣泛的關注,因爲不管是Spring 4官方發佈的新聞稿還是針對首席架構師Adrian Colyer的專訪,都對這個子項目所帶來的生產率提升讚譽有加。

Spring Boot充分利用了JavaConfig的配置模式以及“約定優於配置”的理念,能夠極大的簡化基於Spring MVC的Web應用和REST服務開發。

Spring 4倡導微服務的架構,針對這一理念,近來在微博上也有一些有價值的討論,如這裏和這裏。微服務架構倡導將功能拆分到離散的服務中,獨立地進行部署,Spring Boot能夠很方便地將應用打包成獨立可運行的JAR包,因此在開發模式上很契合這一理念。目前,Spring Boot依然是0.5.0的里程碑版本,因此相關的文檔尚不完善,本文將會以一個簡單的樣例來介紹基於這個項目的開發過程。
2、
spring boot 基於spring MVC的基礎上進行了改進, 將@Controller 與@ResponseBody 進行了合併成一個新的註解 @RestController。

當用戶請求時,需要有視圖渲染的,與請求數據的請求分別使用@Controller與@RestController 。
3、
本文章重點介紹如何應用springboot簡單的配置返回一個jsp頁面
4、
首先新建一個maven項目(war包和jar包都行,簡易實用war包,jar測試沒問題,但是打包之後會出現找不到頁面的問題。),gradle也可以,根據自己的喜好。本人使用的是maven3.9 。
(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.qbd</groupId>
    <artifactId>springbootmybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>springboot-mybatis</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.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>
        <!-- Compile -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- Provided -->
        <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>
        <!-- Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <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>

(2)在src/main/resources下新建一個application.properties內容如下

#返回的前綴   目錄對應src/main/webapp下
spring.mvc.view.prefix: /pages/
#返回的後綴
spring.mvc.view.suffix: .jsp

(3)編寫啓動類

package com.qbd;

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
//返回jsp頁面必須繼承SpringBootServletInitializer類重寫裏面的方法
public class SpringbootMybatisApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringbootMybatisApplication.class);
    }
}

(3)編寫controller

package com.qbd;


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


//這個註解不能使用RestController,不然會返回模板類型的頁面
@Controller
public class UserController {
    @RequestMapping("/index")
    public String index() {

        return "index";
    }
}

(4)在src/main/webapp/pages下新建一個index.jsp頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello
</body>
</html>

(5)然後運行啓動類 訪問http://localhost:8080/index結果如下
這裏寫圖片描述
5
根據以上配置即可,如果還有不明白可以參考官方的例子地址爲
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章