SpringMVC
2021-10-08 22:49:29 27 举报
AI智能生成
登录查看完整内容
此思维导图包含了有关SpringMVC的注解,配置文件等必要东西
作者其他创作
大纲/内容
<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.6.RELEASE</version></dependency>
导入依赖
<init-param><param-name>contextConfigLocation</param-name><param-value>classpath:mvc.xml</param-value></init-param>
局部参数:声明配置文件位置
配置核心控制器
后端控制器
告知springmvc哪些包存在被注解的类
注册注解开发驱动
1.捕获后端控制器的返回值=\"index\"
2.解析:在返回的值前后拼接==>\"/index.jsp\"
作用
<!--前缀--><property name=\"prefix\" value=\" /\"></property>
前缀
<!--后缀--><property name=”suffix\" value=\" .jsp\"></property>
后缀
试图解析器
配置文件
开发流程
@RequestMapping(\" /forw\" )class ForwardController{@RequestMapping(\" /test1\")public String testForward(){System . out . println(\"test forward1\");//转发跳转/views/users.jsp// return \"views/users\" ;//和下一-行等价return \"forward:/views/users. jsp\";}@RequestMapping(\" /test2\" )public String testForward2(){System . out. println(\"test forward2\");//转发到/forw/test1//return”forward:test1\";//相对路径(转发到本类中的test1)//转发到/forw/test1return \"forward:/forw/test1\"; //绝对路径}
举例
请求转发
@RequestMapping(\" /redir\")class RedirectController{@RequestMapping(\" /test1\")public String testRedirect1(){System . out. println(\"test redirect1\");//重定向到/redir/test1//return \" redirect:test1\"; //相对路径(转发到本类中的test1)return \" redirect:/redir/test1\";//绝对路径@RequestMapping(\" /test2\" )public String testRedirect2(){System . out . println(\"test redirect2\");//重定向到/views/users.jspreturn \"redirect:/view/user .jsp\";
重定向
跳转细节
跳转
<dependency><groupId>com. fasterxml. jackson . core</groupId><artifactId>j ackson-databind</artifactId><version>2.9.8</version></dependency>
@RequestMapping(\" /test1\
// @ResponseBody还可以用在handler的返回值 上@RequestMapping(\" /test2\" )public @ResponseBody List<User> hello2(){System . out. println(\"hello world\
使用@ResponseBody
Controller类上加了@RestController注解,等价于在类中的每个方法上都加了@ResponseBody
@Controller@RestControllerpublic class JsonController{@RequestMapping(\" /test1\")public User hello1(){System . out. println(\"hello world\");User user = new User();return user ;}/ /@Re sponseBody还可以用在handler的返回值上@RequestMapping(\" /test2\" )public List<User> he1lo2(){System. out . println(\"hello world\
使用@RestController
class User{private Integer id;private String name;private Boolean gender ;//set get}@RequestMapping(\" /users\")public String addUser(@RequestBody User user){/ /@RequestBody将请求体中的j son数据转换为java对象System. out. println(\"cap2\");System . out . println(\"Post user :\"+user);return \"index\" ;}
1.定义handler
var xhr = new XMLHttpRequest();xhr . open(“post\
2.Ajax发送json
使用@RequestBody
@JsonFormat(pattern-=\"yyyy-MM-dd HH:mm:ss\
public class User{private Integer id;private String name;@JsonFormat( pattern= \"yyyy-MM-dd HH:mm:ss\
日期格式化
@JsonProperty(\"new_ name\")
public class User{@JsonProperty(\"new_ id\") //不再使用原属性名,而是\"new_ .id\"private Integer id;private String name ;get/set}输出的json: {\"new_ id\
属性名修改
@JsonIgnore
public class User{private Integer id;@JsonIgnore //生成j son时,忽略此属性private String name;get/set}输出json时: {\"id\" :xx}
属性忽略
Jackson默认会输出null值的属性,如果不需要,可以排除。
@Jsonlnclude(JsonInclude.Include.NON_ NULL) /ull值属性不输出@JsonIncludel(value= Jsonlnclude.Include.NON EMPTY) // empty属性不输出(空串,长度为0的集合,null值)
public class User{private Integer id;@JsonInclude( JsonInclude . Include . NON NULL) // 若”name==null\
null和empty属性排除
@JsonSerialize(using = MySerializer.class) //使用MySerializer输出某属性
public class User {private Integer id; private String name;@JsonSerialize(using = MySerializer .class)private Double salary = 10000 .126;//在输出此属性时,使用MySerializer输出get/set则输出json时: {\"id\
自定义序列化
Jackson常用注解
<dependency><groupId>com. alibaba< /groupId><artifactId>fastjson< /artifactId><version>1 .2. 54</version></dependency>
安装FastJson
@ResponseBody @RequestBody @RestController使用方法不变
使用
@JSONField(format*\"yyy/MM/dd\")
@JSONField(name=\"birth\")
@JSONField(serialize = false)
忽略属性
@JSONField(serialzeFeatures = SerializerFeature.WriteMapNullValue)默认会忽略所有null值,有此注解会输出null
@JSONField(serialzeFeatures = SerializerFeature.WriteNullStringAsEmpty) null的String输出为\"\"
包含null值
@JSONField(serializeUsing = MySerializer2.class)
常用注解
FastJson
Json处理
SpringMVC
模型:即业务模型,负责完成业务中的数据通信处理,对应项目中的service和dao
Model
视图:渲染数据,生成页面。对应项目中的Jsp
View
控制器:直接对接请求,控制MC流程,调度模型,选择视图。对应项目中的Servlet
Controller
概念
1.MVC是现下软件开发中的最流行的代码结构形态
好处
MVC架构
请求参数和方法的形参同名即可
springmvc默认可以识别的日期字符串格式为:YYYY/MM/dd HH:mm:ss通过@DateTimeFormat可以修改默认日志格式
// idname gender// http://localhost : 8989/xxx/ .. /test1?id=1&name=zzz&gender=false&birth-2018-12-12 12 :20 :30@RequestMapping(\" /test1\
基本数据类型
请求参数和实体的属性同名即可
public class User {private Integer id;private String name ;@DateTimeFormat(pattern='”yyy-MM-dd\" )private Date birth;private Boolean gender ;//set/get ...}//http://localhost :8989/ ... /test2?id=1&name=zzz&gender=false&birth=2018-12-12 12 :20:30@RequestMapping(\" /test2\")public String testParam2(User user){System. out. println(\"test param2\");System . out. println(\"user:\" +user);return \"index\" ;}
实体收参
简单类型的数组
<form>......<input type=\" checkbox\" name= ”hobby\" value=\"fb\" />足球<input type=\" checkbox\" name=\" hobby\" value=\"bb\" />篮球<input type=\" checkbox\" name= ”hobby\" value=\"vb\" />排球</form>//http://localhost:8989/ .../test3?hobby=football&hobby=basketball@RequestMapping(\" /test3\")public String testParam3 (String[] hobby){for(String h:hobby){System. out. print(h+\" \");}return \" index\" ;}
数组收参
public class UserList {//private User[] users;private List<User> users;//set/get. .}// <input type=\"text\" name=\"users[0] .id\"/>// post请求: http://...?users[0] . id=1&users[0] .name=zhangsan&users[0]. birth=2018-12-12&users[1] . id=2&.@RequestMapping(\" /test4\")public String testPar am4(UserList userList){for(User user :userList . getUsers()){System. out. println(user);return \"index\" ;}
集合收参
// {id} 定义名为id的路径; [/hello/{id}] 的匹配能力和[/hello/*] 等价// http://localhost :8989/... /he1lo/10 {id}匹配到10@RequestMapping(\" /hello/{id}\")// @PathVariable将 {id }路径匹配到值赋给id参数//路径名和参数名相同则@PathVariable(\" id\" )可简写为@PathVariablepublic String testParam5(@PathVariable(\"id\") Integer id){System. out. print1n(\"id:\"+id);return \" index\" ;}// http://localhost :8989/.. /hello/tom {username }匹配到tom@RequestMapping(\" /he1lo/ {username }\")public String testParam6 (@PathVariable(\"username\") String name) {/ /将{username }路径匹配到的值赋给name参数System. out . println(\"username:\" +name);return \" index\" ;
路径参数
1.页面中字符集统一
<!--此过滤器会进行: request. setCharactorEncoding(\"utf-8\"); --><filter><filter-name> encoding< / filter-name><filter-class>org. springframework . web . filter .CharacterEncodingFilter</filter-class><init-param><par am-name> encoding</ par am-name><param-value>utf-8</ param-value></init-par am></filter><filter-mapping><filter-name>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping>
中文乱码
接受请求参数
静态资源问题
<servlet><servlet-name>mvc9</servlet -name><servlet-class>org. spr ingf r amework . web. servlet . DispatcherServlet</servlet-class></servlet><servlet -mapping><servlet-name>mvc9< /servlet-name><url-pattern>* . action</url-pattern></servlet -mapping>
解决方案1
DispathcerServlet的url-pattern依然采用\"/\
解决方案2
. mapping是访问路径,location是静态资源存放的路径
将/htm/** 中/**匹配到的内容,拼接到/hhh/后htp./...html/a.htmtl访问/hhh/a.html
解决方案3
静态资源
抽取handler中的冗余功能
preHandle--postHandle--afterCompletion
执行顺序
在handler之前执行:抽取handler中的冗余代码
在handler之后执行:进一步的响应定制
在页面渲染完毕之后执行:资源回收
定义拦截器
<mvc :interceptors><mvc :interceptor><mvc :mapping path=\" /inter/test1\"/><mvc :mapping path=\" /inter/test2\" /><mvc :mapping path=\" /inter/test*\"/> <!-- test开头--><mvc :mapping path=\" /inter/**\"/> <!-- /** 任意多级任意路径--><mvc :exclude-mapping path=\" /inter/a/**\"/> <!--不拦截此路径--><bean class=\" com. baizhi . interceptor . MyInter1\"></bean> < !--拦截器类-->< /mvc : interceptor> < /mvc :interceptors>
配置拦截路径
拦截器
0 条评论
回复 删除
下一页