Getting object from field form

Vampirenostra

Problem: getting an object from a field as a parameter.

Code: Entity User with fields:

Long id;
String name;
Office office;

Entity office with fields:

Long id;
String name;

newuser.vm

<title>NEW USER</title>
<body>
<form method="post" action="/save" property="user">  
    Name:
    <input type="text" name="name" path="name"/>    <br>

    Office:
    <select name="office" path="office">
        #foreach($office in $offices)
            <option value="$office">$office.name</option>
        #end
    </select>    <br>
    <input type="submit" value="SAVE"/>
</form>
</body>

And the Controller

@Controller
public class ViewController {

    @Autowired
    private UserService userService;
    @Autowired
    private OfficeService officeService;

@RequestMapping(value = "/newuser", method = RequestMethod.GET)
    public ModelAndView newuser(){

        return new ModelAndView("fragments/newuser.vm","command",new User());
    }
@RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView save(@ModelAttribute("user") User user){

        userService.create(user);
        return new ModelAndView("redirect:/list");
    }
//Model Attributes
    @ModelAttribute
    public void userTypesList(Model model){
        model.addAttribute("types", userService.getPositions());
    }
    @ModelAttribute
    public void officesList(Model model){
        model.addAttribute("offices", officeService.getAll();
}

So in the result of submition I have to get the new User with an Office as one of its fields. But the <option value="$office">$office.name</option> returns a string representation of the object but not the object itself i guess. So i need to find a way to correctly get that to the /save controller. Ofcourse I can get field by field data from the form and create a new user manualy getting the office.id from the form and than sending another request to sql to get the officeById(id), but that seems to be the bad way of coding. Anyone can help?

Claude Brisson

What you need is:

<option value="$office.id">$office.name</option>

That's what id fields are for. Only the office id get transmitted back when the form is submitted, and that's all you need to populate the join towards the office table while creating your new user.

To have $office display the string representation of the whole object (that is, its toString() method) is the expected behavior.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Form field values are not getting passed into JSON object correctly

From Dev

getting value from user in form's text field html

From Dev

getting a field from a json object using a "address string" in JavaScript

From Dev

Cloning object after getting data from a form in Symfony2

From Dev

Getting the Reference URL from form element using hidden field(PHP, HTML form)

From Dev

Getting field values of a javascript object

From Dev

Getting JSON from Form

From Dev

Getting JSON from Form

From Dev

Getting object form ObservableArray in Nativescript

From Dev

Getting empty data object from my form react-hook-form

From Dev

Access form field object in Template

From Dev

Access form field object in Template

From Dev

Ruby on Rails: Getting the value from a select form field before it's submitted

From Dev

CodeIgniter getting form field value to lowercase

From Dev

Getting bad results to form field formula Word

From Dev

Getting path manually entered into a form field

From Dev

CodeIgniter getting form field value to lowercase

From Dev

Having trouble getting <select> value to form field

From Dev

Rails: Form field not getting passed to method

From Dev

Getting Error with Django Phone Number Form Field

From Dev

Getting Mysql field from an array

From Dev

Getting Mysql field from an array

From Dev

When getting a repo object from the GitHub API, what does the network_count field represent?

From Dev

Simplify getting the data from the form

From Dev

Getting data from outside the form

From Dev

Simplify getting the data from the form

From Dev

Dynamic form class for getting value from a form

From Dev

How Spring MVC getting data from Model and why is modelAttribute object created everytime after submitting form?

From Dev

Getting object field previous value hibernate JPA

Related Related

  1. 1

    Form field values are not getting passed into JSON object correctly

  2. 2

    getting value from user in form's text field html

  3. 3

    getting a field from a json object using a "address string" in JavaScript

  4. 4

    Cloning object after getting data from a form in Symfony2

  5. 5

    Getting the Reference URL from form element using hidden field(PHP, HTML form)

  6. 6

    Getting field values of a javascript object

  7. 7

    Getting JSON from Form

  8. 8

    Getting JSON from Form

  9. 9

    Getting object form ObservableArray in Nativescript

  10. 10

    Getting empty data object from my form react-hook-form

  11. 11

    Access form field object in Template

  12. 12

    Access form field object in Template

  13. 13

    Ruby on Rails: Getting the value from a select form field before it's submitted

  14. 14

    CodeIgniter getting form field value to lowercase

  15. 15

    Getting bad results to form field formula Word

  16. 16

    Getting path manually entered into a form field

  17. 17

    CodeIgniter getting form field value to lowercase

  18. 18

    Having trouble getting <select> value to form field

  19. 19

    Rails: Form field not getting passed to method

  20. 20

    Getting Error with Django Phone Number Form Field

  21. 21

    Getting Mysql field from an array

  22. 22

    Getting Mysql field from an array

  23. 23

    When getting a repo object from the GitHub API, what does the network_count field represent?

  24. 24

    Simplify getting the data from the form

  25. 25

    Getting data from outside the form

  26. 26

    Simplify getting the data from the form

  27. 27

    Dynamic form class for getting value from a form

  28. 28

    How Spring MVC getting data from Model and why is modelAttribute object created everytime after submitting form?

  29. 29

    Getting object field previous value hibernate JPA

HotTag

Archive