Spring MVC和其他众多框架(Structs,webwork)一样,它基于MVC(GOF的一种)设计理念,它采取了松耦合可插拔的组件结构,比其他MVC框架更具扩展性和灵活性。Spring MVC的请求处理控制器无需实现任何接口,只需要通过注解(Java5新特性)就可使普通的Java类充当控制器使用,同时他还支持REST(表述性状态转移)风格。
如果你对何为REST风格不甚了解的,这里有一篇引自百度文库的文章(第五章开始):http://wenku.baidu.com/view/660324c58bd63186bcebbcc3.html很好的介绍了REST的理念。
Spring MVC作为MVC框架的后起之秀,如果能得到广大程序员的亲睐呢,让我们来看看它与Struts,Struts2(基于webwork2.0)的区别及优势(引自百度文库):
http://wenku.baidu.com/view/492447777375a417866f8f68.html
1.DispatcherServlet
DispatcherServlet作为Spring MVC的总控,它负责截获客户端请求并将其分派给对应的处理器进行处理,通过下面的图,我们来看看SpringMVC的整体架构
可以看出在整个框架中DIspatcherServlet处于核心位置,他负责协调和组织不同组件完成请求并返回响应。和其他大多数MVC框架一样,SpringMVC通过DIspatcherServlet接收要处理的所有请求。
2.Spring与SpringMVC
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/conf/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
通过contextConfigLocation参数指定业务层Spring容器的配置文件(多个配置文件使用逗号分隔),ContextLoaderListener是一个ServletContextListener,他通过contextConfigLocation处配置的的参数指定的配置文件启动业务层的Spring容器。
3.SpringMVC组件装配
此方法在WebApplicationContext初始完成后被执行,该方法的工作就是通过反射机制查找并装配Spring容器中用户自定义的组件bean(找不到会装载默认的组件)。
下面写个简单的实例:
1.环境搭建(使用maven),加入对spring的依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
2.web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>Blog</display-name>
<context-param>
<!--业务层配置文件-->
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/conf/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/conf/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<servlet-name>blog</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>blog</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.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: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-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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 扫描该包下的所有类,使注解生效-->
<context:component-scan base-package="com.blog.web"/>
<!-- 定义Spring MVC的视图解析器,将视图的逻辑名解析为/WEB-INF/views/XXXXX.jsp-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans><span style="font-family: Arial, Helvetica, sans-serif;"> </span>
4.业务层配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<!-- 扫描该包,使注解生效 -->
<context:component-scan base-package="com.blog.model"></context:component-scan>
</beans>
5.写一个简单的controller
@Controller//使用<span style="font-family: Arial, Helvetica, sans-serif;">@Controller对类进行标注,使其成为一个可以处理HTTP请求的控制器</span>
@RequestMapping("/user")//在类处标注相对于web应用的部署路径
public class UserController {
@Autowired//自动注入业务类
IUserService userService;
/**
* 返回注册页面
*/
@RequestMapping("/register")
public String register(){
return "user/register";//被解析为<span style="font-family: Arial, Helvetica, sans-serif;">/WEB-INF/views/X</span><span style="font-family: Arial, Helvetica, sans-serif;">user/register</span><span style="font-family: Arial, Helvetica, sans-serif;">.jsp</span><span style="font-family: Arial, Helvetica, sans-serif;">
</span> }
/**
* 创建用户
*/
@RequestMapping(value="/userRegister",method=RequestMethod.POST)
public ModelAndView createUser(User user){//Spring MVC会自动通过反射将传入的参数放入user对象中
boolean flag = userService.createUser(user);
ModelAndView modelAndView = new ModelAndView();
if(flag){
modelAndView.setViewName("user/createSuccess");
modelAndView.addObject("user", user);
}else{
modelAndView.setViewName("user/createError");
}
return modelAndView;
}
}
6.业务实现类
@Service
public class UserServiceImpl implements IUserService {
@Override
public boolean createUser(User user) {
return true;
}
下面运行程序让我们来看看效果