SpringMVC支持ant风格路径
? 表示任意的单个字符
* 表示任意的0个或者多个字符
** 表示任意的一层或者多层目录 在使用**时只能使用/**/XXX的形式
@Controller
public class antController {
//@RequestMapping(value = "/a?a/testAnt" )
// @RequestMapping(value = "/a*a/testAnt" )
@RequestMapping(value = "/**/testAnt" )
public String test1(){
return "success";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/a1a/testAnt}" method="get"> 测试SpringMVC支持Ant风格 ?</a>
<a th:href="@{/a123a/testAnt}" method="get"> 测试SpringMVC支持Ant风格 *</a>
<a th:href="@{/aa/testAnt}" method="get"> 测试SpringMVC支持Ant风格 **</a>
</body>
</html>
SpringMVC支持路径中的占位符
springMVC支持路径中的占位符,传到服务器后可以再相应的@RequestMapping注解的value属性中通过{}来获取这个参数在通过@PathVariable注解赋值到方法的形参上边
<a th:href="@{/testPath/1/zhangsan}" method="get"> 测试SpringMVC支持路径中的占位符 **</a>
@RequestMapping(value = "/testPath/{id}/{name}" )
public String test1(@PathVariable("id") String id,@PathVariable("name") String name){
System.out.println("传过来的的参数id是"+id);
System.out.println("传过来的的参数name是"+name);
return "success";
}
SpringMVC 获取请求参数
通过ServletAPI获取参数
<a th:href="@{/testServletApi(username='admin',password=123456)}">测试使用ServletApi获取参数</a>
@RequestMapping("/testServletApi")
public String testServletApi(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username="+username+",password="+password);
return "success";
}
通过控制器的形参获取参数
<form th:action="@{/testParam}" method="post">
username :<input type="text" name="username"><br>
password :<input type="password" name="password"><br>
hobby : <input type="checkbox" name="hobby" value="a">a
<input type="checkbox" name="hobby" value="b">b
<input type="checkbox" name="hobby" value="c">c<br>
<input type="submit" value="控制器形参获取参数">
</form>
@RequestMapping("/testParam")
public String testParam(String username,String password,String [] hobby){
System.out.println("username="+username+",password="+password+",hobby="+ Arrays.toString(hobby));
return "success";
}
也可以是使用String来接受参数,会根据逗号来分割
@RequestMapping("/testParam")
public String testParam(String username,String password,String hobby){
System.out.println("username="+username+",password="+password+",hobby="+hobby);
return "success";
}
@RequestParam
上变通过形参来获取的参数是必须 穿过来的跟形参名一直才可以获取,但是如果不一致,也改不了就需要使用@RequestParam注解来创建i请求参数和处理器形参之间的关系
有三个属性
- value 指定形参对应的请求参数的参数名
- required 是否必输 默认为true 不传的话会报错400,如果为false 非必输却不传输,获取就是null
- defaultValue 不管required为true或者false时,当value指定的参数没穿或者穿了空 都会自动用默认值赋值
@RequestHeader @CookieValue 把请求头信息,cookie跟控制器形参创建关系,也有三个属性 用法跟 @RequestParam相似
<a th:href="@{/testRequestParam(user_name='admin',password=123456)}">测试使用RequestParam获取参数</a>
@RequestMapping("/testRequestParam")
public String testRequestParam(@RequestParam("user_name") String username, String password){
System.out.println("username="+username+",password="+password);
return "success";
}
通过pojo获取请求参数
请求参数跟pojo对象的参数一致并作为形参接受就能赋值
<form th:action="@{/testpojo}" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
性别:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br>
年龄:<input type="text" name="age"><br>
邮箱:<input type="text" name="email"><br>
<input type="submit">
</form>
@RequestMapping("/testpojo")
public String testPOJO(User user){
System.out.println(user);
return "success";
}
@Data
public class User {
private Integer id;
private String username;
private String password;
private Integer age;
private String sex;
private String email;
}
结果 我们发现sex变成问号了,加上SpringMVC提供的编码过滤器CharacterEncodingFilter在web.xml中进行注册
必须下载其他过滤器之前,否则获取过一次参数后,在用编码过滤器就失效了(改天更下这个过滤器)
<!--配置springMVC的编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</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>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>