1. JSON数据交互
1.1. 先导入json和Spring整合的jar包
1.2. Controller中接收json
1.2.1.1. jsp中发送json写法
|
1.2.1.2. controller中接收json
|
1.3. Controller中给客户端响应Json
|
2. 文件上传
2.1. 配置虚拟目录
在tomcat上配置图片虚拟目录,在tomcat下conf/server.xml中添加:
<ContextdocBase="F:\develop\upload\temp" path="/pic"reloadable="false"/>
访问http://localhost:8080/pic即可访问F:\develop\upload\temp下的图片。
2.2. 指定Tomcat放置图片的虚拟路径
2.3. 配置文件上传解析器
|
2.4. 接收上传文件,并将路径放置到对象中
|
3. 文件下载
|
4. 数据回显
4.1. 普通回显
将数据放置到Request域中
4.2. 直接回显
|
4.3. 用ModelAttribute注解完成回显
|
5. 类型转换器
5.1. 创建自定义类型转换器
|
5.2. springmvc文件中配置类型转换器
|
6. 拦截器
6.1. 为什么要配置拦截器
当需要使用权限控制时,可以对某些模块设置拦截器
6.2. 创建拦截器
|
6.3. 配置拦截器
|
6.4. 拦截器说明
preHandle | 在执行contoller方法之前执行,return true 代表继续执行,否则拦截 |
postHandle | 在执行controller方法之后,返回结果视图之前执行 |
afterCompletion | 在返回结果视图之后执行 * 这里可得到执行controller时的异常信息 |
拦截器链 | 配置多个拦截器,形成拦截器链 |
6.5. 登陆权限拦截器
6.5.1.1. 需求分析
1、有一个登录页面,需要写一个controller访问页面
2、登录页面有一提交表单的动作。需要在controller中处理。
a) 判断用户名密码是否正确
b) 如果正确 将session中写入用户信息
c) 返回登录成功,或者跳转到商品列表
3、拦截器。
a) 拦截用户请求,判断用户是否登录
b) 如果用户已经登录。放行
c) 如果用户未登录,跳转到登录页面。
6.5.1.2. LoginIntercepter拦截器代码操作
|
|
7. 异常处理
7.1. 定义异常解析器
|
7.2. springmvc.xml配置异常
|
7.3. 定义自定义异常
|
8. Restful支持
8.1. 什么是Restful
Restful就是一个资源定位及资源操作的风格。
不是标准也不是协议,
只是一种风格,是对http协议的诠释
8.2. 通常的URL:
http://localhost:8080/ssm/queryItems.action?id=1
8.3. Restful的URL写法:
http://localhost:8080/ssm/items/1
8.4. Restful使用方式
8.4.1.1. 修改web.xml中拦截action的方法为 *.action 为 /
*.action
代表拦截后缀名为.action结尾的
/
拦截所有但是不包括.jsp
/*
拦截所有包括.jsp
8.4.1.2. 设置静态资源不进行拦截
使用mvc标签单独文件类型配置
<mvc:resources location="/js/" mapping="/js/**"/>
使用mvc标签配置默认的处理器
<!--
可以访问任意静态资源 -->
<mvc:default-servlet-handler/>
8.4.1.3. Controller中写法
一个参数类型
@RequestMapping("/selectUserById/{id}")
public String selectUserById(@PathVariable Integer id, Model model) {
http://localhost:8080/Day03_SSM/selectUserById/2
一个参数,映射和参数名称不一致类型
@RequestMapping("/selectUserById/{xx}")
public String selectUserById(@PathVariable(value="xx") Integer id, Model model)
多参数类型
@RequestMapping("/selectUserById/{id}/{username}")
public String selectUserById(@PathVariable Integer id,@PathVariable String username, Model model)
http://localhost:8080/Day03_SSM/selectUserById/2/lisi