10.12 Interview: Spring MVC static resource release + how to realize forwarding and redirection + how to support json + setting time format + setting json key + serializing json value

1. What are the ways to release SpringMVC static resources?

3 types

1.在web.xml中,DispathcerServlet采用其他的url-pattern,此时,所有访问handler
的路径都要以 action结尾!!

<servlet>
  	<servlet-name>mvc9</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>mvc9</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>
2.在springMVC中,DispathcerServlet的url-pattern依然采用 "/",但追加配置

<!-- 
  额外的增加一个handler,且其requestMapping:  "/**" 可以匹配所有请求,但是优先级最低
  所以如果其他所有的handler都匹配不上,请求会转向 "/**" ,恰好,这个handler就是处理静态资源的
  处理方式:将请求转会到tomcat中名为default的Servlet
  -->
<mvc:default-servlet-handler/>
3.在springMVC中,修改访问路径
- mapping是访问路径,location是静态资源存放的路径
- 将/html/** 中 /**匹配到的内容,拼接到 /hhh/后
  http://..../html/a.html  访问 /hhh/a.html

<mvc:resources mapping="/html/**" location="/hhh/"/>

2. How does Spring MVC realize forwarding and redirection

转发:forward  或者  直接写跳转路径名称

重定向:redirect

3. How does SpringMVC support json?

加注解,@ResponseBody 或者 @RestController

4. How to set the time format when the bottom layer uses jackson to transfer json? How to set the key of json? How to serialize the value of json?

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")


@JsonProperty("new_name")


自定义序列化
@JsonSerialize(using = MySerializer.class) // 使用MySerializer输出某属性

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_39773004/article/details/109020984