Spring MVC--8.JSON、国际化、文件上传
来源:互联网 发布:java设置全局编码格式 编辑:程序博客网 时间:2024/06/16 21:38
九、处理JSON
1.基本步骤
(1)加入 jar 包:
(2)编写目标方法,使其返回 JSON 对应的对象或集合
@ResponseBody @RequestMapping("/testJson") public Collection<Employee> testJson(){ return employeeDao.getAll(); }
(3)在方法上添加@ResponseBody 注解
2. HttpMessageConverter<T>
HttpMessageConverter<T>是 Spring3.0 新添加的一个接口,负责将请求信息转换为一个对象(类型为 T),将对象(类型为 T)输出为响应信息
(1)接口定义的方法:
Boolean canRead(Class<?>clazz,MediaType – mediaType):
指定转换器可以读取的对象类型,即转换器是否可将请求信息转换为 clazz 类型的对象,同时指定支持 MIME 类型(text/html,applaiction/json等)
Boolean canWrite(Class<?>clazz,MediaType mediaType):
指定转换– 器是否可将 clazz 类型的对象写到响应流中,响应流支持的媒体类型在MediaType 中定义。
LIst<MediaType>getSupportMediaTypes():
该转换器支持的媒体– 类型。
T read(Class<? extends T>clazz,HttpInputMessage inputMessage– ):
将请求信息流转换为 T 类型的对象。
void write(T t,MediaTypecontnetType,HttpOutputMessgae –outputMessage):
将T类型的对象写到响应流中,同时指定相应的媒体类型为 contentType。
(2)运行流程
3.使用 HttpMessageConverter<T>
使用 HttpMessageConverter<T> 将请求信息转化并绑定到处理方法的入参中或将响应结果转为对应类型的响应信息,Spring提供了两种途径:
使用 @RequestBody / @ResponseBody– 对处理方法进行标注
使用 HttpEntity<T> / ResponseEntity<T>作为处理方法的入参或返回值
当控制器处理方法使用到 @RequestBody/@ResponseBody 或HttpEntity<T>
/ResponseEntity<T>时, Spring 首先根据请求头或响应头的Accept属性选择匹配的HttpMessageConverter, 进而根据参数类型或泛型类型的过滤得到匹配的HttpMessageConverter, 若找不到可用的HttpMessageConverter 将报错
@RequestBody 和 @ResponseBody 不需要成• 对出现
<br>模拟文件上传 <form action="testHttpMessageConverter" method="POST" enctype="multipart/form-data"> File:<input type="file" name="file"> Desc:<input type="text" name="desc"> <input type="submit" value="提交"> </form>
@ResponseBody @RequestMapping("/testHttpMessageConverter") public StringtestHttpMessageConverter(@RequestBody String body){ System.out.println(body); return "helloworld! "+ newDate(); }
<br>模拟文件下载 <a href="testResponseEntity">Test ResponseEntity</a>
@RequestMapping("/testResponseEntity") public ResponseEntity<byte[]>testResponseEntity(HttpSession session) throws IOException{ byte [] body = null; ServletContextservletContext = session.getServletContext(); InputStreamin = servletContext.getResourceAsStream("/files/abc.txt"); body= newbyte[in.available()]; in.read(body); HttpHeadersheaders = newHttpHeaders(); headers.add("Content-Disposition","attachment;filename=abc.txt"); HttpStatusstatusCode = HttpStatus.OK; ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers,statusCode); return response; }
十、国际化
1.国际化概述
默认情况下,SpringMVC 根据 Accept-Language参数判断客户端的本地化类型。
当接受到请求时,SpringMVC 会在上下文中查找一个本地化解析器LocalResolver,找到后使用它获取请求所对应的本地化类型信息。
SpringMVC还允许装配一个动态更改本地化类型的拦截器,这样通过指定一个请求参数就可以控制单个请求的本地化类型。
1.在页面上能够根据浏览器语言设置的情况对文本(不是内容), 时间, 数值进行本地化处理: 使用 JSTL 的 fmt 标签<a href="i18n">I18N PAGE</a>
i18n.properties:
i18n.user=Useri18n.password=Passwordi18n_zh_CN.properties:
i18n.user=用户名i18n.password=密码
i18n_en_US.properties:
i18n.user=Useri18n.password=Passwordi18n.jsp:
<fmt:message key="i18n.user"></fmt:message> <br><br> <a href="i18n2">I18N2 PAGE</a>
i18n2.jsp:
<fmt:message key="i18n.password"></fmt:message> <br><br> <a href="i18n">I18N PAGE</a>
springmvc.xml
<mvc:view-controller path="/i18n" view-name="i18n"/> <mvc:view-controller path="/i18n2" view-name="i18n2"/>
2. 可以在 bean 中获取国际化资源文件 Locale 对应的消息在 bean 中注入 ResourceBundleMessageSource 的示例, 使用其对应的 getMessage 方法即可 注销掉<mvc:view-controller path="/i18n" view-name="i18n"/>
@Autowired privateResourceBundleMessageSource messageSource; @RequestMapping("/i18n") public String testI18n(Localelocale){ Stringval = messageSource.getMessage("i18n.user",null,locale); System.out.println(val); return "i18n"; }
2.SessionLocaleResolver & LocaleChangeInterceptor 工作原理
3. 本地化解析器和本地化拦截器
AcceptHeaderLocaleResolver:根据 HTTP 请求头的Accept-Language参数确定本地化类型,如果没有显式定义本地化解析器, SpringMVC 使用该解析器。
CookieLocaleResolver:根据指定的 Cookie 值确定本地化类型
SessionLocaleResolver:根据 Session 中特定的属性确定本地化类型
LocaleChangeInterceptor:从请求参数中获取本次请求对应的本地化类型。
i18n.jsp
<br><br> <a href="i18n?locale=zh_CH">中文</a> <br><br> <a href="i18n?locale=en_US">英文</a>
springmvc.xml
<!-- 配置 SessionLocalResolver --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"> </bean> <mvc:interceptors> <!-- 配置LocaleChanceInterceptor --> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"></bean> </mvc:interceptors>
十一、文件上传
1.概述
Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的MultipartResolver实现的。Spring 用Jakarta Commons FileUpload技术实现了一个
MultipartResolver 实现类:CommonsMultipartResovler
Spring MVC 上下文中默认没有装配 MultipartResovler,因此默认情况下不能处理文件的上传工作,如果想使用Spring 的文件上传功能,需现在上下文中配置MultipartResolver
2.配置 MultipartResolver
defaultEncoding: 必须和用户 JSP 的 pageEncoding 属一致,以便正确解析表单的内容为了让 CommonsMultipartResovler 正确工作,必须先将 Jakarta Commons FileUpload 及 Jakarta Commons io的类包添加到类路径下。
index.jsp
<br><br> <form action="testFileUpload"method="POST" enctype="multipart/form-data"> File: <input type="file"name="file"/> Desc: <input type="text"name="desc"/> <input type="submit"value="Submit"/> </form>
Springmvc.xml
<!-- 配置 MultipartResolver --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"></property> <property name="maxUploadSize" value="1024000"></property> </bean>
@RequestMapping("/testFileUpload") public String testFileUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFilefile) throwsIOException{ System.out.println("desc: "+ desc); System.out.println("OriginalFilename: "+ file.getOriginalFilename()); System.out.println("InputStream: "+ file.getInputStream()); return "success"; }
源代码文件:http://download.csdn.net/detail/qq_26553781/9757497
- Spring MVC--8.JSON、国际化、文件上传
- spring mvc 文件上传
- spring MVC 文件上传
- Spring MVC文件上传
- Spring MVC文件上传
- spring mvc 文件上传
- Spring mvc 文件上传
- spring mvc 文件上传
- spring mvc 上传文件
- spring mvc 上传文件
- spring mvc 文件上传
- spring mvc 上传文件
- spring mvc文件上传
- Spring MVC 文件上传
- spring mvc 文件上传
- spring mvc文件上传
- Spring MVC文件上传
- spring mvc 文件上传
- 进程优先级 nice
- 关于使用 Okhttps,response.body().byteStream(),用decodeStream解析,Bitmap为null的解决方法
- AngularJS 自定义常量服务
- Keras 切换GPU
- Zoj 2868 Incredible Cows (分半dfs暴搜)
- Spring MVC--8.JSON、国际化、文件上传
- task struct
- Java通过代理访问互联网平台提供的WebService接口的一种方法
- MYSQL性能优化详解(一)
- 线程的简单小列子- 龟兔赛跑-继承Thread类
- AngularJS 路由和多视图
- 13.线程间通信
- Linux中内核控制块task_struck
- 跟着内核学框架-从misc子系统到3+2+1设备识别驱动框架