码迷,mamicode.com
首页 > 移动开发 > 详细

01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器

时间:2014-12-28 20:50:09      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:



1  添加Spring MVC所需的jar.

技术分享

2  创建一个以下项目结构的springmvc项目

技术分享

3  web.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

   

    <servlet>

        <servlet-name>springmvc</servlet-name>

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

    </servlet>

    <servlet-mapping>

        <servlet-name>springmvc</servlet-name>

        <!-- structs/*springmvc不能是/*类型,要是*.xxx类型 -->

        <url-pattern>*.do</url-pattern>

    </servlet-mapping>

</web-app>

4  springmvc-servlet.xml的配置,这里的springmvc-servlet.xml名字默认是web.xml文件中的映射名称”+“-”+“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: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.0.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context-3.0.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

 

    <!--

    如果不配置映射处理器会采用默认的映射处理器BeanNameHandlerMapping

    下面可以通过:http://localhost:8080/springmvc/hello.do来访问

    -->

    <bean id="testController" name="/hello.do" class="com.rl.controller.TestController" />

 

    <!-- 定义视图解析器 -->

    <bean

    class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="prefix" value="/WEB-INF/jsp/"></property>

        <property name="suffix" value=".jsp"></property>

    </bean>

 

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        <property name="mappings">

            <props>

                <!-- 通过:http://localhost:8080/springmvc/hello1.do可以访问 -->

                <prop key="/hello1.do">testController</prop>

                <!-- 通过:http://localhost:8080/springmvc/toIndexBean.do可以访问 -->

                <prop key="/toIndexBean.do">toIndexBean</prop>

            </props>

        </property>

    </bean>

 

    <bean

    class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

    <!-- 使用Controller的类名的首字符小写加.do -->

    <bean

    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"></bean>

 

    <bean id="toIndexBean" name="/toIndex.do"

    class="org.springframework.web.servlet.mvc.ParameterizableViewController">

        <!-- 也需要使用视图解析器来解析 -->

        <property name="viewName" value="jsp1/index"></property>

    </bean>

    <!-- 通过:http://localhost:8080/springmvc/toIndex1.do访问 -->

    <bean name="/toIndex1.do"

    class="org.springframework.web.servlet.mvc.ParameterizableViewController">

        <!--

            也需要使用视图解析器来解析,下面的实际访问的是地址是:

            上面的prefix + 下面的value + 上面的suffix

        -->

        <property name="viewName" value="jsp1/index"></property>

    </bean>

   

    <!--

    访问方式是:http://localhost:8080/springmvc/comm.do?id=2&name=toto&XXXXX=方式

    在控制台可以看到输出结果

    -->

    <bean name="/comm.do" class="com.rl.controller.CommController">

        <!-- 指定Controller中的object类型的参数上的数据类型 -->

        <property name="commandClass" value="com.rl.model.Person"></property>

    </bean>

   

    <!--

    这种可以做简单的类型转换,如果数据类型不一致,它会返回到页面中

    访问路径是:http://localhost:8080/springmvc/form.do-->

    <bean name="/form.do" class="com.rl.controller.FormController">

        <!-- 指定ControllerObject类型的参数上的数据类型 -->

        <property name="commandClass" value="com.rl.model.Person"></property>

        <property name="formView" value="form"></property>

        <property name="successView" value="success"></property>

    </bean>

</beans>

5  TestController的代码如下:

package com.rl.controller;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.AbstractController;

 

/**

 * @brief TestController.java springmvcController

 * @attention

 * @author toto

 * @date 2014-12-28

 * @note begin modify by 涂作权

 */

public class TestController extends AbstractController {

 

         @Override

         protected ModelAndView handleRequestInternal(HttpServletRequest arg0,

                            HttpServletResponse arg1) throws Exception {

                   System.out.println("hello springmvc");

                   //视图解析器在解析ModelAndView的时候会自动追加前缀和后缀,

                   //如果视图解析器的前缀下依然有目录结构,在ModelAndView

                   //来指定前缀和后缀直接的一段路径

                   return new ModelAndView("jsp1/index");

         }

}

