Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1,Struts2等。
相信学到SpringMVC的同学们已经学过Struts2这个webwork框架,甚至学过Spring这个优雅的框架,如果读者已经对这两个框架有过了解,那么想学好这个框架应该就很简单了。
SpringMVC所需的框架请自行在网上下载。本文只是简单的记录学习历程。
web.xml之中的配置如下,SpringMVC过滤器是基于servlet 的一个类。这个类负责映射我们的请求。
<!-- 配置servlet -->
<servlet>
<!-- 配置servlet 名称 -->
<servlet-name>springMVC</servlet-name>
<!-- 配置servlet 对应的类-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置servlet 初始化参数 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
springMVC.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:mvc="http://www.springframework.org/schema/mvc"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<span style="white-space:pre"> </span><bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name="/hello.action" class="controller.HelloController"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
对应的控制类,实现了Controler接口
@Override
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
// TODO Auto-generated method stub
ModelAndView mav = new ModelAndView();
mav.addObject("hello", "hello,This is my Hellow Page");
mav.setViewName("/hello.jsp");
return mav;
}
只需简单的配置就能使用,初稿,待修稿。