SpringBoot无废话入门03:SpringMVC支持
1.默认配置
springboot对于路径的默认位置为:
spring.resources.static-locations=classpath:/meta-inf/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
我们可以将资源文件放置上面的目录中。我们也可以将资源文件放置到webapp中,比如让我们放置一个图片到webapp的static目录下,可以看到,
2.配置jsp支持
springboot默认支持thymeleaf模版引擎,我们可以关闭它转而默认支持jsp。
在application.properties中配置如下,
#jsp 支持
spring.mvc.view.suffix=.jsp
spring.mvc.view.prefix=/web-inf/views/
#关闭默认模板引擎
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这些方法,就可以实现更多的自定义配置。这里就不再一一举例的。
感谢关注“码农星球”。本文版权属于“码农星球”。我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系我们。