spring boot:创建一个简单的web(maven web project)

Posted 穆晟铭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot:创建一个简单的web(maven web project)相关的知识,希望对你有一定的参考价值。


1、新建一个maven web project;

2、在pom.xml文件中添加相应的依赖包;

3、新建一个HelloController请求控制类;

4、编写index.jsp页面;

5、编写启动类App.java

6、启动应用程序进行代码测试

 

pom.xml配置

 
 <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <!--<version>2.0.1.RELEASE</version>-->
    </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>
  
  
  <dependencies>
  
  <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
  <dependency>
  	<groupId>org.springframework.boot</groupId>
  	<artifactId>spring-boot-starter-web</artifactId>
  </dependency> 
 
  
 	<!-- servlet 依赖. -->
	<!-- dependency>
		<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
		<scope>provided</scope>
	</dependency-->       
    
	<!-- 
	    	JSTL(JSP Standard Tag Library,JSP标准标签库)是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的。
	 -->
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>jstl</artifactId>
	</dependency>
		
    
    <!-- tomcat 的支持.-->
    <!-- Spring Boot应用启动器还有一些替换技术的启动器,具体如下: -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
			
		
		
		
     <!-- FastJson -->
    <dependency>
    	<groupId>com.alibaba</groupId>
    	<artifactId>fastjson</artifactId>
    	<version>1.2.15</version>
    </dependency>		
    
    
   <!-- spring boot devtools 热部署 -->
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-devtools</artifactId>
    	<optional>true</optional>
    	<scope>true</scope>
    </dependency>
    
  </dependencies>
  
  
  <build>
    <finalName>boot2</finalName>
    
    <plugins>
  	
  	<!-- spring boot devtools的plugin -->
  	<plugin>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-maven-plugin</artifactId>
  		<configuration>
  			<!--fork :  如果没有该项配置,呢个devtools不会起作用,即应用不会restart -->
  			<fork>true</fork>
  		</configuration>
  	</plugin>  	
  	
  	</plugins>
  	
  	</build>

  

App.java

package boot2.muyang.app;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

@SpringBootApplication
public class App {

	
	private static Logger logger = Logger.getLogger(App.class);

	
	
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters()
	{
		
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(
				SerializerFeature.PrettyFormat
				);
		
		fastConverter.setFastJsonConfig(fastJsonConfig);
		
		HttpMessageConverter<?> converter = fastConverter;
		return new HttpMessageConverters(converter);
	}
	
	public static void main(String[] args)
	{
		logger.info("----: run start");
		SpringApplication.run(App.class, args);
		logger.info("----: runing");
	}
	
}

  

 

application.properties不能有空格

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

  

HelloController.java

package boot2.muyang.app.controller;

import java.util.Map;

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



@Controller
@RequestMapping("/hello")
public class HelloController {


	
	@RequestMapping("/index")
	public String index(Map<String,Object> map){
		map.put("name", "Andy");
		return "index";
	}
}

  

 

 

以上是关于spring boot:创建一个简单的web(maven web project)的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 的静态资源处理

如何利用spring boot做一个简单的web应用

Spring Boot2(002):手动创建第1个SpringBoot2简单应用——“HelloWorld” web 工程

spring boot web开发 简单的增删改查和spring boot 自带的Junit测试 案例

Spring Boot with hibernate:创建复合键和多对多关系

使用 Spring-Boot 创建一个简单的 JAR 而不是可执行的 JAR