Spring-boot 프레임 초보 구축
SpringMVC 는 매우 위대 한 틀 로 개원 하고 신속하게 발전 한다.우수한 디자인 은 반드시 구분 되 고 결합 을 풀 것 이다.그래서 spring 에는 core,context,bean,mvc 등 하위 항목 이 많 습 니 다.이것 은 근본 을 아 는 사람 에 게 는 간단명료 하지만,springmvc 는 바보 같은 조작 을 위해 발명 한 것 이다.springmvc 를 처음 배 운 사람 에 게 는 구 하려 면 일련의 dependency 를 복사 해 야 하 는데 이것 이 무엇 인지 의존 이 적은 것 은 아 닌 지 모르겠다.제 가 springmvc 를 처음 접 했 을 때 바 이 두 튜 토리 얼 이 각각 다르다 는 것 을 알 게 되 었 습 니 다.그래서 하나의 코드 를 복 사 했 지만 스스로 설정 할 수 없 었 습 니 다.근본 적 인 원인 은 각 의존 하 는 가방 을 모 르 기 때 문 입 니 다.
Spring-boot 는 복잡 한 코드 설정 을 해결 하기 위해 만들어 진 것 입 니 다.Spring-boot 도 자바-base 를 기반 으로 개발 한 코드 이 며 xml 파일 설정 을 사용 하지 않 고 모든 코드 는 자바 로 이 루어 집 니 다.Groovy 의 동적 언어 실행 에 도 가입 할 수 있 습 니 다.
2.기본 적 인 웹-mvc 프로젝트 구축
2.1 Configure environment
아이디어 에서 new-maven 을 선택 하여 빈 maven 프로젝트 를 만 듭 니 다.예 를 들 어 이름 springboot-test.
2.2.1pom.xml
자바 버 전 설정:
<properties>
    <java.version>1.8</java.version>
</properties> 
<dependencyManagement>
    <dependencies>
      <dependency>
        <!-- Import dependency management from Spring Boot -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.3.5.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
</dependencyManagement> 
<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
  </dependency>
</dependencies> 
<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <fork>true</fork>
        </configuration>
      </plugin>
    </plugins>
 
</build> 
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> 
새 항목 의 이름 이:springboot boot-test 라면 가방 springboot-test/src/main/java/com/test 를 만 듭 니 다.
com
 +- example
   +- myproject
     +- Application.java
     |
     +- domain
     |  +- Customer.java
     |  +- CustomerRepository.java
     |
     +- service
     |  +- CustomerService.java
     |
     +- web
       +- CustomerController.javapackage com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * Created by miaorf on 2016/6/19.
 */
@SpringBootApplication
public class AppConfig {
  public static void main(String[] args) {
    SpringApplication.run(AppConfig.class);
  }
} 
이렇게 배치 가 거의 완성 되 었 습 니 다.다음 개발 제어 층 controller:
com.test.controller.HelloController 를 만 듭 니 다.
package com.test.controller; 
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 java.util.HashMap;
import java.util.Map;
 
/**
 * Created by miaorf on 2016/6/19.
 */
@Controller
public class HelloController {
 
  @RequestMapping("/index")
  public String index(Model model){
 
    model.addAttribute("name","Ryan");
 
    return "index";
  } 
  @RequestMapping("/json")
  @ResponseBody
  public Map<String,Object> json(){
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("name","Ryan");
    map.put("age","18");
    map.put("sex","man");
    return map;
  }
} 
보 기 는 기본적으로 springboot-test\src\main\resources\templates**에 놓 여 있 습 니 다.
그래서 springboot-test\src\main\\resources\templates\\index.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> 
<!DOCTYPE HTML>
<html>
<head>
  <title>Getting Started: Serving Web Content</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
hello, This is static page. not resolve by server, just the html.
</body>
</html> 
시작 방식 이 다양 합 니 다.main 방법 을 시작 할 수도 있 고 명령 행 을 통 해 시작 할 수도 있 습 니 다.
D:\temp\springboot-test>mvn spring-boot:run
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.test:springboot-test:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.springframework.boot:spring-boot-maven-plugin is missing. @ line 49, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springboot-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) > test-compile @ springboot-test >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springboot-test ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springboot-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ springboot-test ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\temp\springboot-test\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ springboot-test ---
[INFO] No sources to compile
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) < test-compile @ springboot-test <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) @ springboot-test ---
[INFO] Attaching agents: []
 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::    (v1.3.5.RELEASE)
 
  
  
 demo 다운로드 경로:springboot_jb51.rar
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.