SpringBoot無廢話入門03:SpringMVC支持

1.默認配置java

        Springboot對於路徑的默認位置爲:web

        spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/spring

        咱們能夠將資源文件放置上面的目錄中。咱們也能夠將資源文件放置到webapp中,好比讓咱們放置一個圖片到webapp的static目錄下,能夠看到,apache

2.配置jsp支持api

        Springboot默認支持Thymeleaf模版引擎,咱們能夠關閉它轉而默認支持jsp。tomcat

        在application.properties中配置以下,mvc

#jsp 支持app

spring.mvc.view.suffix=.jspwebapp

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

#關閉默認模板引擎

spring.thymeleaf.cache=false

spring.thymeleaf.enabled=false

        加入對servlet和jsp的pom依賴,以下:

<?xml version="1.0" encoding="UTF-8"?>

<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>

  <packaging>war</packaging>

  <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>2.1.3.RELEASE</version>

  </parent>

  <name>testboot01</name>

  <groupId>com.zuikc</groupId>

  <artifactId>testboot01</artifactId>

  <version>1.0-SNAPSHOT</version>

  <dependencies>

      <!--jsp支持, servlet 依賴. -->

      <dependency>

          <groupId>javax.servlet</groupId>

          <artifactId>javax.servlet-api</artifactId>

          <scope>provided</scope>

      </dependency>

      <dependency>

          <groupId>javax.servlet</groupId>

          <artifactId>jstl</artifactId>

      </dependency>

      <!-- tomcat 的支持.-->

      <dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-tomcat</artifactId>

      </dependency>

      <dependency>

          <groupId>org.apache.tomcat.embed</groupId>

          <artifactId>tomcat-embed-jasper</artifactId>

          <scope>provided</scope>

      </dependency>

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

  </dependencies>

  <build>

    <plugins>

      <plugin>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-maven-plugin</artifactId>

      </plugin>

    </plugins>

  </build>

</project>

 

        讓咱們建立一個新的controller,如StudentController,

package com.zuikc;

import org.springframework.stereotype.Controller;

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

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;

@Controller

public class StudentController {

    @RequestMapping(value = {"test01"})

    public String view(Map<String, Object> map) {

        map.put("name", "zuikc.com");

        map.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        return "views/test01";

    }

}

        繼而,讓咱們去前臺建立jsp界面,

        搞定,執行如下吧,看看結果如何:http://localhost:9090/test01

3.自定義消息轉換器

        能夠直接在HelloApplicaiton中直接加入,

    @Bean

    public StringHttpMessageConverter stringHttpMessageConverter(){

        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));

        return converter;

    }

4.自定義攔截器

        定義一個的WebMvcConfigurerAdapter子類,重點是要將其配置configuration,以下,

package com.zuikc;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@Configuration

public class MyConfig extends WebMvcConfigurerAdapter {

    @Override

    public void addInterceptors(InterceptorRegistry registry) {

        HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {

            @Override

            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

                    throws Exception {

                System.out.println("自定義攔截器............");

                return true;

            }

            @Override

            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

            }

            @Override

            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

            }

        };

        registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");

    }

}

        而後運行之,看看自定義攔截器執行沒有哦。

5.自定義配置

        在WebMvcConfigurerAdapter的子類中,咱們發現還能夠override其它的一些方法,好比override configureMessageConverters,這是自定義消息管理器的第二種實現,

    @Override

    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        StringHttpMessageConverter converter  = 

new StringHttpMessageConverter(Charset.forName("UTF-8"));

        converters.add(converter);

    }

        經過override這些方法,就能夠實現更多的自定義配置。這裏就再也不一一舉例的。

        感謝關注「碼農星球」。本文版權屬於「碼農星球」。咱們提供諮詢和培訓服務,關於本文有任何困惑,請關注並聯系咱們。

相關文章
相關標籤/搜索