springMVC和spring的集成

主要是2点

1.web.xml里 配置spring所需的listener

2. 新增applicationContext.xml, 配置注入的bean

3. 使用注解获取bean @Resource

步骤:

1. 新建web project

2. 添加jar包

3. 新增内容在web.xml

    主要是新增 <context-param>和listener

    web容器启动顺序:

    第一:context-param

    第二:Listerer

    第三:Filter

    第四:servlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:config/applicationContext.xml</param-value>
  </context-param>
  
  <!-- 配置spring启动listener入口 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 配置springMVC启动DisptcherServlet入口 -->
  <servlet>
  	<servlet-name>springMVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath*:config/spring-servlet.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <filter>  
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>
  
  
  <servlet-mapping>
  	<servlet-name>springMVC</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
 
</web-app>

4. 新增config包下的 spring-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:p="http://www.springframework.org/schema/p"    
    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-3.0.xsd     
           http://www.springframework.org/schema/context     
           http://www.springframework.org/schema/context/spring-context-3.0.xsd    
           http://www.springframework.org/schema/mvc     
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    <!-- 注解扫描包 -->
    <context:component-scan base-package="com.tgb.web.controller" />
   
      
    <!-- 开启注解 -->
    <mvc:annotation-driven/>
   
    <!-- 静态资源访问 -->
    <mvc:resources location="/img/" mapping="/img/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>
   
    	
    <!-- ViewResolver 视图解析器 --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/> 
        <property name="suffix" value=".jsp"/> 
    </bean>
    <!-- 上传文件 -->
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">   
          <property name="defaultEncoding"  value="UTF-8"/> 
          <property name="maxUploadSize"    value="1048576000"/> 
          <property name="maxInMemorySize"  value="40960"/> 
    </bean> 
     
</beans>

  

5. 新增applicationContext.xml,通过bean注入要调用的接口实现类 

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<bean id="springService" class="com.tgb.web.controller.SpringService"></bean>
</beans> 

6. 新建接口文件ISpring.java

package com.tgb.web.controller;

public interface ISpring {
	public String get();
}

7. ISpring实现类  SpringService.java 

package com.tgb.web.controller;

public class SpringService implements ISpring{

	@Override
	public String get() {
		System.out.println("-----------I am springService----");
		return "I am getMethod";
	}

}

8. SpringController.java

    注意注入 @Resource(name="springService")

 

package com.tgb.web.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SpringController {
	@Resource(name="springService")
	private ISpring springService;
	
	@RequestMapping("/spring/get")
	public String get(){
		System.out.println(springService.get());
		return "/success";
	}
}

9. IE端验证结果 http://localhost:8080/springMVCSpring/spring/get 

 

配置文件技巧, 在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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<import resource="classpath*:com/tgb/controller/spring-import.xml"/>
</beans> 

 

在com/tgb/controller/spring-import.xml里配置bean

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<bean id="springService" class="com.tgb.web.controller.SpringService"></bean>
</beans> 

  

 

  

  

  

posted @ 2016-06-22 10:40  wujixing909  阅读(242)  评论(0编辑  收藏  举报