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 周銳 閱讀(1448) 評論(1)  編輯  收藏 所屬分類: RESTSpring
          主站蜘蛛池模板: 开化县| 天台县| 三亚市| 阜南县| 长治市| 马尔康县| 礼泉县| 嘉兴市| 揭阳市| 克什克腾旗| 长乐市| 剑河县| 克拉玛依市| 临颍县| 防城港市| 历史| 晋城| 任丘市| 鄂托克前旗| 花垣县| 津市市| 汪清县| 集安市| 神池县| 右玉县| 杂多县| 海丰县| 潍坊市| 台中县| 广州市| 运城市| 宁远县| 合作市| 云霄县| 阿拉尔市| 长沙市| 潼南县| 罗甸县| 台湾省| 松江区| 东宁县|