使用Spring Tool Suite4创建Spring Boot Web项目

准备工作:

 (1)安装并配置好Spring Tool Suite

创建步骤:

(1)File->New->Spring Starter Project,输入项目名

                         

(2)勾选如下两项,点击Finish等待创建成功。

                         

(3)其中1为启动项目的文件,直接以app方式Run起来即可;2为html/css/js等静态资源的存储目录;3为App的配置文件。

                                              

(4)新建包com.example.web.controller,然后在包中创建文件TestController.java,内容为:

package com.example.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//这里很重要
@Controller
public class TestController {

	// 配置请求地址
	@RequestMapping(value = "/test")
	@ResponseBody
	public String test() {
		System.out.println("hello world");
		// 返回字符串ok给前端
		return "ok";
	}
}

(5)配置启动类,将

@SpringBootApplication

更改为:

@SpringBootApplication(scanBasePackages = "com.example.web.controller")

(6)在src/main/resources/static目录下创建index.html文件,内容为:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello world!
</body>
</html>

(7)创建src/main/resources/application.yml,内容为:

server:
  servlet:
    context-path: /test
  port: 80
  use-forward-headers: true
  tomcat:
    remote-ip-header: X-Real-IP
    protocol-header: X-Forwarded-Proto

其中80为访问端口,/test为访问基础路径

(8)配置src/main/resources/application.properties使其支持访问html文件

# 定位页面的目录到static/下
 spring.mvc.view.prefix=/
 spring.mvc.view.suffix=.html

(9)Run As Java Application或Spring Boot App直接运行启动文件:SpringbootDemo1Application.java  打印出下列日志则说明启动成功


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-08-02 15:47:30.442  INFO 788 --- [  restartedMain] c.e.demo.SpringbootDemo1Application      : Starting SpringbootDemo1Application on slave1 with PID 788 (D:\Eclipse-Spring\project\springboot-demo-1\target\classes started by Administrator in D:\Eclipse-Spring\project\springboot-demo-1)
2019-08-02 15:47:30.450  INFO 788 --- [  restartedMain] c.e.demo.SpringbootDemo1Application      : No active profile set, falling back to default profiles: default
2019-08-02 15:47:30.644  INFO 788 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-08-02 15:47:30.644  INFO 788 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-08-02 15:47:33.403  INFO 788 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 80 (http)
2019-08-02 15:47:33.489  INFO 788 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-08-02 15:47:33.489  INFO 788 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-08-02 15:47:33.771  INFO 788 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/test]   : Initializing Spring embedded WebApplicationContext
2019-08-02 15:47:33.771  INFO 788 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3127 ms
2019-08-02 15:47:34.390  INFO 788 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-02 15:47:34.689  INFO 788 --- [  restartedMain] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2019-08-02 15:47:34.784  INFO 788 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-08-02 15:47:34.891  INFO 788 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 80 (http) with context path '/test'
2019-08-02 15:47:34.895  INFO 788 --- [  restartedMain] c.e.demo.SpringbootDemo1Application      : Started SpringbootDemo1Application in 5.28 seconds (JVM running for 6.453)

(10)浏览器访问地址:http://localhost/test/index.html

访问http://localhost/test/test

 

控制台打印出hello world

 

 

最后说一下出现404错误的原因:

极有可能是这三个文件没有配置好,

SpringbootDemo1Application.java文件中的

@SpringBootApplication

一定要配置成:

@SpringBootApplication(scanBasePackages = "com.example.web.controller")

其它两个文件要配置成上面的内容,大概意思就是这样。

