SpringMVC简单总结-day01

一、
xml表头信息:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.sprihttp://marketplace.eclipse.org/marketplace-client-intro?mpc_install=1794107ngframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>

二、
注解:
@RequestMapping(value="wecome",method = RequestMethod.POST,params = {"name2=zs","age!=23","!height"}) //映射
说明:
method:方法
params:参数
1.必须有name="name2"参数, exa:<input type="text" name="name2"> 值为zs
2.age!=23 a.如果有name="age",则age值不能是23
b.没有age
3.!height 不能name="height"的属性

@RequestMapping(value="wecome2",headers = {"Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","Accept-Encoding=gzip, deflate, br"})
说明:
headers:浏览器请求头信息,注意用不用冒号,而是=

三、
ant风格的请求路径
? 单字符
* 任意个字符(0或多个)
** 任意目录

@RequestMapping(value="wecome3/**/test")
接受示例:
<a href="wecome3/abc/xyz/qwe/test"></a>

@RequestMapping(value="wecome3/a?c/test")
<a href="wecome3/abc/test"></a>

四、
通过@PathVariable获取动态参数

之前的jsp方法:
<a href="${pageContext.request.contextPath }/springMVCHandler/wecome4?name=zs">登录</a>

String zs = request.getParamater("name");

SpringMVC方法:
<a href="${pageContext.request.contextPath }/springMVCHandler/wecome4/zs">登录</a>

@RequestMapping(value="wecome4/{name}")
public String wecome4(@PathVariable("name") String name) {
System.out.println(name);
return "success";
}

五、
web.xml中的servlet配置可以用插件,alt+问号,点击dispatcherservlet
指定springmvc配置文件的路径,如果要省略,必须放到默认路径,默认路径是WebContent/WEB-INF下
/WEB-INF/(servlet-name的值)-servlet.xml

六、
REST风格:软件编程风格

Springmvc:
GET:查
POST:增
DELETE:删
PUT:改

普通浏览器,只支持get post方法;
其他请求方式,如delete|put请求是通过过滤器新加入的支持

springmvc实现:put|post请求方式步骤
a.增加过滤器
<!-- 增加HiddenHttpMethodFilter过滤器:目的是给普通浏览器,增加put|delete请求方式 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

b.表单
<form action="${pageContext.request.contextPath }/springMVCHandler/testRest/张三.html" method="post">
<input type="hidden" name="_method" value="DELETE">
<input type="submit" value="删">
</form>
i:必须是post发式
ii:通过隐藏域的value值,设置实际的请求方式DELETE|PUT

c.控制器
@ResponseBody
@RequestMapping(value="testRest/{id}",method = RequestMethod.PUT) //映射
public String testPut(@PathVariable("id") Integer id) {
// 默认请求转发
System.out.println("post:改"+id);
// Service层实现真正的删
return "success";
}
通过 method=RequestMethod.DELETE 匹配具体的请求方式

七、
@RequestParam("uname") String name:
说明:
接受前台传递的值,等价于request.getParameter("uname")

@RequestParam("uname") String name,@RequestParam(value="uage",required = false,defaultValue = "23") Integer age)
说明:
required=false:该属性不是必须的。
defaultValue="23":默认值23

获取请求头信息 @RequestHeader
public String testHander(@RequestHeader("Accept") String header)
说明:
通过@RequestHeader("Accept") String header
获取请求头中的Accept的值,并将值保存到header中

@CookieValue
(前置知识:服务端在接受客户端第一次请求时,会给客户端分配一个session(该session包含一个sessionId),并且服务端会在第一次响应客户端时,请该sessionId复制给JSESSIONID并传递给客户端的cookie中)

小结:
SpringMVC处理各种参数的流程/逻辑:
请求:前端发请求a->@RequestMappting("a")
处理请求中的参数xyz:
@RequestMapping(value="testCookie") //映射
public String testCookie(@CookieValue("JSESSIONID") String cookie)
}

八、
使用对象(实体类Student)接受请求参数

在SpringMVC中使用原生态的Servlet API:HttpServletRequest

posted @ 2021-05-11 10:59  蔡地像徐坤  阅读(42)  评论(0编辑  收藏  举报