springMvc整合spring

简介:本文技术要点springMvc整合spring开发问题

1、项目清单

2、项目顺序源码

[html]  view plain  copy
  1. package com.atguigu.springmvc;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6.   
  7. @Controller  
  8. public class HelloWorld {  
  9.   
  10.     @Autowired  
  11.     private UserService userService;  
  12.       
  13.     public HelloWorld() {  
  14.         System.out.println("HelloWorld Constructor...");  
  15.     }  
  16.       
  17.     @RequestMapping("/helloworld")  
  18.     public String hello(){  
  19.         System.out.println("success");  
  20.         System.out.println(userService);  
  21.         return "success";  
  22.     }  
  23.       
  24. }  
[html]  view plain  copy
  1. package com.atguigu.springmvc;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Service;  
  5.   
  6. @Service  
  7. public class UserService {  
  8.   
  9.     @Autowired  
  10.     private HelloWorld helloWorld;  
  11.       
  12.     public UserService() {  
  13.         System.out.println("UserService Constructor...");  
  14.     }  
  15.       
  16. }  

 解析:

SpringMVC 的 IOC 容器中的 bean 可以来引用 Spring IOC 容器中的 bean. 
返回来呢 ? 反之则不行. Spring IOC 容器中的 bean 却不能来引用 SpringMVC IOC 容器中的 bean!

3、springMvc.xml配置讲解

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  8.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">  
  9.   
  10.     <!--    
  11.         需要进行 Spring 整合 SpringMVC 吗 ?  
  12.         还是否需要再加入 Spring 的 IOC 容器 ?  
  13.         是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 ContextLoaderListener ?  
  14.           
  15.         1. 需要: 通常情况下, 类似于数据源, 事务, 整合其他框架都是放在 Spring 的配置文件中(而不是放在 SpringMVC 的配置文件中).  
  16.         实际上放入 Spring 配置文件对应的 IOC 容器中的还有 Service 和 Dao.   
  17.         2. 不需要: 都放在 SpringMVC 的配置文件中. 也可以分多个 Spring 的配置文件, 然后使用 import 节点导入其他的配置文件  
  18.     -->  
  19.       
  20.     <!--    
  21.         问题: 若 Spring 的 IOC 容器和 SpringMVC 的 IOC 容器扫描的包有重合的部分, 就会导致有的 bean 会被创建 2 次.  
  22.         解决:  
  23.         1. 使 Spring 的 IOC 容器扫描的包和 SpringMVC 的 IOC 容器扫描的包没有重合的部分.   
  24.         2. 使用 exclude-filter 和 include-filter 子节点来规定只能扫描的注解  
  25.     -->  
  26.       
  27.     <!--    
  28.         SpringMVC 的 IOC 容器中的 bean 可以来引用 Spring IOC 容器中的 bean.   
  29.         返回来呢 ? 反之则不行. Spring IOC 容器中的 bean 却不能来引用 SpringMVC IOC 容器中的 bean!  
  30.     -->  
  31.       
  32.     <context:component-scan base-package="com.atguigu.springmvc" use-default-filters="false">  
  33.         <context:include-filter type="annotation"   
  34.             expression="org.springframework.stereotype.Controller"/>  
  35.         <context:include-filter type="annotation"   
  36.             expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  37.     </context:component-scan>  
  38.   
  39.     <!-- 配置视图解析器 -->  
  40.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  41.         <property name="prefix" value="/WEB-INF/views/"></property>  
  42.         <property name="suffix" value=".jsp"></property>  
  43.     </bean>  
  44.       
  45.     <mvc:default-servlet-handler/>  
  46.     <mvc:annotation-driven></mvc:annotation-driven>  
  47.       
  48. </beans>  

 

4、spring.xml配置讲解

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
  7.   
  8.     <context:component-scan base-package="com.atguigu.springmvc">  
  9.         <context:exclude-filter type="annotation"   
  10.             expression="org.springframework.stereotype.Controller"/>  
  11.         <context:exclude-filter type="annotation"   
  12.             expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  13.     </context:component-scan>  
  14.   
  15.     <!-- 配置数据源, 整合其他框架, 事务等. -->  
  16.   
  17. </beans>  

 

5、web.xml配置讲解

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">  
  6.       
  7.     <!-- 配置启动 Spring IOC 容器的 Listener -->  
  8.     <!-- needed for ContextLoaderListener -->  
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>classpath:beans.xml</param-value>  
  12.     </context-param>  
  13.   
  14.     <!-- Bootstraps the root web application context before servlet initialization -->  
  15.     <listener>  
  16.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  17.     </listener>  
  18.       
  19.     <servlet>  
  20.         <servlet-name>springDispatcherServlet</servlet-name>  
  21.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  22.         <init-param>  
  23.             <param-name>contextConfigLocation</param-name>  
  24.             <param-value>classpath:springmvc.xml</param-value>  
  25.         </init-param>  
  26.         <load-on-startup>1</load-on-startup>  
  27.     </servlet>  
  28.   
  29.     <servlet-mapping>  
  30.         <servlet-name>springDispatcherServlet</servlet-name>  
  31.         <url-pattern>/</url-pattern>  
  32.     </servlet-mapping>  
  33.       
  34. </web-app>  

6、顺序jsp页面写法(success.jsp和abc.html没什么意义)

[html]  view plain  copy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.   
  11.     <a href="helloworld">Hello World</a>  
  12.   
  13. </body>  
  14. </html>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于Spring MVC与Spring的集成,您可以按照以下步骤进行操作: 1. 首先,确保您的项目中已经引入了SpringSpring MVC的相关依赖。 2. 在Spring的配置文件中(一般为applicationContext.xml),配置Spring MVC的相关bean,例如DispatcherServlet、HandlerMapping、ViewResolver等。可以使用以下示例作为参考: ```xml <!-- 配置DispatcherServlet --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 映射DispatcherServlet的URL --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置HandlerMapping --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> <!-- 配置ViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> ``` 3. 创建Spring MVC的配置文件(一般为springmvc-servlet.xml),在该文件中配置组件扫描、视图解析器等。例如: ```xml <!-- 开启组件扫描 --> <context:component-scan base-package="com.example.controller"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> ``` 4. 创建Controller类,在该类中编写处理请求的方法。例如: ```java @Controller public class HelloWorldController { @RequestMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello, Spring MVC!"); return "hello"; } } ``` 5. 创建JSP视图文件(例如hello.jsp),用于显示处理结果。 通过以上步骤,您就可以实现Spring MVC与Spring整合。当请求`/hello`时,将会调用`HelloWorldController`的`hello`方法,并返回`hello.jsp`视图进行显示。 希望以上信息对您有所帮助!如有更多问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值