2011-05-16 43 views
1

我想在jspx文件中有一個隱藏字段。Robo隱藏字段在Jspx中

我想要做的是自動將認證用戶的名稱保存在數據庫中。下面是我如何做到這一點:

與修改我的豆:

public void Got.setUserkt(String userkt) { 
     final String currentUser = SecurityContextHolder.getContext().getAuthentication().getName(); 
     this.userkt = currentUser; 
    } 

環顧四周,我發現,我不得不使用呈現在我的create.jspx頁=「假」,但是當渲染設置爲假,我的輸入字段沒有數據保存在我的數據庫中。

我在做什麼錯?

回答

2

我會繼續爲我的新手做的。

第一步:從我的袋鼠CONTROLER切我的方法(EntityController_Roo_Controller.aj)

@RequestMapping(method = RequestMethod.POST) 

    public String create(@Valid Got got, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) { 

     if (bindingResult.hasErrors()) { 

      uiModel.addAttribute("got", got); 

      addDateTimeFormatPatterns(uiModel); 

      return "gots/create"; 

     } 

     uiModel.asMap().clear(); 

     got.persist(); 

     return "redirect:/gots/" + encodeUrlPathSegment(got.getId().toString(), httpServletRequest); 

    } 

第二步:梅索德粘貼到Java位指示(EntityController.java)

第三步:編輯獲取usernmae的方法並修改我的方法

  1. 加入主要主要作爲 參數
  2. 更新我的實體: got.setUserkt(principal.getName());

    @RequestMapping(方法= RequestMethod.POST)

    public String create(@Valid Got got, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest, Principal principal) { 
    
        if (bindingResult.hasErrors()) { 
    
         uiModel.addAttribute("got", got); 
    
         addDateTimeFormatPatterns(uiModel); 
    
         return "gots/create"; 
    
        } 
    
        uiModel.asMap().clear(); 
    
        got.setUserkt(principal.getName()); 
    
        got.persist(); 
    
    
    
        return "redirect:/gots/" + encodeUrlPathSegment(got.getId().toString(), httpServletRequest); 
    
    } 
    
  3. 再次

謝謝!

1

爲什麼不直接創建一個攔截保存目標實體的方面,並在ITD的Spring Controller中添加相應的字段或覆蓋相應的方法,方法是將其複製到* .java控制器並在其中添加字段?

+0

這是個好主意。我可以看到你擅長春天...... – gpasse 2011-05-16 15:31:57

+0

告訴我,我可以找到一個關於這種方法的好教程嗎? – gpasse 2011-05-16 15:43:27

+0

我找不到教程,但我發現[此鏈接](http://forum.springsource.org/showthread.php?100109-can-i-override-the-method()-in-controller-OR-域)。因此,只需在目標控制器的ITD中選擇目標方法(文件名爲'XxxController_Roo_Controller.aj')併爲其執行'Push In'重構(應該在Eclipse中可用)。然後打開你的'XxxController.java'並在保存之前執行你的實體修改(如你的代碼片段)。請記住,您應該在Roo控制檯運行時執行此操作。關於我的第一個建議(創建方面)只是讀了一些關於AspectJ的內容。 – Constantiner 2011-05-16 15:51:14

0

disableFormBinding和type的組合應該可以工作,如下所示。

注:如果不知道它的設計是這樣:)

+0

field:input disableFormBinding =「true」type =「hidden」 – Sharphill 2014-07-08 11:32:18