PDA

View Full Version : Binding command object in view


ksriram
May 19th, 2007, 02:18 AM
Hi,
I want to populate the drop down menu with data from data base on the page load.
I am able to pass the data to the page.But the drop won menu is not populating.

Here is my controller code :

public StateEditController() {
setCommandClass(State.class);
}

protected Object formBackingObject(HttpServletRequest request) throws Exception {
List stateNameList = new ArrayList();
State state = new State();
if(request.getParameter("state_name_eng") != null) {
logger.info("State Name"+request.getParameter("state_name_eng"));
}
else {
logger.info("Inside Else !!!!!!");
//state.getState_name_eng();
stateNameList = editStateModel.editData();
state.setStateList(stateNameList);
}

logger.info("State Name !!!!!!!!!!"+state.getStateList().size());
request.setAttribute("state_name_eng", state.getStateList());
return state;
}

public void setEditStateModel(EditStateModel editStateModel) {
this.editStateModel = editStateModel;
}


And this is my jsp code :

<spring:bind path="command">

<form:select path="state_name_eng" size="1">

<c:forEach items="${command.state}" var="name">
<c:choose>
<c:when test="${not empty name.state_name_eng}" >

<c:out value="${name.state_name_eng}"/>
<br/>
</c:when>
<c:otherwise>
N/A<br/>
</c:otherwise>
</c:choose>
</c:forEach>

</form:select>
</spring:bind>


And is this the right way to go about populating data in the controls while the page is loaded.

Kindly give in ur suggestions.

ksriram
May 19th, 2007, 03:53 AM
I learn t that i should use <form: options tag> to iterate a list.

What is itemsValue and itemsLabel in that tag ?
Can any one explain that to me.Because my data in not populated in the drop down menu.

ksriram
May 19th, 2007, 04:08 AM
Can anyone help me out in this ?

nekoval
May 19th, 2007, 04:58 AM
I learn t that i should use <form: options tag> to iterate a list.

No, you don't need <form:options> to iterate a list.

Just wondering why you are using command.state, when your command is State. For some reason, you are putting data into request attributes, rather than using ModelAndView. I think that reading Spring MVC reference documentation on data binding is a good start.

ksriram
May 19th, 2007, 07:24 AM
Hi,
I changed the code.Now the value is coming to the page.
I am not able to use the command object to iterate the value.
As off now i just used a scriplet and passed that list to the options tag.
This is the view code

<form:form name="editStateForm" commandName="state">

<%
List list = (List) request.getAttribute("state_name_eng");
System.out.println("State Size !!!!!"+list.size());
%>
<spring:bind path="state_name_eng">

<form:select path="state_name_eng" size="1" onchange="getStateID();">
<form:options items="<%=list%>"></form:options>
</form:select>
</spring:bind>

My requirement is :
I want to populate a drop down menu when the page loads.The data for the drop down menu is got from the db.And When the user selects a name from the drop down menu, data for that name should be fetched and returned to the same page.

For example i need to bring both the state id and state name to the drop down menu i.e. when the user clicks the state name, actaully the state id should be passed as a request.
How can i populate both the id and name in the same drop down.

Kindly give in ur suggestion

noon
May 19th, 2007, 09:27 AM
seems like you are a bit los with the "spring architechture". Just read and study the Spring reference manual and you'll learn tonns of stuff :)
And by browsing older threads in this forum you can find answers to many of your questions.

Just jumping into the "full speed spring train" isn't always the best way to study Spring Framework.