views:

29

answers:

1

Hello All,

I'm new to Spring and little confused of how to use beans for populating and retrieving values to/from the view.

Here is what I'm doing now.

In the controller I'm initializing two beans xxxMain.java and xxxView.java. I'm using xxxMain.java to retrieve values FROM the view and xxxView.java for pre-populating the view. Here is my controller

@RequestMapping(value = "accounting", method = RequestMethod.GET)
public String showPage( Model model) {
XXXMain xxxMain = new XXXMain();
XXXView xxxView = new XXXView();


service.loadXXXForm(xxxMain, xxxView);

model.addAttribute("xxxMain", xxxMain);
model.addAttribute("xxxView", xxxView);


return "admin/xxx";
}

So as I'm using the xxxMain.java for retrieving I'm coding the jsp like this.

<form:form  modelAttribute="XXXMain" method="post" action="/app/home/save">
</form:form>

also I'm using Spring tags, like

<form:input path="name" size="15"/>

Now, when the fields in the view are empty, all is fine, but when I have to pre-populate the fields, I'm not sure what approach to take as

<form:input path="name" size="15"/>

does not has a value attribute to populate the field. So what I have done is populate the XXXMain.java class along with the XXXView.java class with the default values, as you can see in the controller code snippet. That way values are pre-populated when view is first loaded. But I'm not sure if I'm doing the right thing by populating the xxxMain.java file which in fact should only contain the user entered values.

How can I improve this design?

Thanks a lot.

Ravi

+1  A: 

Here is an example I wrote which might help steer you right.

James Earl Douglas
@jamestastic Nice blog! (+1)
Arthur Ronald F D Garcia
Thanks, I'm glad you like it!
James Earl Douglas