Spring启动Web应用示例

时间:2020-02-23 14:35:50  来源:igfitidea点击:

在本教程中,我们将看到如何使用JSP创建Spring Boot Web应用程序示例。

让我们看看Spring Boot如何在寿命中浮现更简单。
我将使用Spring Boot创建Spring MVC Hello World示例。

Spring Boot Web应用程序示例:

以下是使用JSP创建Spring引导Web应用示例的步骤。

用于创建以下项目的工具:

  • Spring Boot 1.5.3.RELEASE
  • Spring 4.3.8.RELEASE
  • Tomcat Embed 8
  • Maven 3
  • Java 8
  • Eclipse

步骤1:使用名为"springboothelly申请"的Eclipse中的Maven创建动态Web项目。

maven依赖项:

第2步:更改POM.xml如下所示:

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.igi.theitroad</groupId>
	<artifactId>SpringBootHelloWorldExample</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBootHelloWorldExample Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- JSTL for JSP -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
 
		<!-- For JSP compilation -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>SpringBootHelloWorldExample</finalName>
	</build>
</project>

Spring-Boot-Starter-Parent为我们提供任何Spring项目所需的所有Maven默认值。
由于我们正在开发Web应用程序,我们还需要添加Spring-Boot-Starter-Web依赖项。
这将包括其他依赖性此类应用程序所需的Spring Boot,Tomcat等。
如果我们注意到,我们没有为特定组件提供任何版本。
我们只需要提供版本号。
(1.5.3.Release)用于春令生。
第3步:创建名为"org.igi.theitroad.springboot"的包创建一个名为"helloworldcontroller.java"的控制器类

package org.igi.theitroad.springmvc.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
public class HelloWorldController {
 
 @RequestMapping("/helloworld")
 public ModelAndView hello() {
 
  String helloWorldMessage = "Hello world from theitroad!";
  return new ModelAndView("hello", "message", helloWorldMessage);
 }
}

由于请求首先转到DispatcherServlet并将其重定向到控制器类。
这里@controller描绘了这是我们的控制器类。
@RequestMapper用于将传入的HTTP请求映射到Handler方法(Hello()中的控制器中的Hello()).so hellorldcontroller.java的方法将处理来自Dispatcher.so的Get请求。
当我们有URL时

http://localhost:8080/helloworld or
http://localhost:8080/helloworld.html

上面的方法将被称为。

第4步:创建名为"org.igi.theitroad"的包创建一个名为"springboothellyorldapplication.java"的类。

package org.igi.theitroad;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class SpringBootHelloWorldApplication {
 
	public static void main(String[] args) 
	{
		SpringApplication.run(SpringBootHelloWorldApplication.class, args);   
	}
}

我们刚刚添加了@springbootapplication,它做了所有的工作。
让我们了解更多关于这个注释的信息。
@springBootApplication是添加以下所有内容的注释:

@configuration使类作为应用程序上下文的Bean定义的源。
@EnableAutoConfiguration允许Spring Boot在ClassPath设置和各种属性设置中添加Bean礼物。
通常,我们将为Spring MVC应用程序添加@bableWebMVC,但Spring Boot会在类路径上看到Spring-WebMVC时自动添加它。
将应用程序标记为Web应用程序,并激活诸如设置DispatcherServlet之类的关键行为。
@ComponentsCan告诉Spring在默认包中查找其他组件,配置和服务,允许它找到控制器。
如果未定义特定包,则会从声明此注释的类的包中扫描。

步骤5:创建名为Application.properties的属性文件,如下所示,并将其放在SRC/Main/Resours中。

spring.mvc.view.prefix:/web-inf/spring.mvc.view.suffix:.jsp

在Spring MVC示例中,在SpringMVC-Dispatcher-servlet.xml中使用的两个属性非常相似。

第6步:修改index.jsp如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HelloWorld</title>
</head>
<body>
<a href="helloworld.html">Click here to read hello message </a>
</body>
</html>

第7步:在/web-inf /文件夹中创建hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
${message}
</body>
</html>

这完全是关于Spring的启动你好世界例。
第8步:这是时候做Maven Build。

右键单击项目 - >运行AS - > Maven Build

步骤9:提供目标作为清洁安装Spring 引导:运行(如下所示)并单击"运行"

我们将在控制台中看到以下输出:

