SpringMVC入门(二)

date: 2019/8/9 16:55:06


MVC 是一种著名的设计模式,特别是在 Web 应用程序领域。



url 映射


1.普通的 url 映射

1
2
3
4
5
6
7
//1.普通URL映射,指定value属性值,如果一个方法个了两个url(多个)这些url之间的关系是"或的关系"
@RequestMapping(value= {"aaa","bbb"})
public ModelAndView show() {
System.out.println( "我是控制器的show方法");
return new ModelAndView("show");
}

2.url 模板模式映射

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@RequestMapping(value= {"/user/{userId}"})
//{XXX}占位符,请求的url可以是“/user/123456”或“/user/abcd”,
通过@PathVariable可以提取url模板中的{XXX}中的XXX变量
@RequestMapping(value= {"/user/{userId}/house"})
//这样也是可以的,请求 url 可以是“/user/123456/house”
@PathVariable(value="XXX")
//路径变量注解:匹配处理器适配器传入的参数值,在方法形参用该注解接收
//2.1 url模板映射
@RequestMapping(value="test1/{userId}")
public ModelAndView test1() {
System.out.println("test1");
return new ModelAndView("show");
}
//2.2 url模板映射
@RequestMapping(value="test2/{userId}")
public ModelAndView test2(@PathVariable(value="userId") String uId) {
System.out.println(uId);
return new ModelAndView("show");
}
//2.3 url模板映射
@RequestMapping(value="test3/{userId}")
public ModelAndView test3(@PathVariable String userId) {
System.out.println(userId);
return new ModelAndView("show");
}
//2.4 url模板映射
@RequestMapping(value="test4/{userId}/{userName}")
public ModelAndView test4(@PathVariable String userId,@PathVariable String userName) {
System.out.println(userId+","+userName);
return new ModelAndView("show");
}

3.Ant 风格映射方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//3.1 ant风格映射方法,**符号代表可以在这里面匹配N级
@RequestMapping("/test1/**")
public ModelAndView test1() {
System.out.println( "我是控制器的test1方法");
return new ModelAndView("show");
}
//3.2 一个*符号可以匹配一级
@RequestMapping("/test2/*")
public ModelAndView test2() {
System.out.println( "我是控制器的test2方法");
return new ModelAndView("show");
}
//3.3 ?问号匹配的是一个字符,几个问号就匹配几个字符
@RequestMapping("/test3/?")
public ModelAndView test3() {
System.out.println( "我是控制器的test3方法");
return new ModelAndView("show");
}
//3.4 url混合映射
@RequestMapping("/test4/**/{hello}/?")
public ModelAndView test3(@PathVariable String hello) {
System.out.println( "hello=["+ hello +"]");
return new ModelAndView("show");
}

4.正则映射方式

1
2
3
4
5
6
7
//4. 正则映射实际上就是对 url 模板变量值进行限制,采用正则表达式进行限制
@RequestMapping("/test1/{password:\\d+}")
public ModelAndView test1(@PathVariable String password) {
System.out.println("password = ["+ password +"]");
return new ModelAndView("show");
}

5.映射提交方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Controller
@RequestMapping("method")
public class MyMethodControllerer {
@RequestMapping(value = "/register",method = RequestMethod.GET)
public ModelAndView showForm() {
return new ModelAndView("register");
}
@RequestMapping(value = "/register",method = RequestMethod.POST)
public ModelAndView subForm() {
return new ModelAndView("hello");
}
}

参数限制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//1.当前映射限制在请求当前的功能请求处理方法的时候需要加上一个参数名叫做user的
@RequestMapping(value = "/test1",params = {"user"})
public ModelAndView test1() {
return new ModelAndView("hello");
}
//2.多个请求参数限制之间是“且”的关系
@RequestMapping(value = "/test2",params = {"user","password"})
public ModelAndView test2() {
return new ModelAndView("hello");
}
//3.!请求参数千万不能有这个参数名
@RequestMapping(value = "/test3",params = {"!user","password"})
public ModelAndView test3() {
return new ModelAndView("hello");
}
//4.还可以对参数值进行限制,代表请求中必须要有一个参数名叫做user,而且值必须等于admin
@RequestMapping(value = "/test4",params = {"user=admin"})
public ModelAndView test4() {
return new ModelAndView("hello");
}
//5.还可以限制请求参数千万不要等于你的某个值
@RequestMapping(value = "/test5",params = {"user!=admin"})
public ModelAndView test5() {
return new ModelAndView("hello");
}


