1. 程式人生 > >springboot2.0之jsp整合

springboot2.0之jsp整合

    這篇部落格是學習springboot整合各種三方框架後基本的CRUD操作。

  1,整合jsp

         注意springboot預設是不支援jsp的,所以我們利用springboot整合jsp時,一定要建立成一個war型別專案,同時要引入外部tomcat。

        如專案結構:

       

      pom.xml

      

<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.sb</groupId>
  <artifactId>springboot2.0-jsp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <dependencies>
        <!-- SpringBoot 對lombok 支援 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- SpringBoot web 核心元件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <!-- SpringBoot 外部tomcat支援 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <!-- springboot-log4j -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>
        <!-- springboot-aop 技術 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

    </dependencies>
</project>

 User實體類:

 

package com.springboot.pojo;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * user實體類
 * @author Administrator
 *
 */
//該註解表示為當前類生產getter setter方法 是lombok的方法
@Data
//該註解表示列印日誌資訊
@Slf4j
public class User {

    private Long id;
    private String name;
    private
Integer age; public User() { // TODO Auto-generated constructor stub } public User(Long id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } }

控制層:

package com.springboot.controller;

import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.springboot.pojo.User; /** * User物件控制層 * @author Administrator * */ @Controller public class UserController { @RequestMapping("/getList") public String getList(Model model){ List<User> list = new ArrayList<User>(); list.add(new User(1L, "東東", 15)); list.add(new User(2L, "南南", 11)); list.add(new User(3L, "西西", 16)); list.add(new User(4L, "貝貝", 12)); model.addAttribute("list", list); return "index"; } @RequestMapping("/getUser") @ResponseBody public String getUser(Long id){ String message = null; if(1==id){ message = "查詢成功"; }else{ message = "找不到指定物件"; } return message; } }

App.java

package com.springboot;

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

@SpringBootApplication
public class App {

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

}

  在resources目錄下建立屬性檔案application.peroperties(這是springboot預設的配置檔案最好不要改名字)

###配置前後綴關係
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.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>
  ${list}
</body>
</html>

通過App.java中的main函式啟動程式

瀏覽器鍵入http://localhost:8080/getList

至此我們的springboot整合jsp就以及完成了,在接下來我會在此專案的基礎上整合mybatis等一些其他常用框架。