springmvc入门案例

  1. 简要概述springmvc
    Spring C 层框架的核心是 DispatcherServlet,它的作用是将请求分发给不同的后端处理器,也即 使用了一种被称为Front Controller 的模式.Spring 的C 层框架使用了后端控制器来、映射处理器和视图解析器来共同完成C 层框架的主要工作。并且spring 的C 层框架还真正地把业务层处理的数据结果和相应的视图拼成一个对象,即我们后面会经常用到的ModelAndView 对象。
  2. 搭建环境
    在spring 的官方API 文档中,给出所有包的作用概述,现列举常用的包及相关作用:
    org.springframework.aop-3.0.5.RELEASE.jar :与Aop 编程相关的包
    org.springframework.beans-3.0.5.RELEASE.jar :提供了简捷操作bean 的接口
    org.springframework.context-3.0.5.RELEASE.jar :构建在beans 包基础上,用来处理资源文件及国际
    化。
    org.springframework.core-3.0.5.RELEASE.jar :spring 核心包
    org.springframework.web-3.0.5.RELEASE.jar :web 核心包,提供了web 层接口
    org.springframework.web.servlet-3.0.5.RELEASE.jar :web 层的一个具体实现包,DispatcherServlet也位于此包中
  3. 编写HelloWorld 实例
    步骤一、建立名为springMVC_01_helloword ,并导入上面列出的jar 包。
    步骤二、编写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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springMVC_01_helloworld</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

注意:配置初始化参数,是为了加载springmvc-servlet.xml配置文件,这里放在了src目录下。如果不配置,则默认放在WEB-INF目录下。
步骤三、建立 spmvc-servlet.xml 文件,它的命名规则: servlet-name-servlet.xml 。它的主要代码如下:

<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 ">
    <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/hello.do">helloControl</prop>
            </props>
        </property>
    </bean> 
    <bean id="helloControl" class="com.asm.HelloWorld">
    </bean>
</beans>

说明: hello.do 的请求将给名为 helloControl 的 bean 进行处理。
步骤四、完成 HelloWord.java 的编写,代码如下:

package com.asm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloWorld implements Controller{

    @Override
    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        // TODO Auto-generated method stub
        ModelAndView mav=new ModelAndView("hello.jsp");
        mav.addObject("message","hello world!" );
        return mav;
    }

}

说明 :ModelAndView 对象是包含视图和业务数据的混合对象,即是说通过此对象,我们可以知道所
返回的相应页面(比如这里返回hello.jsp 页面),也可以在相应的页面中获取此对象所包含的业务数据
(比如这里message-hello worrld )。
步骤五、在当前项目web 根目录下编写hello.jsp ,主要代码如下:

<body>
    世界,你好!
    获取值:${message}
  </body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值