整合SpringMVC框架和Spring框架

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

 

publicvoid setUserService(UserService userService) {

this.userService = userService;

}

 

/**

* 注意:这里没有返回值,仅仅返回了一个 HTTP 状态码 200

*/

@RequestMapping("/show")

@ResponseStatus(HttpStatus.OK)

publicvoid 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:

  

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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 对象 -->

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

  

  

</beans>

  

  

  

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

  

dispatcher-servlet.xml:

  

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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" />

-->

<beanid="userController"class="com.siwuxie095.controller.UserController">

<propertyname="userService"ref="userService"/>

</bean>

 

 

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

<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!--

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

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

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

 

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

-->

<propertyname="prefix"value="/"/>

<propertyname="suffix"value=".jsp"/>

</bean>

 

 

</beans>

  

  

  

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

  

web.xml:

  

<?xmlversion="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】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值