PDA

View Full Version : Spring MVC - html:select equivalent


difranr
Mar 15th, 2006, 09:56 AM
I am starting to use Spring MVC as my main web page builder and love it so far. But I have run into a bit of a snag. I was looking for either a JSTL or Spring MVC equvialent to the Struts, <html:select> tag. is there one or can I just use the struts tages and let Spring MVC bind it properly to my form object? Also on a similiar note, it would be good to see an example of how to make this work, meaning how to populate it with your form object. My best guess is that the form object, must be populated with your drop down lists as List object and what I use for the drop downs is utilized to populate it.

kenevel
Mar 15th, 2006, 12:45 PM
Hi mate,

There are a couple of things to disentangle here. What you see in your JSP can be a combination of reference data (eg: a list to populate a drop down list etc) and form data.

Consider the following:

public class Employee {
private Long id;
private String firstName;
private String surname;
public Employee(){
}
public void setId(Long id){
this.id = id;
}
public Long getId(){
return id;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getFirstName(){
return firstName;
}
public void setSurname(String surname){
this.surname = surname;
}
public String getSurname(){
return surname;
}
}

And a form object:

public class EmployeeForm {
public static final String FORM_NAME = "employeeForm";
private Employee employee;
private String rank;
public EmployeeForm(){
}
public void setEmployee(Employee employee){
this.employee = employee;
}
public Employee getEmployee(){
return employee;
}
public void setRank(String rank){
this.rank = rank;
}
public String getRank(){
return rank;
}
}

Controller code:

public class MyController extends SimpleFormController {
public MyController(){
setCommandClass(EmployeeForm.class);
setCommandName(EmployeeForm.FORM_NAME);
}

protected Map referenceData(HttpServletRequest request) {
List myList = new ArrayList(2);
myList.add("Manager");
myList.add("Underling");
request.setAttribute("listValues",myList);
}
// ... rest DIY
}

Now for a bit of JSP

<form action="/path/toController.do" method="POST">
<string:bind path="employeeForm.employee.name">
<input type="hidden" name="${status.expression}" value="${status.value}"/>
</spring:bind>
<table>
<string:bind path="employeeForm.employee.name">
<c:if test="${not empty status.errorMessage}">
<tr>
<td/>
<td><c:out value="${status.errorMessage}"/></td>
</tr>
</c:if test>
<tr>
<td>First name:</td>
<td><input type="text" name="${status.expression}" value="<c:out value="${status.value}"/>"/></td>
</tr>
</string:bind>
... <!-- do surname tags too, as above -->
<spring:bind path="employeeForm.rank">
<tr>
<td>Rank:</td>
<td><select name="${status.expression}">
<c:forEach var="rank" items="${listValues}">
<option value="${rank}"<c:if test="${rank == status.value}"> selected="selected"</c:if>>${rank}</option>
</c:forEach>
</select></td>
</tr>
</spring:bind>
</form>


Hope this helps get you started.

Mike

difranr
Mar 15th, 2006, 01:45 PM
Mike,

That definitely helps!

Thanks!

Ron

difranr
Mar 15th, 2006, 03:33 PM
Mike,

One last question, in looking at the API for referenceData(), is it better to return the map or just issue the request.setAttribute() as you have done?

Thanks,

Ron

dhewitt
Mar 15th, 2006, 04:30 PM
I would say return the map - this is the intent of the API anyway. The map represents objects which will be bound as the model of the ModelAndView returned by spring. As a result, they will end up in the request anyway.

kenevel
Mar 15th, 2006, 05:08 PM
Hi difranr,

Yup, dhewitt's right - return the map - sorry, my mistake!

Cheers

Mike

sethladd
Mar 16th, 2006, 01:08 AM
It should be noted that Spring 2.0 includes handy JSP taglibs for XHTML form elements. So, Spring will finally have tags like <html:input> and <html:select>. They are currently in the latest Spring 2.0 release, however there are a few bugs. The next release should have everything ironed out. Keep an eye out for them, they really clean up your JSP pages.

Having said that, consider using Velocity or Freemarker. They have had macros for Spring + XHTML form elements for quite some time.

marianogo
Jan 10th, 2007, 01:34 PM
Im using freemarker 2.3.8 y spring 2.0 but I want to use the tags of JSP in my freemarker , How Can I achive that?

to import the tags in my FTL but from struts i have to do something like


<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>


I want to do the same but i want to import TLDS from Spring.
there are libraries in Springframeworks
like org.springframework.web.servlet.tags.form that talks about form tags but I dont know how to use it.