用过Python和Java的Web框架,最后发现Spring Boot创建项目的配置的确比Python的简单,并且Spring Boot还可以不依靠Tomcat容器就启动Web项目,感觉好神奇的样子。

  • 12
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 1. 打开Spring Tool Suite 4,点击File -> New -> Spring Starter Project。 2. 在弹出的窗口中,选择Web作为项目类型,并填写项目的基本信息,如项目名称、包名等。 3. 点击Next,选择需要添加的依赖,如Spring WebSpring Boot DevTools等。 4. 点击Finish,等待项目创建完成。 5. 在项目中添加需要的Java类、HTML、CSS、JavaScript等文件,编写代码。 6. 运行项目,可以通过浏览器访问项目的URL地址,查看效果。 ### 回答2: Spring Tool Suite 4(STS)是一个基于Eclipse的集成开发环境(IDE),用于开发和部署基于Spring框架的Web应用程序。 要使用STS创建Web应用程序,首先需要确保已经安装了Java JDK和Eclipse IDE。然后,按照以下步骤进行操作: 1. 下载并安装最新版本的Spring Tool Suite 4。 2. 启动STS,并选择一个工作区目录进行存储项目文件。 3. 在STS的欢迎页面上选择“Create a Spring Starter Project”以创建一个新的Spring项目。 4. 在“New Spring Starter Project”对话框中,您可以设置项目的基本信息,例如项目名称、类型和包名。选择"Web"作为项目类型,并选择您喜欢的Spring Boot版本。 5. 单击“Next”按钮后,您可以选择要在项目中包含的依赖项。根据您的项目需求,选择适当的依赖项,例如Spring Boot WebSpring Data JPA等。 6. 在“Next”按钮的右下角,可以选择项目的名称和位置。确保选择一个合适的位置,然后单击“Finish”按钮完成项目创建过程。 7. 一旦项目创建完成,您将在STS的项目导航器中看到项目的文件结构。 8. 在项目的src/main/java目录下创建一个包,并在该包中创建一个Java类作为您的控制器类。控制器类用于处理Web请求和响应。 9. 创建相应的HTML、CSS和JavaScript文件作为您的Web界面。 10. 使用Spring框架的注解或XML配置文件来配置您的控制器类和其他组件。 11. 运行应用程序,可以选择在浏览器中查看您的Web应用程序。要运行应用程序,右键单击项目,选择“Run As”>“Spring Boot App”。 12. 在浏览器中输入http://localhost:8080(假设默认端口为8080),您将看到您的Web应用程序的界面。 以上是使用Spring Tool Suite 4创建Web应用程序的基本步骤。通过使用STS和Spring框架的强大功能,您可以轻松地构建和部署功能丰富的Web应用程序。 ### 回答3: Spring Tool Suite 4 (STS4) 是一个基于 Eclipse 的集成开发环境(IDE),专门用于开发和管理基于 Spring 框架的应用程序。在 STS4 中创建一个基于 Web 的项目非常简单。 首先,打开 STS4 并选择 File -> New -> Other,然后在弹出的对话框中搜索 "Spring Starter Project" 并确认。接下来,输入项目名称和项目位置,并选择好适合你的 Spring Boot 版本。然后,点击 "Next" 继续。 在下一个界面中,你可以选择要包含在项目中的相关技术和依赖,例如 Spring MVC、Spring Security等。选择你需要的选项并点击 "Next"。 接下来,你可以定义项目的一些基本设置,如 Group 和 Artifact 名称、包名称等。在这一步中,你还可以选择一个特定的 Web 服务器,如 Tomcat 或 Jetty。完成设置后,点击 "Finish"。 STS4 将为你创建一个基于 Maven 的项目结构,其中包含了一些默认的配置文件和依赖项。在项目的 src 目录下,你将看到一个名为 "main" 的文件夹,其中包含了源代码和资源文件。在其中创建一个名为 "webapp" 的文件夹,并在其中添加你的 HTML、CSS、JS 等前端资源文件。 接下来,你可以在 src/main/java 目录下创建你的 Java 类,并在其中编写你的业务逻辑和控制器。你还可以在 src/main/resources 目录下添加属性文件、配置文件等。 最后,按下 Ctrl + F11 或点击工具栏上的绿色运行按钮,你的 Web 项目将在内置的 Web 服务器上启动,并可以在浏览器中访问。 总结来说,使用 STS4 创建一个基于 Web 的项目非常简单。只需几步即可创建项目,并通过添加所需的依赖项和配置文件来定制你的项目。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值