博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC_入门项目
阅读量:6945 次
发布时间:2019-06-27

本文共 5160 字,大约阅读时间需要 17 分钟。

    本项目是SpringMVC的入门项目,用于演示SpringMVC的项目配置、各层结构,功能较简单
一、Eclipse中创建maven项目

二、pom.xml添加依赖

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
<
dependency
>
    
<
groupId
>javax.servlet</
groupId
>
    
<
artifactId
>javax.servlet-api</
artifactId
>
    
<
version
>3.1.0</
version
>
</
dependency
>
<
dependency
>
    
<
groupId
>javax.servlet.jsp</
groupId
>
    
<
artifactId
>jsp-api</
artifactId
>
    
<
version
>2.2</
version
>
</
dependency
>
 
<!--① 依赖的Spring模块类库 -->
<
dependency
>
    
<
groupId
>org.springframework</
groupId
>
    
<
artifactId
>spring-context</
artifactId
>
    
<
version
>3.1.1.RELEASE</
version
>
</
dependency
>
<
dependency
>
    
<
groupId
>org.springframework</
groupId
>
    
<
artifactId
>spring-beans</
artifactId
>
    
<
version
>3.1.1.RELEASE</
version
>
</
dependency
>
<
dependency
>
    
<
groupId
>org.springframework</
groupId
>
    
<
artifactId
>spring-web</
artifactId
>
    
<
version
>3.2.3.RELEASE</
version
>
</
dependency
>
<
dependency
>
    
<
groupId
>org.springframework</
groupId
>
    
<
artifactId
>spring-webmvc</
artifactId
>
    
<
version
>3.2.3.RELEASE</
version
>
</
dependency
>

三、web.config配置文件

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
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"2.4"
xmlns
=
""
    
xmlns:xsi
=
""
    
xsi:schemaLocation="
    
">
    
<
display-name
>SpringMVC</
display-name
>
     
    
<!-- Spring应用上下文, 理解层次化的ApplicationContext -->
    
<
context-param
>
        
<
param-name
>contextConfigLocation</
param-name
>
        
<
param-value
>/WEB-INF/applicationContext.xml</
param-value
>
    
</
context-param
>
    
<
listener
>
        
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
    
</
listener
>
     
     
    
<!-- DispatcherServlet, Spring MVC的核心 -->
    
<
servlet
>
        
<
servlet-name
>mvc-dispatcher</
servlet-name
>
        
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
        
<!-- DispatcherServlet对应的上下文配置, 默认为/WEB-INF/$servlet-name$-servlet.xml -->
        
<
init-param
>
            
<
param-name
>contextConfigLocation</
param-name
>
            
<
param-value
>/WEB-INF/mvc-dispatcher-servlet.xml</
param-value
>
        
</
init-param
>
        
<
load-on-startup
>1</
load-on-startup
>
    
</
servlet
>
    
<
servlet-mapping
>
        
<
servlet-name
>mvc-dispatcher</
servlet-name
>
        
<!-- mvc-dispatcher拦截所有的请求 -->
        
<
url-pattern
>/</
url-pattern
>
    
</
servlet-mapping
>
</
web-app
>

四、添加applicationContext.xml配置文件 

        文件路径对应web.xml中<context-param><param-value>节点值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
""
    
xmlns:xsi
=
""
xmlns:p
=
""
    
xmlns:context
=
""
    
xmlns:aop
=
""
xmlns:tx
=
""
    
xsi:schemaLocation="
       
       
       
       
       
       
       
">
     
    
<!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
    
<
context:component-scan
base-package
=
"com.james"
/>
</
beans
>

五、添加mvc-dispatcher-servlet.xml配置文件

        文件路径对应web.xml中<servlet><init-param><param-value>节点值

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
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
""
    
xmlns:xsi
=
""
xmlns:context
=
""
    
xmlns:mvc
=
""
    
xsi:schemaLocation="
        
        
        
        
        
        
">
    
<
context:component-scan
base-package
=
"com.james"
>
        
<
context:include-filter
type
=
"annotation"
            
expression
=
"org.springframework.stereotype.Controller"
/>
    
</
context:component-scan
>
 
    
<!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
    
<
mvc:annotation-driven
/>
 
    
<!-- 静态资源处理, css, js, imgs -->
    
<
mvc:resources
mapping
=
"/resources/**"
location
=
"/resources/"
/>
 
    
<!-- /WEB-INF/views/ 对应jsp文件路径 -->
    
<
bean
        
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
        
<
property
name
=
"viewClass"
            
value
=
"org.springframework.web.servlet.view.JstlView"
/>
        
<
property
name
=
"prefix"
value
=
"/WEB-INF/views/"
/>
        
<
property
name
=
"suffix"
value
=
".jsp"
/>
    
</
bean
>
</
beans
>

六、添加web、service、dao层及类

        1、HelloController.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
package
com.web.controller;
 
import
javax.servlet.http.HttpSession;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
com.james.service.HelloService;
 
@Controller
@RequestMapping
(
"/hello"
)
public
class
HelloController {
 
    
@Autowired
    
private
HelloService service;
     
    
@RequestMapping
(
"/mvc"
)
    
public
String helloMvc(HttpSession httpSession) {
 
        
System.out.println(
"进入:HelloController-->helloMvc"
);
        
this
.service.helloMvc();
         
        
// 视图渲染,/WEB-INF/views/home.jsp
        
return
"home"
;
    
}
}

        2、HelloService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package
com.service;
 
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Service;
 
import
com.dao.HelloDao;
 
@Service
public
class
HelloService {
     
    
@Autowired
    
private
HelloDao dao;
     
    
public
void
helloMvc() {
        
System.out.println(
"进入:HelloService-->helloMvc"
);
         
        
this
.dao.helloMvc();
    
}
}

        3、HelloDao.java

1
2
3
4
5
6
7
8
9
10
11
12
package
com.dao;
 
import
org.springframework.stereotype.Repository;
 
@Repository
public
class
HelloDao {
    
public
void
helloMvc() {
        
System.out.println(
"进入:HelloDao-->helloMvc"
);
         
        
System.out.println(
"进入:HelloDao-->helloMvc2"
);
    
}
}

七、利用jetty启动mvc并查看输出

35158-20160219184128738-1630287604.png

附件列表

 

转载于:https://www.cnblogs.com/gossip/p/5201938.html

你可能感兴趣的文章
Dell U2913WM使用感受
查看>>
关于document.createDocumentFragment()(转)
查看>>
3款新鲜的CSS3&amp;HTML5框架
查看>>
【评论】GNU/Linux下有多少是GNU的?
查看>>
NoSQL非关系型数据库
查看>>
C++函数
查看>>
sql 2005 清除日志
查看>>
netbeans 快捷键
查看>>
C#事件-什么是事件
查看>>
微软职位内部推荐-Software Engineer II
查看>>
香农定律和奈奎斯特准则
查看>>
每秒处理3百万请求的Web集群搭建-用 LVS 搭建一个负载均衡集群
查看>>
js toggle事件
查看>>
WebViewJavascriptBridge
查看>>
js 返回并刷新
查看>>
append()与extend()
查看>>
ASPxGridview使用总结(DEVExpress)
查看>>
.NET设计模式(9):桥接模式(Bridge Pattern)
查看>>
极速理解设计模式系列:16.迭代器模式(Iterator Pattern)
查看>>
50个带给你灵感的基于文字的创新Logo设计 - 第一部分
查看>>