沉睡森林@漂在北京

          本處文章除注明“轉載”外均為原創,轉載請注明出處。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            152 隨筆 :: 4 文章 :: 114 評論 :: 0 Trackbacks
          <2009年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          隨筆分類(115)

          隨筆檔案(154)

          技術blog

          搜索

          最新評論

          在利用spring的mvc開發過程中,需要將User對象從session中取出來使用。參照網上的做法,我利用了AnnotationMethodHandlerAdapter來解決這個問題。下面是XML代碼,放到web-inf下面的springMVC配置文件中:
              <bean id="userArgumentResolver" class="com.greatwall.module.yhqxgl.interceptor.UserArgumentResolver" />
              
          <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"  >
                   
          <property name="customArgumentResolver">
                      
          <ref bean="userArgumentResolver"/>
                  
          </property>
              
          </bean>

          下面是java代碼:
          package com.greatwall.module.yhqxgl.interceptor;

          import org.springframework.core.MethodParameter;
          import org.springframework.web.bind.support.WebArgumentResolver;
          import org.springframework.web.context.request.NativeWebRequest;
          import org.springframework.web.context.request.RequestAttributes;

          import com.greatwall.module.yhqxgl.YhqxglConstants;
          import com.greatwall.module.yhqxgl.domain.User;

          public class UserArgumentResolver implements WebArgumentResolver {

              
          public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
                  
                  
          if (methodParameter.getParameterType().equals(User.class)) {
                      
          return webRequest.getAttribute(YhqxglConstants.GREATWALL_SESSION_USER, RequestAttributes.SCOPE_SESSION);
                  }
                  
          return UNRESOLVED;
              }

              
          }

          調用的過程很簡單,直接在controller對應的函數里面加入參數User即可。
          @RequestMapping("/txn960204.do")
              
          public void txn960204(HttpServletResponse response, HttpServletRequest req,SysCodedata sysCodedata,User user) {
                  
          try {
                      sysCodedata.setUpdateTime(DateUtils.currentDateTime());
                      sysCodedata.setUpdateUsername(user.getUsername());
                      
                      sysCodedataService.updateOneSysCodedata(sysCodedata);
                      JsonUtils.success(
          "修改保存代碼明細表成功!", response);
                  } 
          catch (ServiceException e) {
                      e.printStackTrace();
                      JsonUtils.error(e, response);
                  }
              }
              

          posted on 2009-06-04 19:31 王總兵 閱讀(3107) 評論(0)  編輯  收藏 所屬分類: Spring
          主站蜘蛛池模板: 山阴县| 平山县| 同心县| 延庆县| 扎兰屯市| 精河县| 延寿县| 巴东县| 察雅县| 石狮市| 平舆县| 泌阳县| 罗田县| 长岛县| 施秉县| 仁寿县| 炎陵县| 桃源县| 和田市| 梁平县| 韶山市| 双牌县| 甘南县| 河北区| 溆浦县| 闵行区| 察雅县| 长岭县| 砚山县| 大洼县| 延边| 广饶县| 永善县| 密山市| 大邑县| 寻甸| 阳高县| 康平县| 柘荣县| 平南县| 乡城县|