浏览器访问地址:http://localhost:8080/springmvc/hello.do结果如下:

技术分享

浏览器访问地址:http://localhost:8080/springmvc/hello1.do结果如下:

技术分享

浏览器访问地址:http://localhost:8080/springmvc/toIndexBean.do结果如下:

技术分享

6  PersonjavaBean代码如下:

package com.rl.model;

 

import java.sql.Date;

 

/**

 * @brief Person.java person对应的JavaBean

 * @attention

 * @author toto-pc

 * @date 2014-12-28

 * @note begin modify by 涂作权

 */

public class Person {

    Integer id;

    String name;

    Integer age;

    String address;

    /**

     * 修改所有同名的变量Shift + Alt + R

     */

    Date birthday;

    /**

     * @return the id

     */

    public Integer getId() {

       return id;

    }

    /**

     * @param id the id to set

     */

    public void setId(Integer id) {

       this.id = id;

    }

    /**

     * @return the name

     */

    public String getName() {

       return name;

    }

    /**

     * @param name the name to set

     */

    public void setName(String name) {

       this.name = name;

    }

    /**

     * @return the age

     */

    public Integer getAge() {

       return age;

    }

    /**

     * @param age the age to set

     */

   

    public void setAge(Integer age) {

       this.age = age;

    }

    /**

     * @return the address

     */

    public String getAddress() {

       return address;

    }

    /**

     * @param address the address to set

     */

   

    public void setAddress(String address) {

       this.address = address;

    }

    /**

     * @return the birthday

     */

    public Date getBirthday() {

       return birthday;

    }

    /**

     * @param birthday the birthday to set

     */

   

    public void setBirthday(Date birthday) {

       this.birthday = birthday;

    }

}

7  CommController的代码如下:

package com.rl.controller;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.springframework.validation.BindException;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.mvc.AbstractCommandController;

 

import com.rl.model.Person;

 

/**

 * @brief CommController.java

 * @attention

 * @author toto

 * @date 2014-12-28

 * @note begin modify by 涂作权

 */

@SuppressWarnings("deprecation")

public class CommController extends AbstractCommandController {

 

         /**

          * Object的参数用来接受实体参数,Person中的属性的接收传

          * 递参数的名字一定要和Person中的set方法后名的字符串匹配,

          * 大小写不区分

          */

         @Override

         protected ModelAndView handle(HttpServletRequest arg0,

                            HttpServletResponse arg1, Object obj, BindException arg3)

                            throws Exception {

                   Person p = (Person) obj;

                   System.out.println("id:" + p.getId() + " name:" + p.getName());

                   return new ModelAndView("index");

         }

}

浏览器中的地址:http://localhost:8080/springmvc/comm.do?id=2&name=toto结果如下:

技术分享

控制台中的内容如下:

技术分享

8  FormController的代码如下:

package com.rl.controller;

 

import java.sql.Date;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;

import org.springframework.web.bind.ServletRequestDataBinder;

import org.springframework.web.servlet.mvc.SimpleFormController;

 

@SuppressWarnings("deprecation")

public class FormController extends SimpleFormController {

        

         @Override

         protected void doSubmitAction(Object command) throws Exception {

                   System.out.println(command);

         }

        

         /**

          * 注册时间类型的属性转换器

          */

         @Override

         protected void initBinder(HttpServletRequest request,

                            ServletRequestDataBinder binder) throws Exception {

                   binder.registerCustomEditor(Date.class,

                                     new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));

         }

}

浏览器中的地址:http://localhost:8080/springmvc/form.do结果如下:

技术分享

01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器

标签:

原文地址:http://blog.csdn.net/tototuzuoquan/article/details/42217425

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!