十天熟练掌握SpringMvc第六天

共 8185字,需浏览 17分钟

 ·

2023-05-08 15:23

Spring MVC 构造URI

1 Spring MVC中,使用了UriComponentsBuilderUriComponents两个类来提供URI的加密解密。

UriComponents uriComponents = UriComponentsBuilder.fromUriString(

"http://example.com/hotels/{hotel}/bookings/{booking}").build();

URI uri = uriComponents.expand("42", "21").encode().toUri();

UriComponents 是不可变对象。因此 expand() encode() 操作在必要的时候会返回一个新的实例。

2 也可以使用一个URI组件实例对象来实现URI的填充与加密:

UriComponents uriComponents = UriComponentsBuilder.newInstance()

.scheme("http").host(" example.com").path("/hotels/ {hotel}/bookings/{booking}").build()

.expand("42", "21")

.encode();

3 servlet开发中,可以使用ServletUriComponentsBuilder类 提供一个静态工厂方法 可以从请求获取URI

ServletUriComponentsBuilder ucb = ServletUriComponentsBuilder.fromContextPath(request)

.path("/accounts").build()

4 如果DispatchServlet是通过名字请求映射的,还可以通过DispatchServlet获取请求

ServletUriComponentsBuilder ucb = ServletUriComponentsBuilder.fromServletMapping(request)

.path("/accounts").build() ,也是基于父类 Servlet 的静态工厂方法

Spring MVC 提供地区信息

DispatcherServlet 为你提供了自动使用用户的地区信息来解析消息的能力。而这,是通过 LocaleResolver 对象来完成的。

一个请求进入处理时, DispatcherServlet 会查找一个地区解析器。如果找到,就尝试使用它来设置地区相关的信息。

通过调用 RequestContext.getLocale() 都能取到地区解析器所解析到的地区信息。

此外,如果你需要自动解析地区信息,你可以在处理器映射前加一个拦截器

Spring MVC 获取时区信息

除了获取客户端的地区信息外,有时他们所在的时区信息也非常有用。

LocaleContextResolver 接口为 LocaleResolver 提供了拓展点,

允许解析器在 LocaleContext 中提供更多的信息,这里面就可以包含时区信息。

如果用户的时区信息能被解析到,那么你总可以通过 RequestContext.getTimeZone() 方法获得。

时区信息会自动被 SpringConversionService 下注册的日期 / 时间转换器 Converter 及格式化对象 Formatter 所使用。

SpringMVC Accept 请求头解析器

AcceptHeaderLocaleResolver 解析器会检查客户端(比如,浏览器,等)

所发送的请求中是否携带 accept-language 请求头。通常,该请求头字段中包含了客户端操作系统的地区信息。

不过请注意,该解析器不支持时区信息的解析。()

SpringMVC Cookie 解析器

CookieLocaleResolver 解析会检查客户端是否有 Cookie ,里面可能存放了地区 Locale 或时区 TimeZone 信息。

如果检查到相应的值,解析器就使用它们。通过该解析器的属性,你可以指定 cookie 的名称和其最大的存活时间。

定义一个 CookieLocaleResolver

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">

<property name="cookieName" value="clientlanguage"/>

<!-- 单位为秒。若设置为-1,则cookie不会被持久化(客户端关闭浏览器后即被删除) -->

<property name="cookieMaxAge" value="100000">

</bean>

CookieLocaleResolver 的属性

cookie 名) cookieName :默认值 classname + LOCALE

cookieMaxAge:默认值 Integer.MAX_INT

cookie 被保存在客户端的最长时间。如果该值为 -1

那么 cookie 将不会被持久化,在客户端浏览器关闭之后就失效了)

cookiePath :存放位置 默认为 /

SpringMVC Session 解析器

SessionLocaleResolver 允许你从 session 中取得可能与用户请求相关联的地区 Locale 和时区 TimeZone 信息。

CookieLocaleResolver 不同,这种存取策略仅将 Servlet 容器的 HttpSession 中相关的地区信息存取到本地。

