springmvc部分要点笔记

编程入门 行业动态 更新时间:2024-10-27 04:27:56

springmvc部分<a href=https://www.elefans.com/category/jswz/34/1769303.html style=要点笔记"/>

springmvc部分要点笔记

RequestMapping

一、参数

value

method(get post delete put)

(异常 404 资源不存在 405 参数有问题 500程序有异常)、

params 参数是强制限定请求参数的名称或者值,或者名称和值

二、请求风格

传统url:localhost:8080/hello?id=12&name=tom

restful url : localhost:8080/hello/12/tom

@RequestMapping("/test2/{id}/{name}")
public String test2(@PathVariable("id") Integer id ,@PathVariable("name") String name){System.out.println("name : " + name + "id : " + id);return SUCCESS;
}
三、映射cookie
@RequestMapping("/getcookie")
public String test3(@CookieValue("JSESSIONID") String sessionId){System.out.println("JSESSIONID" + sessionId);return SUCCESS;
}
四、获取模型对象数据

直接在方法参数中定义一个pojo变量,支持级联赋值

@RequestMapping("/adduser")
public String test4(User user){System.out.println(user);return SUCCESS;
}
<form action="/hello/adduser" method="post"><table><tr><td>姓名:</td><td><input type="text" name="name"></td></tr><tr><td>id :</td><td><input type="text" name="id"></td></tr><tr><td>编码 :</td><td><input type="text" name="address.code"></td></tr><tr><td>值 :</td><td><input type="text" name="address.value"></td></tr><tr><td></td><td><input type="submit" value="提交"></td></tr></table>
</form>

支持级联赋值,但必须要求包含的类有无参构造,否则无法创建会出现下面异常

org.springframework.beans.NullValueInNestedPathException: Invalid property 'address' of bean class [com.zw.entity.User]: Could not instantiate property type [com.zw.entity.Address] to auto-grow nested property path; nested exception is java.lang.NoSuchMethodException: com.zw.entity.Address.<init>()

乱码问题解决:

<filter><filter-name>EncodingFilter</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>
<filter-mapping><filter-name>EncodingFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>

五、重定向

redirect:物理路径

forward:物理路径

否则会无限次重新访问

@RequestMapping("/test2/{id}/{name}")
public String test2(@PathVariable("id") Integer id ,@PathVariable("name") String name){System.out.println("name : " + name + "id : " + id);return "redirect:/index.jsp";
}
@RequestMapping("/test2/{id}/{name}")
public String test2(@PathVariable("id") Integer id ,@PathVariable("name") String name){System.out.println("name : " + name + "id : " + id);return "forward:/index.jsp";
}

数据绑定

基本数据类型

这里的请求id不能为空

http://localhost:8080/hello/baseType?id=12

@RequestMapping("/baseType")
@ResponseBody
public String test5(int id){return "id:"+id;
}

http://localhost:8080/hello/baseType

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aVICy4s7-1628511898167)(D:\app\Typora\img/image-20210511101041935.png)]

http://localhost:8080/hello/baseType?id=a

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-e2XRnxpY-1628511898170)(D:\app\Typora\img/sffsf)]

包装类
@RequestMapping("/packageType")
@ResponseBody
public String test6(Integer id){return "id:"+id;
}

请求参数可以为空显示为null

@RequestMapping("/paramrequire")
@ResponseBody
public String test7(@RequestParam(value = "id",required = true,defaultValue = "0") Integer id){return "id:"+id;
}

设置requestParam注解参数,设置其为必须,默认值为0

数组绑定

把请求参数中的names属性都添加到数组中去

@RequestMapping("/arrayType")
@ResponseBody
public String test8(String[] names){StringBuffer buffer = new StringBuffer();for(String name : names){buffer.append(name+" ");}return buffer.toString();
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-suOnD5IF-1628511898171)(D:\app\Typora\img/image-20210511104800464-1620701287165.png)]

绑定对象
@Data
public class User {private Integer id ;private String name ;<

更多推荐

springmvc部分要点笔记

本文发布于:2024-03-07 17:00:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1718384.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要点   笔记   springmvc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!