[INFO] Scanning for projects...
[INFO]
[INFO] ————————————————————————
[INFO] Building SpringBootHelloWorldExample Maven Webapp 0.0.1-SNAPSHOT
[INFO] ————————————————————————
[INFO]
[INFO] — maven-clean-plugin:2.6.1:clean (default-clean) @ SpringBootHelloWorldExample —
[INFO] Deleting /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ SpringBootHelloWorldExample —
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ SpringBootHelloWorldExample —
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/classes
[INFO]
[INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ SpringBootHelloWorldExample —
[INFO] Not copying test resources
[INFO]
[INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SpringBootHelloWorldExample —
[INFO] Not compiling test sources
[INFO]
[INFO] — maven-surefire-plugin:2.18.1:test (default-test) @ SpringBootHelloWorldExample —
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.18.1/maven-surefire-common-2.18.1.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.18.1/maven-surefire-common-2.18.1.pom (7 KB at 2.0 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.3/maven-plugin-annotations-3.3.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.3/maven-plugin-annotations-3.3.pom (2 KB at 2.6 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-tools/3.3/maven-plugin-tools-3.3.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-tools/3.3/maven-plugin-tools-3.3.pom (13 KB at 16.1 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.18.1/surefire-api-2.18.1.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.18.1/surefire-api-2.18.1.pom (3 KB at 2.8 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.18.1/surefire-booter-2.18.1.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.18.1/surefire-booter-2.18.1.pom (3 KB at 3.5 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.pom (3 KB at 3.7 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/15/maven-shared-components-15.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/15/maven-shared-components-15.pom (10 KB at 7.5 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/16/maven-parent-16.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/16/maven-parent-16.pom (23 KB at 20.3 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/apache/7/apache-7.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/apache/7/apache-7.pom (15 KB at 17.2 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.18.1/maven-surefire-common-2.18.1.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.18.1/maven-surefire-common-2.18.1.jar (269 KB at 72.2 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.18.1/surefire-booter-2.18.1.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.18.1/surefire-booter-2.18.1.jar (39 KB at 34.6 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/3.0/maven-reporting-api-3.0.jar (11 KB at 12.8 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.18.1/surefire-api-2.18.1.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.18.1/surefire-api-2.18.1.jar (145 KB at 31.4 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.3/maven-plugin-annotations-3.3.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.3/maven-plugin-annotations-3.3.jar (14 KB at 12.2 KB/sec)
[INFO] Tests are skipped.
[INFO]
[INFO] — maven-war-plugin:2.6:war (default-war) @ SpringBootHelloWorldExample —
[INFO] Packaging webapp
[INFO] Assembling webapp [SpringBootHelloWorldExample] in [/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/SpringBootHelloWorldExample]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/src/main/webapp]
[INFO] Webapp assembled in [375 msecs]
[INFO] Building war: /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/SpringBootHelloWorldExample.war
[INFO]
[INFO] — maven-install-plugin:2.5.2:install (default-install) @ SpringBootHelloWorldExample —
[INFO] Downloading: https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.pom (11 KB at 13.2 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.pom (4 KB at 4.8 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.pom (4 KB at 4.4 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/commons-codec/commons-codec/1.6/commons-codec-1.6.jar (228 KB at 51.9 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-utils/0.4/maven-shared-utils-0.4.jar (152 KB at 78.3 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.jar (234 KB at 65.0 KB/sec)
[INFO] Installing /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/SpringBootHelloWorldExample.war to /Users/apple/.m2/repository/org/igi/theitroad/SpringBootHelloWorldExample/0.0.1-SNAPSHOT/SpringBootHelloWorldExample-0.0.1-SNAPSHOT.war
[INFO] Installing /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/pom.xml to /Users/apple/.m2/repository/org/igi/theitroad/SpringBootHelloWorldExample/0.0.1-SNAPSHOT/SpringBootHelloWorldExample-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] >>> spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) > test-compile @ SpringBootHelloWorldExample >>>
[INFO]
[INFO] — maven-resources-plugin:2.6:resources (default-resources) @ SpringBootHelloWorldExample —
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] — maven-compiler-plugin:3.1:compile (default-compile) @ SpringBootHelloWorldExample —
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] — maven-resources-plugin:2.6:testResources (default-testResources) @ SpringBootHelloWorldExample —
[INFO] Not copying test resources
[INFO]
[INFO] — maven-compiler-plugin:3.1:testCompile (default-testCompile) @ SpringBootHelloWorldExample —
[INFO] Not compiling test sources
[INFO]
[INFO] <<< spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) < test-compile @ SpringBootHelloWorldExample <<<
[INFO]
[INFO] — spring-boot-maven-plugin:1.5.3.RELEASE:run (default-cli) @ SpringBootHelloWorldExample —. ____ _ __ _ _
/\/___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_/_` |
\/___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, |////
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)2016-04-28 14:20:10.519 INFO 1051 — [ main] o.a.j.SpringBootHelloWorldApplication : Starting SpringBootHelloWorldApplication on apples-MacBook-Air.local with PID 1051 (/Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample/target/classes started by apple in /Users/apple/Documents/WorkspaceAlgo/SpringBootHelloWorldExample)
2016-04-28 14:20:10.522 INFO 1051 — [ main] o.a.j.SpringBootHelloWorldApplication : No active profile set, falling back to default profiles: default
2016-04-28 14:20:10.611 INFO 1051 — [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4c624d60: startup date [Fri Apr 28 14:20:10 IST 2016]; root of context hierarchy
2016-04-28 14:20:12.697 INFO 1051 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-04-28 14:20:12.720 INFO 1051 — [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-04-28 14:20:12.725 INFO 1051 — [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14
2016-04-28 14:20:13.227 INFO 1051 — [ost-startStop-1] org.apache.jasper.servlet.comScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2016-04-28 14:20:13.240 INFO 1051 — [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-04-28 14:20:13.240 INFO 1051 — [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2629 ms
2016-04-28 14:20:13.436 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-04-28 14:20:13.441 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-04-28 14:20:13.441 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-04-28 14:20:13.441 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-04-28 14:20:13.441 INFO 1051 — [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2016-04-28 14:20:13.807 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4c624d60: startup date [Fri Apr 28 14:20:10 IST 2016]; root of context hierarchy
2016-04-28 14:20:13.921 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/helloworld]}" onto public org.springframework.web.servlet.ModelAndView org.igi.theitroad.springboot.HelloWorldController.hello()
2016-04-28 14:20:13.927 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-04-28 14:20:13.928 INFO 1051 — [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse)
2016-04-28 14:20:13.966 INFO 1051 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-04-28 14:20:13.966 INFO 1051 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-04-28 14:20:14.033 INFO 1051 — [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-04-28 14:20:14.254 INFO 1051 — [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-04-28 14:20:14.372 INFO 1051 — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-04-28 14:20:14.390 INFO 1051 — [ main] o.a.j.SpringBootHelloWorldApplication : Started SpringBootHelloWorldApplication in 4.582 seconds (JVM running for 47.545)

步骤10:让我们现在测试应用程序。
随着Spring使用嵌入式Tomcat,我们可以使用http://localhost:8080/index.jsp访问项目。