Announcement Announcement Module
Collapse
No announcement yet.
Question about empty form Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question about empty form

    Hello!
    I have the following code in my .jsp file. In short, my intention is to make web page which will fetch the data from database, then display it in the following form, so someone can change it. Problem is, form doesn't have any prepopulated fields. I have solved the problem, and my solution is in the last code block, but I'm interested in why couldn't I use first version of controller code.
    Thank you for your answers!
    Code:
    		<form:form commandName="korisnik">
    			ID: <form:input readonly="true" path="id"/>
    			<br />
    			Ime: <form:input path="firstname" />
    			<br />
    			Prezime: <form:input path="lastname" />
    			<br />
    			e-mail: <form:input path="email" />
    			<br />
    			<input type="submit" value="Promijeni!" />
    		</form:form>
    Original (non-functioning) version of code:
    Code:
    	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    	public String update(@PathVariable int id, @ModelAttribute Korisnik korisnik){
    		korisnik = korisnici.getUser(id);
    		return "update";
    	}
    My solution:
    Code:
    	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    	public String update(@PathVariable int id, @ModelAttribute Korisnik korisnik){
    		Korisnik user = korisnici.getUser(id);
    		korisnik.setId(user.getId());
    		korisnik.setFirstname(user.getFirstname());
    		korisnik.setLastname(user.getLastname());
    		korisnik.setEmail(user.getEmail());
    		return "update";
    	}
    P.S. Forgive me if my English is bad, since I'm not native English speaker.

  • #2
    Well how about basic java... Assigning something to a method variable is well pretty much useless if you want to use it outside of the method. You will have to put it in the model so your method should be changed...

    Code:
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable int id, Model model)
    	model.addAttribute("korisnik", korisnici.getUser(id));
    	return "update";
    }
    This is also explained in the reference guide so you might want to read that...

    Comment


    • #3
      Yes, I understand that. I'm just saying that it works without using Model ("My solution"). I was just wandering why doesn't it work in first case ("Original version of code"), since my first and second version are basically the same, no?

      Comment


      • #4
        As I stated it is basic java.

        Originally posted by mdeinum
        Assigning something to a method variable is well pretty much useless if you want to use it outside of the method.
        I suggest a basic java book?

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          As I stated it is basic java.
          I suggest a basic java book?
          Thank you for your answers!
          One more question, if I may: Can you recommend any book?

          Comment

          Working...
          X