【问题标题】:Roo hidden field in JspxJspx中的Roo隐藏字段
【发布时间】:2011-05-16 15:12:45
【问题描述】:

我想在 jspx 文件中有一个隐藏字段。

我想做的是自动将经过身份验证的用户的名称保存在数据库中。这是我的做法:

修改我的bean:

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

环顾四周,我发现我必须在我的 create.jspx 页面中使用 render="false",但是当 render 设置为 false 时,我的输入字段中的任何数据都不会保存在我的数据库中。

我做错了什么?

【问题讨论】:

    标签: spring-roo


    【解决方案1】:

    为了我的新人,我会继续我所做的。

    第一步:从我的 roo 控制器中删除我的方法 (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)中

    第三步:编辑方法以获取用户名并用它修改我的方法

    1. 将 Principal 主体添加为 参数
    2. 更新我的实体: got.setUserkt(principal.getName());

      @RequestMapping(method = 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);
      
      }
      

    再次感谢!

    【讨论】:

      【解决方案2】:

      为什么不直接创建一个切面来拦截保存目标实体并添加相应的字段或通过将其复制到 *.java 控制器并在其中添加字段来覆盖 ITD 中的 Spring Controller 中的相应方法?

      【讨论】:

      • 告诉我,我能找到一个关于这种方法的好教程吗?
      • 我找不到教程,但我找到了this link。因此,只需在目标控制器的 ITD(名为 XxxController_Roo_Controller.aj 的文件)中选择目标方法并为其执行 Push In 重构(应该在 Eclipse 中可用)。然后打开您的XxxController.java 并在保存之前执行您的实体修改(如您的代码 sn-p 中)。请记住,您应该在运行 Roo 控制台的情况下执行它。关于我的第一个建议(创建方面),请阅读有关 AspectJ 的内容。
      【解决方案3】:

      disableFormBinding 和 type 的组合应该可以工作,如下所示。

      注意:不确定是不是这样设计的:)

      【讨论】:

      • 字段:输入 disableFormBinding="true" type="hidden"
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多