java综合(二)springmvc与spring整合

学习整合struts2与spring后,再学习springmvc与spring整合就比较简单了.

1.web.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.     <!-- spring mvc配置 -->  
  8.     <servlet>    
  9.         <servlet-name>springMVC</servlet-name>    
  10.         <servlet-class>com.skymr.smvcs.EncodingDispatcherServlet</servlet-class>    
  11.         <init-param>  
  12.             <param-name>contextConfigLocation</param-name>  
  13.             <param-value>classpath*:/config/spring_annotation-servlet.xml</param-value>  
  14.         </init-param>  
  15.         <init-param>  
  16.             <param-name>encoding</param-name>  
  17.             <param-value>UTF-8</param-value>  
  18.         </init-param>  
  19.         <load-on-startup>1</load-on-startup>    
  20.     </servlet>    
  21.     <servlet-mapping>    
  22.         <servlet-name>springMVC</servlet-name>    
  23.         <url-pattern>/</url-pattern>    
  24.     </servlet-mapping>    
  25.       
  26.     <!-- spring入口 -->  
  27.     <!-- 自动加载spring配置文件WEB-INF/applicationContenxt.xml -->  
  28.     <listener>  
  29.          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  30.     </listener>  
  31.     <!-- 修改spring配置文件路径 -->  
  32.     <context-param>  
  33.          <param-name>contextConfigLocation</param-name>  
  34.          <param-value>WEB-INF/classes/config/applicationContext.xml</param-value>  
  35.     </context-param>  
  36. </web-app>  

spring配置文件与springmvc配置文件都放到src/config/下
2.springmvc配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans    
  3.     xmlns="http://www.springframework.org/schema/beans"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xmlns:tx="http://www.springframework.org/schema/tx"    
  6.     xmlns:context="http://www.springframework.org/schema/context"      
  7.     xmlns:mvc="http://www.springframework.org/schema/mvc"      
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans     
  9.     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd     
  10.     http://www.springframework.org/schema/tx     
  11.     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd    
  12.     http://www.springframework.org/schema/context    
  13.     http://www.springframework.org/schema/context/spring-context-3.2.xsd    
  14.     http://www.springframework.org/schema/mvc    
  15.     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">   
  16.     <!-- 视图解释类 -->    
  17.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
  18.         <property name="prefix" value="/"/>    
  19.         <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->  
  20.     </bean>    
  21.       
  22.     <mvc:resources mapping="/image/**" location="/image/" />  
  23.     <mvc:resources mapping="/js/**" location="/js/" />  
  24.     <!-- 自动扫描包 -->  
  25.     <context:component-scan base-package="com.skymr.smvcs.hello.ctrl"></context:component-scan>  
  26.     <!-- 开启注解 -->  
  27.     <mvc:annotation-driven></mvc:annotation-driven>  
  28.       
  29.     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  30.         <!-- 默认编码 -->  
  31.         <property name="defaultEncoding" value="UTF-8"></property>  
  32.         <!-- 上传文件大小限制 -->  
  33.         <property name="maxUploadSize" value="3000000"></property>  
  34.         <!-- 缓存大小 -->  
  35.         <property name="maxInMemorySize" value="40960"></property>  
  36.     </bean>  
  37. </beans>      


3.Service ,serviceBean与controller类 

  1. package com.skymr.smvcs.hello.service;  
  2.   
  3. public interface HelloWorldService {  
  4.   
  5.     public void say();  
  6. }  



  1. package com.skymr.smvcs.hello.service.impl;  
  2.   
  3. import com.skymr.smvcs.hello.service.HelloWorldService;  
  4.   
  5. public class HelloWorldServiceBean implements HelloWorldService{  
  6.   
  7.     public void say() {  
  8.         System.out.println("springmvc-spring第一个实例:Hello World!!!");  
  9.     }  
  10.   
  11. }  




  1. package com.skymr.smvcs.hello.ctrl;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7.   
  8. import com.skymr.smvcs.hello.service.HelloWorldService;  
  9. @Controller  
  10. @RequestMapping("/hello")  
  11. public class HelloWorldController{  
  12.   
  13.     //spring注解注入  
  14.     @Resource  
  15.     private HelloWorldService helloWorldService;  
  16.       
  17.     @RequestMapping("/helloWorld")  
  18.     public String toHelloWorld(){  
  19.         System.out.println("执行HelloWorldController toHelloWorld方法");  
  20.         helloWorldService.say();  
  21.         return "index";  
  22.     }  
  23.       
  24. }  


4.spring配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  6.     <bean id="helloWorldService" class = "com.skymr.smvcs.hello.service.impl.HelloWorldServiceBean"></bean>  
  7. </beans>  


5部署测试

http://localhost:8080/springmvc_spring/hello/helloWorld显示index.jsp页面成功.

后台打印

执行HelloWorldController toHelloWorld方法
springmvc-spring第一个实例:Hello World!!!

原文地址:https://www.cnblogs.com/bkyliufeng/p/6293679.html