spring + springMVC集成


感叹下,不知道是老了还是怎么了,学了的东西,不过多久就忘了,要么就是没有完全掌握啊。

进入正题,今天搞定下怎么集成spring、springMVC。

1. jar包挺简单的,把所有的spring框架的jar包全放到lib目录下就OK了。spring 、 springMVC是一家的,挺方便的。


2.关键是配置文件:

(1) web.xml文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- spring配置文件的路径 -->
  <context-param><!-- 最先启动 -->
  			<param-name>contextConfigLocation</param-name>
  			<param-value>classpath*:config/applicationContext.xml</param-value>
  </context-param>
  
  <listener> <!--加载spring配置文件  -->
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  
  
  <!-- 配置springMVC -->
  <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/springMVC-servlet.xml</param-value> <!--springMVC-servlet.xml文件位置 -->
  		</init-param>
  		
  		<load-on-startup>1</load-on-startup>  <!-- 设置启动顺序,1为最高 -->
  </servlet>
  
  <servlet-mapping>
  		<servlet-name>springMVC</servlet-name>
  		<url-pattern>/</url-pattern>  <!-- 配置成/*出错 -->
  </servlet-mapping>
  
  
 
  <!-- 编码相关 -->
  <filter>  
    <filter-name>encodingFilter</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>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
  </filter-mapping>  
  
</web-app>
注:启动顺序是:

<context-param/>

<listener/>

       <filter/>

       <servlet/>

(2) springMVC-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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	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  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/" />  <!--   <property name="prefix" value="/WEB-INF/jsp/" /> 更安全-->
			<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 注解扫描包 -->
	<context:component-scan base-package="com.study.controller.annotation"></context:component-scan>
	<!-- 开启注解 -->
	<mvc:annotation-driven/>  <!-- 3.0版本后才有 --> 
	
	
	<!-- 静态资源,表明不会经过过滤器 -->
	<mvc:resources location="/img/" mapping="/img/**"/> <!-- 对img文件夹下的所有文件 -->
	<mvc:resources location="/js/" mapping="/js/**"/> <!-- 对img文件夹下的所有文件 -->
	
  	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
        <property name="cacheSeconds" value="0" />  
        <property name="messageConverters">  
            <list>  
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>  
            </list>  
        </property>
    </bean>  
    
    
    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 --> 
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
        <!-- 指定所上传文件的总大小不能超过。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 --> 
        <property name="maxUploadSize" value="10485760000"/> 
        <property name="defaultEncoding" value="utf-8"/> 
        <property name="maxInMemorySize" value="40960"/> 
    </bean> 
    
</beans>

(3)  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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	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">


	<context:annotation-config /> <!-- 针对使用注解进行配置 -->

	<bean id="springManager" class="com.study.controller.annotation.SpringManaget"></bean>

</beans>

     3 测试代码:

package com.study.controller.annotation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.support.RequestContextUtils;

@RequestMapping("/spring")
@Controller
public class SpringController {
	
	//Ioc
	@Resource(name = "springManager")//applicationContext.xml中的bean
	private ISpring iSpring;  

	@RequestMapping("/get")
	public String get(HttpServletRequest request) {

		WebApplicationContext webApplicationContext = WebApplicationContextUtils
				.getWebApplicationContext(request.getSession()
						.getServletContext()); // spring的上下文

		WebApplicationContext webApplicationContext2 = RequestContextUtils
				.getWebApplicationContext(request); //springMVC的上下文,集成自spring的上下文
		
		//ISpring iSpring = (SpringManaget)webApplicationContext.getBean("springManager");  通过spring的上下文获取bean
		//ISpring iSpring = (SpringManaget)webApplicationContext2.getBean("springManager");  通过springMVC的上下文获取bean

		System.out.println(iSpring.get());

		return "/success";
	}

}



具体项目见 myeclipse10中的springMVC3。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值