Announcement Announcement Module
Collapse
No announcement yet.
HELP : auto populate into spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HELP : auto populate into spring

    I create my project using Spring roo, so needless to say there are loads of files to be edited to obtain a particular output. I just need to bring in my username into a form im creating automatically. I'm having problems with this added to the fact that i just started out with spring roo and spring.

    i tried using request. get User Principal(). getName(). Im able to retrieve the user who is logged in currently but am not able to place that name in the input field (say, name) as soon as i load the form.

    Please help me out.

    Thanks !!

  • #2
    Personally, I think it is better, and easier, to set defaults and such in the controller.


    So for an arbitrary domain object, MyObject, that has a Username field:
    Code:
        @RequestMapping(params = "form", method = RequestMethod.GET)
        public String createForm(Model model) {
            MyObject myObject = new MyObject();
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            UserDetails userDetails = null;
            if (principal instanceof UserDetails) {
              userDetails = (UserDetails) principal;
            }
    
            myObject.setUsername(userDetails.getUsername());
            model.addAttribute("myObject", myObject);
            return "audiences/create";
        }
    And then on the update you can check if there is a value in username and populate it with the current user if needed.

    Code:
        @RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET)
        public String updateForm(@PathVariable("id") Long id, Model model) {
            MyObject myObject = MyObject.findMyObject(id);
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            UserDetails userDetails = null;
            if (principal instanceof UserDetails) {
              userDetails = (UserDetails) principal;
            }
            if(!myObject.getUsername().contentEquals(userDetails.getUsername()) {
                  myObject.setUsername(userDetails.getUsername());
            }
    
            model.addAttribute("myObject", myObject);
            return "audiences/update";
        }

    createForm and updateForm are roo generated and Pushed In, then modified.

    Something like the above might work for you.

    Comment


    • #3
      thanks, will try !!

      Comment

      Working...
      X