springMVC是spring的一个模快:
使用springMVC的开发步骤:
1)导入jar包:spring核心包+spring-web-3.2.5.RELEASE(spingweb核心博爱).jar+spring-webmvc-3.2.5.RELEASE.jar(spingweb的包)
2)导入映射文件:sping源码包中搜索xmls:mvc约束 找到指定的约束配置
3)在web.xml中配置springmvc的核心类DispatcherServlet
4)写一控制器类HelloAction实现controller接口或者继承AbstractController类
5)在WEB-INF目录下写一个命名为<servlet-name>(这是核心类在web.xml的配置名称),假设名称为action那么需要在这个目录下写一个action-servlet.xml的文件
案例:
web.xml的配置----->这个类间接的继承了HttpServlet类的接口,因此我们知道springmvc是单例模式..即action只创建一次.
<servlet> <servlet-name>action</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>HelloAction
package cn.itcast.springmvc01;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;public class HelloAction extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("Hello SpringMVC!"); ModelAndView modelAndView=new ModelAndView(); modelAndView.setViewName("index.jsp"); return modelAndView; } }WEB-INF下的命名为action-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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 注册控制器类 --> <bean name="/hello.action" class="cn.itcast.springmvc01.HelloAction"></bean> </beans>HelloAction转发到的index.jsp
body> Hello Spring MVC <br> </body>运行测试 :
版权声明:本文为博主原创文章,未经博主允许不得转载。