网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

教你搭建一个Spring-Boot框架

教你搭建一个Spring-Boot框架?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

10年积累的成都网站设计、网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计制作后付款的网站建设流程,更有西青免费网站建设让你可以放心的选择与我们合作。

一、简介

SpringMVC是非常伟大的框架,开源,发展迅速。优秀的设计必然会划分、解耦。所以,spring有很多子项目,比如core、context、bean、mvc等。这对知根底的人来说很简单明了,然而springmvc就是为了傻瓜式的操作而发明的。对于初学springmvc的人来说,想要入手就开发需要拷贝一连串的dependency而不知道这个是干嘛,不知道是不是少了依赖。像我刚接触springmvc的时候到处百度教程而发现各有不同,于是复制了一个又一个代码却不能自己设置,根本原因是不了解各个依赖的包。

Spring-Boot 正是为了解决繁复的代码配置而产生的。Spring-Boot 也是基于java-base 开发的代码,及不用xml文件配置,所有代码都由java来完成。还可以加入Groovy的动态语言执行。

 二、搭建一个基本的web-mvc 项目

2.1 Configure environment

  1. java 1.8+
  2. maven 3.3+
  3. spring-boot 1.3.5
  4. idea 15
  5. Thymeleaf 3

2.2 Start

在idea中,选择new-》maven创建一个空的maven项目,比如名字springboot-test。

2.2.1pom.xml

设定java版本:



    1.8

 

添加依赖版本管理dependencyManagement



    

      

        

        org.springframework.boot

        spring-boot-dependencies

        1.3.5.RELEASE

        pom

        import

      

    

 

添加spring-web项目依赖



    

      org.springframework.boot

      spring-boot-starter-web

    

  

    org.springframework.boot

    spring-boot-devtools

    true

  

 

添加build-plugin



    

      

        org.springframework.boot

        spring-boot-maven-plugin

        

          true

        

      

    

 

 

如此,一个简单的restful的webservice的项目就搭建好了。如果想要支持视图渲染,即jsp、freeMark、velocity等,添加对应的依赖即可。比如,我使用Thymeleaf模板:



      org.springframework.boot

      spring-boot-starter-thymeleaf

 

2.2.2 创建java代码

如果新建项目的名字是:springboot-test. 则创建包springboot-test/src/main/java/com/test.

com
 +- example
   +- myproject
     +- Application.java
     |
     +- domain
     |  +- Customer.java
     |  +- CustomerRepository.java
     |
     +- service
     |  +- CustomerService.java
     |
     +- web
       +- CustomerController.java

com.test是我们的基本包名。下面创建配置类com.test.AppConfig。

package 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);

  }

} 

@SpringBootApplication 标注启动配置入口,可以发现通过一个main方法启动。使用这个注解的类必须放置于最外层包中,因为默认扫描这个类以下的包。否则需要自己配置@ComponentScan。 

这样,配置基本完成了。下面开发控制层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 json(){

    Map map = new HashMap();

    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







  Getting Started: Serving Web Content

  





D:\workspace\springboot\springboot-test\src\main\webapp\hello.html







  Getting Started: Serving Web Content

  





hello, This is static page. not resolve by server, just the html.



 

2.2.3 run

启动方式多种,可以启动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)

浏览器访问:localhost:8080/index

教你搭建一个Spring-Boot框架

教你搭建一个Spring-Boot框架

教你搭建一个Spring-Boot框架

关于教你搭建一个Spring-Boot框架问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


文章标题:教你搭建一个Spring-Boot框架
网页地址:http://bjjierui.cn/article/pisgjh.html

其他资讯