3.31_myEclipse spring&SpringMVC集成

题首:当你对某项技术熟练的时候。你才会逻辑清晰的表述出来(ps。现在还只是入门阶段。demo版)

一.首先是jar包。搞好spring的就ok

二.随后是搞清楚几点

  2.1 spring与springMVC的集成,相当于是无缝集成,只要拿到spring需要的jar包就可以用来集成            springMVC。

  2.2 spring是通过listener来加载进web容器的,而springMVC是通过<servlet>加载进来的。

  2.3 在顺序上,容器加载的顺序为:context---》listener----》filter----》servlet

三.集成步骤

  3.1 从web.xml文件配置。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 3   <display-name>springMVC1</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   
13   <context-param>
14       <param-name>contextConfigLocation</param-name>
15       <param-value>classpath*:config/springAnnotation-servlet.xml,classpath*:config/springAnnotation-core.xml
16       </param-value>
17   </context-param>
18   
19   <listener>
20        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
21   </listener>
22   
23   
24   <servlet>
25       <servlet-name>springMVC</servlet-name>
26       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
27           <init-param>
28               <param-name>contextConfigLocation</param-name>
29               <param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
30           </init-param> 
31       <load-on-startup>1</load-on-startup>
32   </servlet>
33   
34   <servlet-mapping>
35       <servlet-name>springMVC</servlet-name>
36       <url-pattern>/</url-pattern>
37   </servlet-mapping>
38 </web-app>
web.xml

  3.2 spring配置:springAnnotation-core.xml。。。。其中context-param我还是没懂是干啥的。如果不放这个的话ServletContext的默认文件是在WEB-INF下的applicationContext,而我这里指向的xml文件为springAnnotation-core.xml...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/context/WEB-INF/contextInclude.xml">
]>

<beans>
	<bean id="springManager" class="com.tgb.web.controller.annotation.SpringManager"></bean>
</beans>

  3.3  springMVC配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3  xmlns:context="http://www.springframework.org/schema/context"  
 4  xmlns:p="http://www.springframework.org/schema/p"  
 5  xmlns:mvc="http://www.springframework.org/schema/mvc"  
 6  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 7  xsi:schemaLocation="http://www.springframework.org/schema/beans  
 8       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 9       http://www.springframework.org/schema/context  
10       http://www.springframework.org/schema/context/spring-context.xsd  
11       http://www.springframework.org/schema/mvc  
12       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
13      <!-- 注解扫描包 -->
14     <context:component-scan base-package="com.tgb.web.controller.annotation" />
15     <!-- 开启注解 -->
16     <mvc:annotation-driven/>
17     
18     <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
19     <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
20     <!-- 静态资源访问 -->
21      <mvc:resources location="/img/" mapping="/img/**"/>  
22      <mvc:resources location="/js/" mapping="/js/**"/>   
23 
24     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
25         <property name="prefix" value="/"></property>
26         <property name="suffix" value=".jsp"></property>
27     </bean>
28  </beans>  

配置暂时告一段落。下面开始编码,附源码。。

package com.tgb.web.controller.annotation;

public interface ISpring {
    public String get();
}
 1 package com.tgb.web.controller.annotation;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 
 8 @Controller
 9 public class SpringController {
10 
11     @Resource(name="springManager")
12     private ISpring springManger;
13     
14     @RequestMapping("/spring/get")
15     public String get(){
16         System.out.println(springManger.get());
17         
18         return "/success";
19     }
20     
21 }
 1 package com.tgb.web.controller.annotation;
 2 
 3 public class SpringManager implements ISpring {
 4 
 5     public String get() {
 6         System.out.println("this is springManager print out!");
 7         return "i am SpringManager";
 8     }
 9 
10 
11 }

暂时告一段落

原文地址:https://www.cnblogs.com/weizizhe/p/3636060.html