学习整合struts2与spring后,再学习springmvc与spring整合就比较简单了.
1.web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- spring mvc配置 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>com.skymr.smvcs.EncodingDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/config/spring_annotation-servlet.xml</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- spring入口 -->
<!-- 自动加载spring配置文件WEB-INF/applicationContenxt.xml -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 修改spring配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/config/applicationContext.xml</param-value>
</context-param>
</web-app>
spring配置文件与springmvc配置文件都放到src/config/下
2.springmvc配置文件
<?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:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 视图解释类 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
</bean>
<mvc:resources mapping="/image/**" location="/image/" />
<mvc:resources mapping="/js/**" location="/js/" />
<!-- 自动扫描包 -->
<context:component-scan base-package="com.skymr.smvcs.hello.ctrl"></context:component-scan>
<!-- 开启注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 上传文件大小限制 -->
<property name="maxUploadSize" value="3000000"></property>
<!-- 缓存大小 -->
<property name="maxInMemorySize" value="40960"></property>
</bean>
</beans>
3.Service ,serviceBean与controller类
package com.skymr.smvcs.hello.service;
public interface HelloWorldService {
public void say();
}
package com.skymr.smvcs.hello.service.impl;
import com.skymr.smvcs.hello.service.HelloWorldService;
public class HelloWorldServiceBean implements HelloWorldService{
public void say() {
System.out.println("springmvc-spring第一个实例:Hello World!!!");
}
}
package com.skymr.smvcs.hello.ctrl;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.skymr.smvcs.hello.service.HelloWorldService;
@Controller
@RequestMapping("/hello")
public class HelloWorldController{
//spring注解注入
@Resource
private HelloWorldService helloWorldService;
@RequestMapping("/helloWorld")
public String toHelloWorld(){
System.out.println("执行HelloWorldController toHelloWorld方法");
helloWorldService.say();
return "index";
}
}
4.spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorldService" class = "com.skymr.smvcs.hello.service.impl.HelloWorldServiceBean"></bean>
</beans>
5部署测试
http://localhost:8080/springmvc_spring/hello/helloWorld显示index.jsp页面成功.
后台打印
执行HelloWorldController toHelloWorld方法
springmvc-spring第一个实例:Hello World!!!