-------------------------siwuxie095

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

整合 SpringMVC 框架和 Spring 框架

 
 

 
 

1、导入相关
jar 包(共 17 个)

 
 

(1)导入
Spring 的核心 jar 包和日志相关的 jar 包(6 个)

 
 

 
 

 
 

Commons Logging
下载链接:

 
 

http://commons.apache.org/proper/commons-logging/download_logging.cgi

 
 

 
 

LOG4J 下载链接:

 
 

https://www.apache.org/dist/logging/log4j/

 
 

 
 

 
 

(2)导入
Spring 的 AOP 开发的 jar 包(4 个)

 
 

 
 

 
 

AOP Alliance
下载链接:

 
 

http://mvnrepository.com/artifact/aopalliance/aopalliance

 
 

 
 

AspectJ Weaver
下载链接:

 
 

http://mvnrepository.com/artifact/org.aspectj/aspectjweaver

 
 

 
 

 
 

(3)导入
Spring 的
JDBC 开发的 jar 包(2 个)

 
 

 
 

 
 

 
 

(4)导入
Spring 整合 Web 项目的 jar 包(1 个)

 
 

 
 

 
 

 
 

(5)导入
SpringMVC 的 jar 包(1 个)

 
 

 
 

 
 

 
 

(6)导入
SpringMVC 中使用 JSON 所需的 jar 包(3 个)

 
 

 
 

 
 

Jackson Core 下载链接:

 
 

http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/

 
 

 
 

Jackson Annotations 下载链接:

 
 

http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/

 
 

 
 

Jackson Databind 下载链接:

 
 

http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/

 
 

 
 

 
 

 

 

2、测试

 
 

(1)编写一个
Controller 类

 
 

UserController.java:

 
 

package com.siwuxie095.controller;

 
 

import org.springframework.http.HttpStatus;

import org.springframework.stereotype.Controller;

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

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

 
 

import com.siwuxie095.service.UserService;

 
 

// Controller 类

@Controller

@RequestMapping("/user")

public class UserController {

 

private UserService userService;

 

public
void setUserService(UserService userService) {

this.userService = userService;

}

 

/**

*/

@RequestMapping("/show")

@ResponseStatus(HttpStatus.OK)

public
void show() {

 

System.out.println("UserController 调用了 " + userService.show());

 

}

 

}

 
 

 
 

 
 

(2)编写一个
Service 类

 
 

UserService.java:

 
 

package com.siwuxie095.service;

 
 

// Service 类

public class UserService {

 
 

public String show(){

return
"UserService";

}

 

}

 
 

 
 

 
 

(3)在
Spring 核心配置文件中进行配置

 
 

applicationContext.xml:

 
 

<?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:aop="http://www.springframework.org/schema/aop"

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

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

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">

 

 

<!-- 配置 Service 对象 -->

<bean
id="userService"
class="com.siwuxie095.service.UserService"/>

 
 

 
 

</beans>

 
 

 
 

 
 

(4)在
SpringMVC 核心配置文件中进行配置

 
 

dispatcher-servlet.xml:

 
 

<?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:context="http://www.springframework.org/schema/context"

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

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

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

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

 

 

<!-- 启用注解驱动 -->

<mvc:annotation-driven/>

 

 

<!--

配置 Controller(必须,即
必须进行配置)

 

class 为自定义 Controller 类的完全限定名,这里通过 Controller

类中的 @RequestMapping 注解来设置访问路径

 

使用纯注解时,另一种配置 Controller 的方式:配置扫描包

<context:component-scan base-package="com.siwuxie095.controller" />

-->

<bean
id="userController"
class="com.siwuxie095.controller.UserController">

<property
name="userService"
ref="userService"/>

</bean>

 

 

<!-- 配置 ViewResolver(必须,即
必须进行配置) -->

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!--

配置视图解析的前缀 prefix 和后缀 suffix:

(1)前缀:如果在 WebContent 目录下,则为 /,如果在 WEB-INF 目录下,则为 /WEB-INF/

(2)后缀:一般为 JSP 文件,所以为 .jsp

 

例如:prefix="/",suffix=".jsp",viewname="test",则:"/test.jsp"

-->

<property
name="prefix"
value="/"/>

<property
name="suffix"
value=".jsp"/>

</bean>

 

 

</beans>

 
 

 
 

 
 

(5)在部署描述文件中进行配置

 
 

web.xml:

 
 

<?xml
version="1.0"
encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<display-name>TestSpringMVCAndSpring</display-name>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

 

 

<!-- 配置 Spring 的监听器 ContextLoaderListener -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

 

<!-- 配置 Spring 核心配置文件的位置(路径) -->

<context-param>

<param-name>contextConfigLocation</param-name>

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

</context-param>

 

 

<!-- 配置 SpringMVC 的核心分发器 -->

<servlet>

<servlet-name>dispatcher</servlet-name>

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

<!-- 配置 SpringMVC 核心配置文件的位置(路径) -->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:dispatcher-servlet.xml</param-value>

</init-param>

<!-- 自动加载:随 Tomcat 容器启动,完成初始化 -->

<load-on-startup>1</load-on-startup>

</servlet>

 

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

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

</servlet-mapping>

 

 

</web-app>

 
 

 
 

 
 

(6)访问路径

 
 

http://localhost:8080/工程名/user/show.do

 
 

 
 

 
 

 
 

 
 

附:

 
 

另一种配置方式,即
纯注解,对以上「测试」做如下修改:

 
 

(1)在
Controller 类中:

 
 

1)在 userService 属性上加 @Autowired 注解

 
 

