springMVC 体验springMVC

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>
运行测试 :






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值