参数绑定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@Controller
@RequestMapping("data")
public class DataController {
@RequestMapping("test1")
public String test1() {
return "show";
}
//如果在功能中请求处理方法中定义了内置对象,那么可以在功能中直接获取到程序的内置对象
@RequestMapping("test2")
public String test2(HttpServletRequest req,HttpServletResponse resp) {
//使用servletAPI在功能请求处理方法接收客户端提交的参数
String name = req.getParameter("name");
String password = req.getParameter("pwd");
System.out.println(name+","+password);
return "show";
}
//还可以直接获取session
@RequestMapping("test3")
public String test3(HttpSession session) {
String name = (String) session.getAttribute("name");
return "show";
}
//表单对象,springmvc能够自动将请求参数绑定到功能处理方法的对象上
@RequestMapping("test4")
public String test4(User user) {
System.out.println(user.toString());
return "show";
}
/*
* Model 或者 ModelMap
* Model 和 ModelMap 的实例都是spirng mvc框架来自动创建并作为控制器方法参数传入,用户无需自己创建。
* 而且需要return 返回指定的页面路径
*/
//@RequestMapping("test5")
@GetMapping("test5")
public String test4(ModelMap mpdelMap) {
User user = new User("admin","123456");
mpdelMap.addAttribute("msg", user);
return "show";
}
}

参数赋值

1
2
3
4
5
6
7
8
public class User {
private String name;
private String password;
private List<Integer> ids;
private List<User> users;
private Map<String, String> map;
}

JSP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<ul> <li>非简单类型(对象)参数赋值
<form action="user/test2" method="post">
ID:<input name="id"/><br />
用户名:<input name="name"/><br />
<input value=" 登录 " type="submit" />
</form>
</li>
<li>数组参数赋值
<form action="user/test3">
id1:<input name="ids" value="1"/><br/>
id2:<input name="ids" value="2"/><br/>
id3:<input name="ids" value="3"/><br/>
<input value=" 登录 " type="submit" />
</form>
</li>
<li>list集合(简单类型泛型约束)赋值
<form action="user/test4" method="post">
id1:<input name="ids[0]" value="1"/><br/>
id2:<input name="ids[1]" value="2"/><br/>
id3:<input name="ids[2]" value="3"/><br/>
<input value=" 登录 " type="submit" />
</form>
</li>
<li>list集合(非简单类型泛型约束)赋值
<form action="user/test5" method="post">
user1:<input name="users[0].id" value="0"/>
<input name="users[0].name" value="name0"/><br/>
user2:<input name="users[1].id" value="1"/>
<input name="users[1].name" value="name1"/><br/>
user3:<input name="users[2].id" value="2"/>
<input name="users[2].name" value="name2"/><br/>
<input value=" 登录 " type="submit" />
</form>
</li>
<li>map集合赋值
<form action="user/test6" method="post">
1:<input name="map['k1']" value="v1"/><br/>
2:<input name="map['k2']" value="v2"/><br/>
3:<input name="map['k3']" value="v3"/><br/>
<input value=" 登录 " type="submit" />
</form>
</li>
</ul>

UserController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@RequestMapping("user")
public class UserController {
//@RequestMapping("/test2",)
@RequestMapping(value="/test2",method={RequestMethod.POST,RequestMethod.GET})
public ModelAndView test2(User user){
return null;
}
@RequestMapping("/test3")
public ModelAndView test3(Integer[] ids){
return null;
}
@RequestMapping("/test4")
public ModelAndView test4(User user){
return null;
}
@RequestMapping("/test5")
public ModelAndView test5(User user){
return null;
}
@RequestMapping("/test6")
public ModelAndView test6(User user){
return null;
}
}

Jstl依赖

<dependency> 
     <groupId>taglibs</groupId> 
     <artifactId>standard</artifactId> 
     <version>1.1.2</version> 
</dependency> 

<dependency> 
     <groupId>javax.servlet.jsp.jstl</groupId> 
     <artifactId>jstl</artifactId> 
      <version>1.2</version>‘
</dependency> 

附加工具


lombok 是一款可以不用在实体类上提供get set toString的依赖

maven

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.8</version>
    <scope>provided</scope>
</dependency>

例如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.xiaolan.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class TDept {
private Integer deptid;
private String dname;
public TDept() {
super();
}
public TDept(Integer deptid, String dname) {
super();
this.deptid = deptid;
this.dname = dname;
}
}
package com.xiaolan.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class TEmp {
private Integer empid;
private String ename;
private Integer egendar;
private TDept tdept;
public TEmp() {
super();
}
public TEmp(Integer empid, String ename, Integer egendar, TDept tdept) {
super();
this.empid = empid;
this.ename = ename;
this.egendar = egendar;
this.tdept = tdept;
}
}
文章目录
  1. 1. date: 2019/8/9 16:55:06
  • url 映射
    1. 1. 1.普通的 url 映射
    2. 2. 2.url 模板模式映射
    3. 3. 3.Ant 风格映射方式
    4. 4. 4.正则映射方式
    5. 5. 5.映射提交方式
  • 参数限制
  • 参数绑定
  • 参数赋值
    1. 1. JSP
  • 附加工具