美文网首页
Spring Boot/Spring MVC

Spring Boot/Spring MVC

作者: 米刀灵 | 来源:发表于2016-09-27 18:04 被阅读1476次

先创建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>org.springframework</groupId>
    <artifactId>gs-serving-web-content</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

创建web controller:
src/main/java/hello/GreetingController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class GreetingController {

    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        return "greeting";
    }

}

创建application.properties:
src/main/resources/application.properties

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
#如果按照一般web工程将页面放在src/main/webapp/WEB-INF/jsp/,则配置前缀
#spring.mvc.view.prefix=/WEB-INF/jsp/
#spring.mvc.view.suffix=.jsp

完整的application.properties 示例

创建thymeleaf页面:
spring boot不推荐使用jsp,因为在tomcat/jetty上jsp不能在嵌套的tomcat/jetty容器中解析,即不能在打包成可执行的jar的情况下解析 (直接run main函数或者打成可执行的jar包),只能是打成war包在非嵌套的tomcat容器才能看到效果。Spring Boot提供了默认配置的模板引擎主要有以下几种:

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

src/main/resources/templates/greeting.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

执行application:

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

@SpringBootApplication
public class Application {

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

因为使用了spring boot,到现在我们都没有配置过任何.xml文件,包括web.xml。接下来我们可以打包,执行。

java -jar target/gs-serving-web-content-0.1.0.jar

打开浏览器 8080/greeting , 可以看到:
"Hello, World!"
8080/greeting?name=User 可以看到:
"Hello, User!"

配置默认页面和静态资源:
Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:

  • /static
  • /public
  • /resources
  • /META-INF/resources

例如:/static/js,/static/css。index.html 一般作为默认页面一般也配置在静态资源路径下,打开 http://localhost:8080/ 即展示:
src/main/resources/static/index.html

<!DOCTYPE HTML>
<html>
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p>Get your greeting <a href="/greeting">here</a></p>
</body>
</html>

在四个目录下同名的html页面优先级先后为:/META-INF/resources > /resources > /static > /public。

一个完整的项目结构:

  • root package结构:com.example.myproject
  • 应用主类Application.java置于root package下,我们放在root package下可以帮助程序减少手工配置来加载到我们希望被Spring加载的内容。
  • 实体(Entity)与数据访问层(Repository)置于com.example.myproject.domain包下。
  • 逻辑层(Service)置于com.example.myproject.service包下。
  • Web层(web)置于com.example.myproject.web包下。

相关文章

网友评论

      本文标题:Spring Boot/Spring MVC

      本文链接:https://www.haomeiwen.com/subject/yfthyttx.html