1

I have a query. I am using @ModelAttribute on a function in a formController.

@ModelAttribute("modelAttrVar")
public ModelAttr function(){
    ModelAttr obj = new ModelAttr();
    //...code
    return obj;
}

But this model attribute is not added to the ModelAndView I am returning.....

public ModelAndView func2(){
    ModelAndView obj = new ModelAndView();
    obj.addAttribute("variableName" , value);
    obj.setViewName("viewName");
    return obj;
}

Now when I see the jsp then the model Attribute variable modelAttrVar is available in jsp and is giving values. How ??

Please Help!!

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
Software Company
  • 151
  • 4
  • 15

1 Answers1

2

There's something not clear!

The @ModelAttribute annotation is used to bind a form inside a jsp to a controller, in order to have all the fields, written inside an html form, available in a Controller.

What is @ModelAttribute in Spring MVC?

So basically a method annotated with @ModelAttribute should be work as landing point method, after a post request (form submit).

So let's take an example, you have a POJO with two variable:

public class ModelAttrExample {

String name;
String lastName;

///getter and setter...
}

a JSP indexForm.jsp

<form:form action="/greeting" >
 <form:input path="name" />
 <form:input path="lastName" />

 <input type="submit" value="Submit" />
</form:form>

and a SpringController

@Controller
public class GreetingController {

@RequestMapping(value="/greeting", method=RequestMethod.GET)
public String greetingForm(Model model) {
    model.addAttribute("", new ModelAttrExample ());
    return "indexForm";
}

@RequestMapping(value="/greeting", method=RequestMethod.POST)
public String greetingSubmit(@ModelAttribute ModelAttrExample example, Model model) {

   example.getName();//same form value
   example.getLastName(); //same form value
   //do your logic here...
   }
}

Once the form is submitted the greetingSubmit() method is triggered, and an instance of the ModelAttrExample, filled with the datas of the form, will be available inside the method.

so... @ModelAttribute is used to take the values from a html form field and put this datas inside an class instance variables.

I suggest you to follow this tutorial from Spring, It is very well written and very easy to understand

If you need more info do not hesitate to ask :)

Community
  • 1
  • 1
daN
  • 101
  • 5
  • Thanks Sir... for your reply. Actually the case is when we open a record from list screen . The call goes to FormController and there it goes to modelAttribute function @ModelAttribute("modelAttrVar") public ModelAttr function(){} which extracts the particular record from db. Then after func2() we receive modelAndView and then finally go to jsp. And Then in jsp we get the modelAttrVar model Attribute like ${modelAttrVar.var1} . – Software Company Mar 24 '16 at 11:16
  • So means when opening a record we never add this modelAttrVar to ModelAndView but it still goes to jsp and data from modelAttrVar object fills up the relevant fields on jsp page. How? – Software Company Mar 24 '16 at 11:19
  • I'm still not understanding your flow, you said "Then after func2() we receive modelAndView", who is calling func2() ? How do you receive model and view in that method? If you need just to show a record selectec from a list, for example you could trigger a method passing via get the id, take the datas from the db and than render the extracted value with model.addAttribute("modelAttrVar", youExtracteInstanceOfmodelAttrVar ); and then in the jsp take the values with ${modelAttrVar.var1}, please show the entire code and explain exactly what do you need to do – daN Mar 27 '16 at 14:14