posts - 431,  comments - 344,  trackbacks - 0

          Spring MVC在使用annotation進行配置controller時候要注意:兩種urlmapping的模式不能同時使用,如果使用annotation就不能再配置urlmapping了。

          <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:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

           <!-- 對com.founder.action包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 -->
           <context:component-scan base-package="com.founder.action"/>
           
          <!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 -->
           <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
           
          <!--把請求的URL映射到Controller的name上面 -->
           <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 
            
           <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前后綴-->
           <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass">
             <value>org.springframework.web.servlet.view.JstlView</value>
            </property>
            <property name="prefix">
               <value>/jsp/</value>
            </property>
            <property name="suffix">
             <value>.jsp</value>
            </property>
           </bean>

          </beans>


          package com.founder.action;

          import org.springframework.stereotype.Controller;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.servlet.mvc.multiaction.MultiActionController;


          @Controller
          @RequestMapping
          public class HelloController{
           @RequestMapping
           public String world(){
            String name = "Eric";
            System.out.println("name is = " + name);
            return "hello";
           }
           
           @RequestMapping
           public String test() {
            String name = "test";
            System.out.println("name is = " + name);
            return "test";
           }
           
          }

          posted on 2009-01-07 21:43 周銳 閱讀(1449) 評論(1)  編輯  收藏 所屬分類: REST 、Spring
          主站蜘蛛池模板: 商水县| 崇义县| 宜君县| 乐山市| 宜春市| 朝阳县| 北海市| 雷波县| 阿巴嘎旗| 道真| 梅州市| 营山县| 鹤峰县| 新丰县| 吉首市| 云阳县| 嘉黎县| 津市市| 安福县| 铜陵市| 瓦房店市| 原平市| 大洼县| 泸水县| 沙洋县| 奉新县| 大厂| 绵竹市| 桂东县| 商洛市| 延川县| 莱西市| 阿坝| 洪湖市| 山阳县| 虹口区| 分宜县| 宣汉县| 海南省| 临高县| 阳春市|