springboot

Posted 认真的谁

tags:

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

@SpringBootApplication
public class demotest {

    public static void main(String[] args) {
        SpringApplication.run(demotest.class ,args);
    }
}

springboot 的启动类

@Controller
public class controllertest {
    @RequestMapping("/hello")
    @ResponseBody
    public Map<String,Object>hello(){
        Map<String,Object>map=new HashMap<String, Object>();
        map.put("msg","hello");
        return  map;
    }
}

对应的conroller

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>

pom文件配置

这里应注意启动类应该在conroller同一个包下,或者在启动器上一层包里,还有一个就是这里如果直接将配置文件中的版本号删掉不能用。需要加上版本号。

 

springboot 和servlet的整合

在启动类上添加@ServletComponentScan,启动扫描servlet

@SpringBootApplication
@ServletComponentScan
public class app {
    public static void main(String[] args) {
        SpringApplication.run(app.class,args);
    }
}

编写servlet类,并配置到spring中

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

另一种springboot和servlet整合的方式(不需要只要编写servlet就行,不用@ServletComponentScan,而且servlet类上面也不需要多余的注解@WebServlet)

    @Bean                                     //相当于@ServletComponentScan,上一种是自动扫描,这种是手动添加
    public ServletRegistrationBean register(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
public class SecondServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("second servlet...");
    }
}

springboot整合filter

@SpringBootApplication
@ServletComponentScan
public class app {
    public static void main(String[] args) {
        SpringApplication.run(app.class,args);
    }
}
//@WebFilter(filterName = "Firstfilter",urlPatterns = {"*.do","*.action"})     这种可以添加多种拦截规则
@WebFilter(filterName = "first",urlPatterns = "/first")
public class Firstfilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("ENTER");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("leave");
    }
    public void destroy() {
    }
}

整合filter方式二

public class app {
    public static void main(String[] args) {
        SpringApplication.run(app.class,args);
    }
    @Bean
    public ServletRegistrationBean register(){
        ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
        bean.addUrlMappings("/second");
        return bean;
    }
    @Bean
    public FilterRegistrationBean getfilter(){
        FilterRegistrationBean filterRegistration=new FilterRegistrationBean(new Firstfilter());
        filterRegistration.addUrlPatterns("/second");
        return filterRegistration;
    }
}
public class Firstfilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {
    }
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("ENTER");
        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("leave");
    }
    public void destroy() {
    }
}

springboot整合listener

@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
@WebListener
public class Firstlistener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.out.println("创建了监听器");
    }
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}

第二种方式通过函数注册

@SpringBootApplication
@ServletComponentScan
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }

    @Bean
    public ServletListenerRegistrationBean <Firstlistener> register(){
        ServletListenerRegistrationBean <Firstlistener>bean =new ServletListenerRegistrationBean<Firstlistener>(new Firstlistener());
        return bean;
    }
}

这种比上面的几种多了泛型,listener的编写和上面的一样,就是少了@WebListenner注解

springboot对静态资源的访问

1必须在classpath/static文件夹下面,就是在resources目录下创建static目录

 

 2或者在src/main/webapp目录下面,同样文件名字不能变

 

以上是关于springboot的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot中表单提交报错“Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported“(代码片段

Spring boot:thymeleaf 没有正确渲染片段

11SpringBoot-CRUD-thymeleaf公共页面元素抽取

学习小片段——springboot 错误处理

springboot 底层点的知识

springboot 底层点的知识