Search code examples
spring-roo

Roo hidden field in Jspx


I want to have a hidden field in a jspx file.

What I would like to do is automatically save the name of the authentificated user in the database. Here is how I do it :

Modify my bean with :

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

Looking around I found out that i had to use render="false" in my create.jspx page but when render is set to false, no data from my input field is saved in my database.

What am I doing wrong ?


Solution

  • I will resume what I did, for my fellow rookies.

    First step : Cut my method from my roo 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);
    
        }
    

    Second Step : Paste the methode into the java contoller (EntityController.java)

    Third Step : Edit the method to get the usernmae and to modifiy my method with it

    1. added Principal principal as a parameter
    2. update my entity : 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);
      
      }
      

    Thanks again !