IDEA上面搭建一个SpringBoot的web

网友投稿 217 2023-05-21


IDEA上面搭建一个SpringBoot的web

这几天一直在研究IDEA上面怎么搭建一个web-mvc的SpringBoot项目,看网上的教程一步步的搭建,可是还是出现一堆的问题。

为了让大家以后少走一些弯路,我在这里分享一下我这几天研究的成果,也希望对大家能有所帮助。

这里先介绍一下各种环境的配置信息:idea2016.2.1  jdk1.8.0_31

因为SpringBoot中是内置tomcat的,所以也就不需要额外的tomcat配置了,现在开始讲如何在idea上面搭建SpringBoot web-mvc项目了

步骤一:在IDEA中新建一个常规的maven项目,具体步骤请看看下面的图示:

通过图上面的几个步骤,一个基本的maven项目就搭建完成了,接下来就是开始搭建SpringBoot中各种配置文件信息了。

步骤二:

1.先复制以下代码到pox.xml中去

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

demo

0.0.1-SNAPSHOT

jar

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.4.0.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.example

demo

0.0.1-SNAPSHOT

jar

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.4.0.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-maven-plugin

2.点击maven中jar包依赖更新按钮,具体操作看下面图示:

3.配置resources下面的Web资源文件,这里我就配置两个文件,一个是用来存放静态文件夹的static文件,还有一个就是用来存放HTML的资源文件夹templates。

这里需要特别主要的是:static文件中一般存放css,js,image等静态资源文件,而templates文件中一般存放各种HTML文件。而且这两个文件都是默认存在的,路径不需要特别的配置就可以直接引用了。

application.properties是个配置文件,这里面可以配置SpringBoot的相关信息。大家需要注意的是这个文件名千万不要写错,也不要放错位置,不然都不会生效的。

下面看图示案例和代码案例:

csstest.css的代码信息:

body {

padding: 0px;

margin: auto;

font-family: "黑体", "仿宋", Arial, "Arial Unicode MS", System;

background-color: #00F;

font-size: 20px;

text-align: left;

}

welcome.html的代码信息:

welcome page is login.........

application.properties配置文件的代码信息:

#修改tomcat的默认的端口号,将8080改为8888

server.port=8888

4.编写SpringBoot中Web-Mvc的控制器和项目启动入口:

DemoApplication.Java具体代码:

package example;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class DemoApplication {

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

}

HelloController.java的具体代码:

package example;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

import java.util.Map;

@Controller

public class HelloController {

@RequestMapping("/index")

public String index(){

return "welcome";

}

}

这样SpringBoot的Web-mvc项目就已经搭建成功了,具体步骤就是这样的。

还有一点需要主要的是:因为我已经把端口号给修改了,所以访问的时候地址就要写成 127.0.0.1:8888/index 。

以上所述是给大家介绍的IDEA上面搭建一个SpringBoot的web-mvc项目遇到的问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:webpack配置的最佳实践分享
下一篇:java异常处理详细介绍及实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~