简体   繁体   中英

How to assign a value of an input to an object that comes from controller class?

I'm new to full stack programming and developing a training project. Before that I was using jsp and it was easy to handle these type of things in there, but nowadays working on ftl. I'm trying to send an object to template and want to fill this object's fields with values from template. However template returns null or gives an error in each attempt. What is my fault, or is it possible to do this in ftl? Thank you from now.

This is my Get method, sending DatabaseUser object to template,

@RequestMapping(value="/createRequest.html", method= RequestMethod.GET)
public ModelAndView add() throws ServiceException {
    DatabaseUser dbUser = new DatabaseUser();
    ModelAndView mav = new ModelAndView("index");
    mav.addObject("user", dbUser);
    return mav;
}

and this one is post method, trying to get form values of template and assign them to object's fields.

I just want to get string from form input, assign it to dbUser object's address field and send this object to save() method.

@RequestMapping(value="/saveRequest.html", method=RequestMethod.POST)
public ModelAndView save(@ModelAttribute("user") DatabaseUser dbUser,
                         BindingResult bindingResult) throws ServiceException{
    if(bindingResult.hasErrors()){
        LOGGER.debug("Binding error count: " + bindingResult.getErrorCount());
    }
    if(dbUser.getBaseUser.getUserId()==0){
        BaseUser baseUser = new BaseUser();
        baseUser.setAddres(dbUser.getBaseUser().getAddress());
        baseUser.setEmail(dbUser.getBaseUser().getEmail());
    }
    return add();

and the part of my ftl file that trying to satisfy this situation:

<div class="row">
    <div class="col-sm-12 col-lg-6">
        <form action="<@spring.url '/saveRequest.html' />"
             modelAttribute="user" method="post" id="form3">
             <input type="hidden" name="user" value="user"/>
<div class="form-group">
    <label for="address"><@spring.message "translationrequest.documentNumber" /></label>
    <input type="text" id="address" name="address" class="form-control"
          maxlength="250" value="${(getBaseuser().setAddress())!}"/>
</div>

Here are the things I noticed in your code:

The request mapping maps the REST url for this controller, hence you need to fix it to something like:

@RequestMapping(value="/save", method=RequestMethod.POST)

Then make sure your form hits it when user submits:

<form action="<@spring.url '/save' />"

Pay attention: There's no closign tag </form> for the form. Please add it after the second input field.

You have only one input field bound to Address, hence you won't get email field populated in this line baseUser.setEmail(dbUser.getBaseUser().getEmail()); which will set null to it.

What your code does is creating new DatabaseUser and sending it to index page. Then it constructs this HTML form with address field displaying the value of ${(getBaseuser().setAddress())!} and if your new BaseUser() doesn't set a value in the address, you won't see any address displayed.

I suggest you provide the DatabaseUser class so I can provide a working answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM