spring boot入门

一、spring、spring mvc、spring boot之间的关系

spring boot就是一个大框架里面包含了许许多多的东西,其中spring就是最核心的内容之一,当然也就包含spring mvc。spring mvc 只是spring 处理web层请求的一个模块。因此他们的关系大概就是这样:spring mvc  < spring < springboot。Spring Boot 设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。嵌入的 Tomcat,无需部署 WAR 文件Spring Boot 并不是对 Spring 功能上的增强,而是提供了一种快速使用 Spring 的方式。

二、快速开始一个spring boot项目

(1)使用 maven 构建 SpringBoot 项目,packaging选jar

(2)pom.xml文件里边增加如下配置
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

(3)编写一个spring mvc controller类,增加相应注解
/**
 *spring boot例子 
 *1、编写spring mvc controller类
 *2、添加@Controller
 *3、添加@RequestMapping("/test")
 *4、返回值要做jackson处理添加@ResponseBody
 */
@Controller
public class TestController {
    @RequestMapping("/test")
    @ResponseBody
    public Map test() {
        Map m = new HashMap<String, Object>();
        m.put("ceshi", "ok");
        return m;
    }
}

(4)添加一个启动类,启动类上增加注解@SpringBootApplication,注意该启动类必须位于controller类的同级或上级目录下
/**
 * 启动类
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
    }
}

(5)运行启动类,访问浏览器结果如下

 

三、整合servlet、filter、listener

(1)整合servlet,在编写好的servlet上加注解@WebServlet(name = "FirstServlet", urlPatterns = "/first" )

@WebServlet(name = "FirstServlet", urlPatterns = "/first" )
public class FirstServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("First servlet!");
}
}

(2)整合filter,在filter上加注解@WebFilter(filterName = "FirstFilter",urlPatterns = {"/first"})

@WebFilter(filterName = "FirstFilter",urlPatterns = {"/first"})
public class FirstFilter implements Filter {
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("过滤开始。。。");
        chain.doFilter(request, response);
        System.out.println("过滤结束");
    }
}

(3)整合listener,在listener上加注解@WebListener

@WebListener
public class FirstListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
    System.out.println("监听器");
}
public void contextDestroyed(ServletContextEvent sce) {
        }    
}

(4)启动类上加注解@SpringBootApplication@ServletComponentScan

@SpringBootApplication
@ServletComponentScan
public class test {
    public static void main(String[] args) {
        SpringApplication.run(test.class, args);
        System.out.println("hello");
    }
}

四、访问静态资源,在resources目录下增加static目录,静态资源放在下边,可以直接通过文件名访问

五、文件上传

(1)static目录下编写upload.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="filename"/>
<button type="submit">提交</button>
</form>
<img alt="测试" src="images/1.png">
</body>
</html>

(2)编写controller,增加注解,注意参数名字filename要与页面的form里的名字一样,否则需要其他配置

import java.util.Map;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class FileUploadController {
    @RequestMapping("/fileUpload")
    public Map<String, Object> fileUpload(MultipartFile filename){
        MultipartFile f = filename;
        System.out.println("-----------"+filename.getOriginalFilename());
        try {
            filename.transferTo(new File("d:/"+filename.getOriginalFilename()));
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Object> map = new HashMap();
        map.put("ceshi", "ok");
        return map; 
    }
}

(3)上传文件最大默认为1M超过需要在resources目录下边增加配置文件application.properties,里边增加如下配置(不同springboot版本可能不一样,本人用的是2.X)

spring.servlet.multipart.max-file-size=20MB
spring.servlet.multipart.max-request-size=20MB

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值