- 最近空闲之余用了四天半的时间简单的回顾了一下SPringMVC,该篇仅作为个人学习总结。
- Spring: IOC 和 AOP
- SpringMVC的执行流程
- 特点:
- 轻量级、简单易学
- 不需要很多依赖
- 高效、基于请求响应的MVC框架
- 与Spring兼容性好,无缝结合
- 约定优于配置
- 功能强大:RESTful、数据验证、格式化、本地化、主题
- 简洁灵活
- 执行流程
- 特点:
- @RequestMapping
- @GetMapping
- @PostMapping
- @DeleteMapping
- @Controller
- @Services
- @Respoister
- @Mapper
- @Param
- @PathVariable
- 蛇者ModelAndView对象,根据viewd 名称,和视图解析器跳到指定页面
- 转发
return "test" //默认转发
2.重定向
return ”redirect: index.jsp"
- 数据处理
- 如果名称和参数一样 就不做处理
- 如果名称和参数不一样,加注解@RequestParam("name")
- 如果提交的是一个对象,只需要保证表单域和对象的属性名一致,参数使用对象即可
- 将结果返回给前端
- ModeAndView
- 可以存储数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转。
- Model:接口,精简版的ModelMap
- 只有寥寥几个方法只适合用于存储数据,简化了新手对Model对象的操作和理解
- ModelMap:继承了LinkedHashMap,拥有全部的功能
- ModeAndView
- 乱码问题解决
- 过滤器解决乱码 实现Filter
- init()
- doFilter()
- request.setCharacterEncoding("utf-8");
- response.setCharacterEncoding("utf-8");
- chain.doFilter(request,response);
- destroy()
- web.xml中配置乱码过滤配置文件
- 过滤器解决乱码 实现Filter
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<filter-mapping>
<filter-nam>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</filter>
- Json
var user = {
name:"zenghao",
age:28,
sex:"男“
}
//Jiang js对象转换为json对象
var json = JSON.stringify(user);
//将JSON 对象转换为js对象
var obj = JSON.parse(json);
- 将后端的数据转换为JSON对象
- Jackson
ObjectMapper mapper = new ObjectMapper();
User user = new User("曾经",16,"男");
Date date = new Date();
mapper.writeValueAsString(user);
mapper.wirteValueAsString(date); //TimeStamp 时间戳
-
- fastjson
JSON.toJSONString()
- SpringMVC, Json乱码问题解决===用于处理Jackson
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bena class="org.springframework.http.converter.stringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http,converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-drivern>
- lombok的使用
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
private String name;
private int userId;
private String sex;
}
- 数据库连接池
dbcp:半自动化 , 不能自动连接 c3p0:自动化操作(自动化的加载配置未见,并且可以自动设置到对象中 druid: hikari: SpringBoot2.0默认集成
- 文件上传和下载
==================================配置===========================
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defailtEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760" />
<property name="maxInMemorySize" value="40960" />
</bean>
====================================代码===========================
//@RequestParam("file") 将name =file控件得到的文件封装成CommonsMultipartFile对象
//批量上传CommonsMultipartFile 为数组即可
@RequestMapping("/upload")
public String fileUpload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
//获取文件名 : file.getOriginalFilename();
String uploadFileName = file.getOriginalFilename();
//如果文件名为空,直接回到首页
if("".equals(uploadFileName)) {
return "redirect:/index";
}
//上传路径保存设置
String path = request.getServletContext().getRealPath("/upload");
//如果路径不存在,创建一个
File realPath = new File(path);
if(!realPath.exists()){
realPath.mkdir();
}
InputStream is = file.getInputStream(); //文件输入流
OutputStream os = new FileOutputStream(new File(realPath,uploadFileName)); //文件输出流
//读取写出
int len = 0;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer))!=-1) {
os.write(buffer,0,len);
os.flush();
}
os.close();
is.close();
return "redirect:/index";
}
《淘宝的十年革命》
《程序员修炼之道》