java mvc原理图_SpringMVC工作原理(含案例图解)

SpimgMVC工作原理

第1步:浏览器发送指定的请求都会交给DispatcherServlet,他会委托其他模块进行真正的业务和数据处理

第2步:DispatcherServlet会查找到HandleMapping,根据浏览器的请求找到对应的Controller,并将请求交给目标Controller

第3步:目标Controller处理完业务后,返回一个ModelAndView给DispatcherServlet

第4步:DispatcherServlet通过ViewResolver视图解析器找到对应的视图对象View

第5步:视图对象View负责渲染,并返回到浏览器

案例分析

下面通过案例图解的方式理解下上面的工作原理

第1步-浏览器请求

4fc01b9623b26cbe63553b5c066fea92.png

第2、3步-找到对应Controller

4f875f480e269dca4180d21324ad1f5f.png

第4、5步-解析视图对象,返回浏览器

f315b29c0ff4b2899d5ee0762d2b9c6e.png

浏览器结果

31790e6bceaa291c1fad56cbd08dad4d.png

源码

web.xml

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

version="2.5">

springmvc

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

springmvc

org.springframework.web.servlet.DispatcherServlet

1

springmvc

*.do

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

springmvc-servlet.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

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/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

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

UserControll类

@Controller

@RequestMapping("user")

public class UserController {

@RequestMapping("findAllUsers")

public ModelAndView findAllUsers() {

ModelAndView mv = new ModelAndView();

ArrayList users = new ArrayList();

for (int i = 0; i < 5; i++) {

User user = new User();

user.setUsername("zs" + i);

user.setAge(20 + i);

user.setIncome(16000.0+i*100);

user.setIsMarry(false);

user.setHobby(new String[] { "篮球"+i, "足球"+i });

users.add(user);

}

mv.addObject("users", users);

mv.setViewName("users");

return mv;

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

实体类

public class User implements Serializable {

/**

*

*/

private static final long serialVersionUID = 1L;

private String username;

private Integer age;

private Boolean isMarry;

private Double income;

private String[] hobby;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public Boolean getIsMarry() {

return isMarry;

}

public void setIsMarry(Boolean isMarry) {

this.isMarry = isMarry;

}

public Double getIncome() {

return income;

}

public void setIncome(Double income) {

this.income = income;

}

public String[] getHobby() {

return hobby;

}

public void setHobby(String[] hobby) {

this.hobby = hobby;

}

@Override

public String toString() {

return "User [username=" + username + ", age=" + age + ", isMarry=" + isMarry + ", income=" + income

+ ", hobby=" + Arrays.toString(hobby) + "]";

}

}

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

53

54

55

56

57

58

59

60

61

62

JSP页面

pageEncoding="UTF-8"%>

Insert title here
用户名年龄收入婚姻状态兴趣爱好
${user.username}${user.age}${user.income}

已婚

未婚

${hobby}

,

原文:https://blog.csdn.net/u010452388/article/details/80930609

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值