服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 三步轻松搭建springMVC框架

三步轻松搭建springMVC框架

2020-12-14 13:04~驰~ Java教程

这篇文章主要教大家三步轻松搭建springMVC框架,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

一、搭建步骤

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;
}
}

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

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

延伸 · 阅读

精彩推荐