首页 > 代码库 > 01基于配置文件方式的SpringMVC,三种HandlerMapping,三种控制器

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



1 添加Spring MVC所需的jar.

技术分享

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

技术分享

3 web.xml的配置如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="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”文件。

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="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来访问

   -->

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

 

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

   <bean

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

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

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

   </bean>

 

   <beanclass="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

       <propertyname="mappings">

           <props>

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

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

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

               <propkey="/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>

 

   <beanid="toIndexBean"name="/toIndex.do"

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

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

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

   </bean>

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

   <beanname="/toIndex1.do"

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

       <!--

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

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

       -->

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

   </bean>

   

   <!--

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

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

   -->

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

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

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

   </bean>

   

   <!--

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

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

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

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

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

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

       <propertyname="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

 *@authortoto-pc

 *@date 2014-12-28

 *@note begin modify by涂作权

 */

publicclass Person {

   Integer id;

   String name;

   Integer age;

   String address;

   /**

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

    */

   Date birthday;

   /**

    * @return the id

    */

   public Integer getId() {

      returnid;

   }

   /**

    * @param id the id to set

    */

   publicvoid setId(Integer id) {

      this.id = id;

   }

   /**

    * @return the name

    */

   public String getName() {

      returnname;

   }

   /**

    * @param name the name to set

    */

   publicvoid setName(String name) {

      this.name = name;

   }

   /**

    * @return the age

    */

   public Integer getAge() {

      returnage;

   }

   /**

    * @param age the age to set

    */

   

   publicvoid setAge(Integer age) {

      this.age = age;

   }

   /**

    * @return the address

    */

   public String getAddress() {

      returnaddress;

   }

   /**

    * @param address the address to set

    */

   

   publicvoid setAddress(String address) {

      this.address = address;

   }

   /**

    * @return the birthday

    */

   public Date getBirthday() {

      returnbirthday;

   }

   /**

    * @param birthday the birthday to set

    */

   

   publicvoid 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,三种控制器