2)删掉
userService 属性的 setter 方法

 
 

 
 

(2)在
Service 类中:

 
 

在类上加 @Service 注解

 
 

 
 

(3)在
Spring 核心配置文件中:

 
 

1)删掉 userService 的 Bean

 
 

2)加上
<context:component-scan base-package="com.siwuxie095.service"/>

 
 

 
 

(4) 在 SpringMVC 核心配置文件中:

 
 

1)删掉 userController 的 Bean

 
 

2)加上
<context:component-scan base-package="com.siwuxie095.controller"/>

 
 

 
 

或
综合(3)(4):删掉两个
Bean,在 SpringMVC 核心配置文件中

加上如下内容:

 
 

<context:component-scan base-package="com.siwuxie095"/>

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

【made by siwuxie095】

整合SpringMVC框架和Spring框架的更多相关文章

  1. 整合Struts2框架和Spring框架

    -----------------------siwuxie095                                 整合 Struts2 框架和 Spring 框架         1 ...

  2. SpringMVC学习指南-Spring框架

    Spring框架主要使用依赖注入.实际上,很多牛叉的框架如Google的Guice都是使用依赖注入. ------------------------------------------------- ...

  3. Eclipse/JavaWeb (三)三大框架之Spring框架 持续更新中...

    (一)发展历史 现在我们有三个层了,可是每层之间的调用是怎样的呢?比如显示层的struts需要调用一个业务类,就需要new一个业务类出来,然后使用:业务层需要调用持久层的类,也需要new一个持久层类出 ...

  4. 什么是Spring框架? Spring框架有哪些主要的模块?

    Spring框架是一个为java应用程序的开发提供了综合,广泛的基础性支持的java平台.Spring帮助开发者解决了开发中基础性的问题,使得开发人员可以专注于应用程序的开发.Spring框架本身亦是 ...

  5. Java - Struts框架教程 Hibernate框架教程 Spring框架入门教程(新版) sping mvc spring boot spring cloud Mybatis

    https://www.zhihu.com/question/21142149 http://how2j.cn/k/hibernate/hibernate-tutorial/31.html?tid=6 ...

  6. Java框架:spring框架整合hibernate框架的xml配置(使用注解的方式)

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. 零基础学习java------40---------Maven(maven的概念,安装,maven在eclipse中使用),springboot(spring整合springmvc(注解),spring整合mybatis(常见的配置文件)),前端页面(bootstrap软件)

    一 maven 1. Maven的相关概念 1.1 项目开发中遇到的问题 (1)都是同样的代码,为什么在我的机器上可以编译执行,而在他的机器上就不行? (2)为什么在我的机器上可以正常打包,而配置管理 ...

  8. Java框架之spring框架的优点,为什么要学习spring框架

    为什么要学习Spring的框架a: 方便解耦,简化开发    Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理 b:AOP编程的支持      Spring提供面向切 ...

  9. Spring框架学习(5)spring整合struts2

    内容源自:spring整合struts2 一.spring框架对struts等表现层框架的整合原理 : 使用spring的ioc容器管理struts中用于处理请求的Action 将Action配置成i ...

随机推荐

  1. ionic 前端接收到后台推送来的消息后,显示在手机的通知栏上

    这里主要用到cordova提供的插件:(在app没有关闭的情况下只要有推送的消息就会有提醒,但是在app关闭的情况下就不会提示) 首先安装cordova-plugin-local-notificati ...

  2. websocket 11

    1. websocket 回顾: - 什么是轮训? - 通过定时器让程序每隔n秒执行一次操作. - 什么是长轮训? - 浏览器向后端发起请求,后端会将请求 hang 住,最多hang 30s. 如果一 ...

  3. 建筑的永恒之道 (C·亚历山大 著)

    永恒之道 建筑或城市只有踏上了永恒之道,才会生机勃勃. 第1章 永恒之道 它是一个唯有我们自己才能带秩序的过程,它不可能被求取,但只要我们顺应它,它便会自然而然地出现. 质 为了探求永恒之道,我们首先 ...

  4. paramiko实现上传目录

    使用paramiko上传目录,重点是上传的那个过程,想了一上午才想出来,可能有点奇葩,但是还是实现了这个功能 #!/usr/bin/python import paramiko import os d ...

  5. Day 13 可迭代对象,迭代器对象,for循环迭代,生成器对象,枚举对象

    一.迭代器概念:# 器:包含了多个值的容器# 迭代:循环反馈(一次从容器中取出一个值)# 迭代器:从装有多个值的容器中一次取出一个值给外界# ls = 'abcdef'ls = [1, 2, 3, 4 ...

  6. DDR3初识

    DDR3初识 选择2:1 ratio 意味用户总线宽度为DDR物理数据接口宽度的4倍.

  7. python3.6 内置函数

    python内置函数 # encoding: utf-8 # module builtins # from (built-in) # by generator 1.145 ""&q ...

  8. Xamarin+Prism开发详解七:Plugin开发与打包测试

    有了上章[Xamarin+Prism开发详解六:DependencyService与IPlatformInitializer的关系]的基础,现在来理解Plugin开发就简单了. 本文实例代码地址:ht ...

  9. jenkines的工作区目录位置查找

    先找到jenkines的主目录 系统-系统配置 然后工作区在主目录的workspace文件夹里面

  10. 注解:大话AOP与Android的爱恨情仇

    转载:大话AOP与Android的爱恨情仇 1. AOP与OOP的区别 平时我接触多的就是OOP(Object Oriented Programming面向对象).AOP(Aspect Oriente ...