三步轻松搭建springMVC框架

2025-05-29 0 78

一、搭建步骤

1、导入jar包、创建项目包结构

2、在web.xml中配置前端控制器

3、编写springmvc核心配置文件

4、编写pojo类和controller类测试

二、实现

1、导入jar包、创建项目包结构

三步轻松搭建springMVC框架

三步轻松搭建springMVC框架

2、在web.xml中配置前端控制器

?

1
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
<!-- springmvc前端控制器 -->

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>

<!-- 指定springmvc核心配置文件位置

如果没有指定那么默认就会去"/web-inf/+ <servlet-name>标签中内容 + -servlet.xml"中找

例如:"/web-inf/springmvc-servlet.xml"

-->

<init-param>

<param-name>contextconfiglocation</param-name>

<param-value>classpath:springmvc.xml</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>

3、编写springmvc核心配置文件

?

1
2

3

4

5

6

7

8

9

10

11

12

13

14
<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

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

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

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

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

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

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

<!-- 配置@controller注解扫描 -->

<context:component-scan base-package="cn.it.controller"></context:component-scan>

</beans>

4、编写pojo类和controller类测试

pojo类代码:

?

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
package cn.it.pojo;

import java.util.date;

public class items {

private integer id;

private string name;

private float price;

private string pic;

private date createtime;

private string detail;

public integer getid() {

return id;

}

public void setid(integer id) {

this.id = id;

}

public string getname() {

return name;

}

public void setname(string name) {

this.name = name == null ? null : name.trim();

}

public float getprice() {

return price;

}

public void setprice(float price) {

this.price = price;

}

public string getpic() {

return pic;

}

public void setpic(string pic) {

this.pic = pic == null ? null : pic.trim();

}

public date getcreatetime() {

return createtime;

}

public void setcreatetime(date createtime) {

this.createtime = createtime;

}

public string getdetail() {

return detail;

}

public void setdetail(string detail) {

this.detail = detail == null ? null : detail.trim();

}

}

controller类代码:

?

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
package cn.it.controller;

import java.util.arraylist;

import java.util.list;

import org.springframework.stereotype.controller;

import org.springframework.web.bind.annotation.requestmapping;

import org.springframework.web.servlet.modelandview;

import cn.it.pojo.items;

@controller

public class itemscontroller {

//@requestmapping指定url到请求方法的映射,例如:

@requestmapping("/itemslist")

public modelandview itemslist(){

list<items>itemlist = new arraylist<items>();

//商品列表

items items_1 = new items();

items_1.setname("联想笔记本_3");

items_1.setprice(6000f);

items_1.setdetail("thinkpad t430 联想笔记本电脑!");

items items_2 = new items();

items_2.setname("苹果手机");

items_2.setprice(5000f);

items_2.setdetail("iphone6苹果手机!");

itemlist.add(items_1);

itemlist.add(items_2);

/*

* 模型和视图:

* model模型:模型对象中存放了返回给页面的数据

* view视图:视图对象中指定了返回的页面的位置

*/

//创建modelandview对象

modelandview modelandview = new modelandview();

modelandview.addobject("itemlist", itemlist);

modelandview.setviewname("/web-inf/jsp/itemlist.jsp");

return modelandview;

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/zhaochi/archive/2017/08/10/7338837.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 三步轻松搭建springMVC框架 https://www.kuaiidc.com/115495.html

相关文章

发表评论
暂无评论