因此,这些设置仅会为该会话( session )临时保存, session 结束后,这些设置就会失效。

但该解析器与其他外部 session 管理机制,比如 Spring Session 项目等,并没有直接联系。

SessionLocaleResolver 仅会简单地从与当前请求 HttpServletRequest 相关的 HttpSession 对象中,

取出对应的属性,并修改其值,仅此而已

SpringMVC 地区更改拦截器

你可以在处理器映射前添加一个 LocaleChangeInterceptor 拦截器来更改地区信息。

它能检测请求中的参数,并根据其值相应地更新地区信息。

它通过调用 LocaleResolver setLocale() 方法来更改地区。

*.view 路径并且携带了 siteLanguage 参数的资源请求更改地区。

一个 URL < http://www.sf.net/home.view?siteLanguage=nl > 的请求将会将站点语言更改为荷兰语。

<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">

<property name="paramName" value="siteLanguage"/>

</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="interceptors">

<list>

<ref bean="localeChangeInterceptor"/>

</list>

</property>

<property name="mappings">

<value>/**/*.view=someController</value>

</property>

</bean>

Spring MVC 主题解析器

定义主题控制器

要在你的应用中使用主题,你必须实现一个 org.springframework.ui.context.ThemeSource 接口。

WebApplicationContext 接口继承了 ThemeSource 接口,但主要的工作它还是委托给接口具体的实现来完成。

默认的实现是 org.springframework.ui.context.support.ResourceBundleThemeSource

它会从 classpath 的根路径下去加载配置文件。如果需要定制 ThemeSource 的实现

定义主题,定义之后,你要决定使用哪个主题。

DispatcherServlet 会查找一个名称为 themeResolver bean 以确定使用哪个 ThemeResolver 的实现。

主题解析器的工作原理与地区解析器 LocaleResolver 的工作原理大同小异。

它会检测,对于一个请求来说,应该使用哪个主题,同时它也可以修改一个请求所应应用的主题。

Spring 提供了下列的这些主题解析器:

1 FixedThemeResolver 选择一个固定的主题,这是通过设置defaultThemeName这个属性值实现的

2 SessionThemeResolver 请求相关的主题保存在用户的HTTP会话(session)中。

对于每个会话来说,它只需要被设置一次,但它不能在会话之间保存

3 CookieThemeResolver 选中的主题被保存在客户端的cookie

Spring 提供了一个主题更改拦截器 ThemeChangeInterceptor ,支持主题的更换。

Spring MVC 文件上传

通用的多路上传解析器 CommonsMultipartResolver

<bean id="multipartResolver" class=" org.springframework.web.multipart.commons.CommonsMultipartResolver ">

<!-- 支持的其中一个属性,支持的最大文件大小,以字节为单位 -->

<property name="maxUploadSize" value="100000"/>

</bean>

上传解析器 CommonsMultipartResolver jar 包是 commons-fileupload.jar

DispatcherServlet 发现多部分请求,把请求转发给多路配置的解析器。

解析器把 HttpServletRequest 请求对象封装成多路文件上传的请求对象 MultipartHttpServletRequest

Servlet3.0 以下,解析器不同, 3.0 以下的解析器不支持基本配置

Spring MVC 处理表单中的文件上传

创建一个接受文件上传的表单将允许用于直接上传整个表单。

编码属性( enctype="multipart/form-data" )让浏览器知道如何对多路上传请求的表单进行编码( encode )。

<form method="post" action="/form" enctype="multipart/form-data">

<input type="text" name="name"/>

<input type="file" name="file"/>

<input type="submit"/>

</form>

接受参数的类型是 MultipartHttpServletRequest ,或 MultipartFile

@RequestMapping(path = "/form", method = RequestMethod.POST)

public String handleFormUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) {

if (!file.isEmpty()) {

byte[] bytes = file.getBytes();

// store the bytes somewhere

return "redirect:uploadSuccess";

}


浏览 29
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报