2011-05-16 47 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