Spring Boot入门篇一: Jsp项目搭建

1. 写在前面

  • 写代码不要着急,注意配置文件或目录名不要存在空格

2. 开发环境/ 工具准备

  • JDK1.8.0_25
C:\Users\Administrator>java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b18)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
  • Maven3.3.3
C:\Users\Administrator>mvn -v
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-
Maven home: D:\appData\maven\apache-maven-3.3.3\bin\..
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_25\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"
  • eclipse

3. 新建 Maven 项目

点击Next
点击Next
点击Finish

由于我这里已经建过一个 spring-boot-example 了,所以就直接跳过了。

4. 配置 pom.xml 依赖

以下是我的 pom.xml 配置:


    4.0.0
    com.example.springboot
    spring-boot-example
    0.0.1-SNAPSHOT
    jar

    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
    

    
        1.8
    

    
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
        
            javax.servlet
            jstl
        
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            provided
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    false
                
            
        
    


5. 建立项目目录结构

以下是我的项目目录截图:


项目目录结构

这里需要注意了, Application.javaSpring Boot 的应用程序配置文件,是整个项目的启动文件,必须放在 com.example 中, 即所有模块(dao、entity、service、web)的父包中。

6. 配置 application.properties

在你的 src/main/resources 目录下配置 application.properties 文件:

spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
application.message: Hello Phil

webapp 目录结构:

webapp目录结构

其中,index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>




spring-boot example


  

index.jsp

welcome.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>




spring-boot example


  

welcome, spring-boot!

7. 编写程序启动入口类 Application.java

package com.example;

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 {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

8. 编写 HelloController.java

package com.example.web;

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.RequestMapping;

@Controller
public class HelloController {
    
    @Value("${application.message: Hello World}")
    private String message = "Hello World";
    
    @RequestMapping("/")
    public String main () {
        return "index";
    }
    
    @RequestMapping("/welcome")
    public String welcome(Map model){
        model.put("time", new Date());
        model.put("message", this.message);
        return "welcome";
    }
    
}

9. 启动项目

  • 打开终端,输入下面命令行:
mvn spring-boot:run
终端启动项目

访问: localhost:8080 显示 :

index.jsp

访问 localhost:8080/welcome 显示:

welcome.jsp

Enjoy it~

你可能感兴趣的:(Spring Boot入门篇一: Jsp项目搭建)