Back Forum Reply New

Spring MVC

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, lt;html:selectgt; 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.

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:

Code:
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:

Code:
public class EmployeeForm {   public static final String FORM_NAME = quot;employeeFormquot;;   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:

Code:
public class MyController extends SimpleFormController {   public MyController(){       setCommandClass(EmployeeForm.class);       setCommandName(EmployeeForm.FORM_NAME);   }      protected Map referenceData(fromServletRequest request) {       List myList = new ArrayList(2);       myList.add(quot;Managerquot;);       myList.add(quot;Underlingquot;);       request.setAttribute(quot;listValuesquot;,myList);   }   // ... rest DIY
}
Now for a bit of JSP

Code:
lt;form action=quot;/path/toController.doquot; method=quotOSTquot;gt;
lt;string:bind path=quot;employeeForm.employee.namequot;gt;   lt;input type=quot;hiddenquot; name=quot;${status.expression}quot; value=quot;${status.value}quot;/gt;
lt;/spring:bindgt;
lt;tablegt;
lt;string:bind path=quot;employeeForm.employee.namequot;gt;
lt;c:if test=quot;${not empty status.errorMessage}quot;gt;   lt;trgt;       lt;td/gt;       lt;tdgt;lt;cut value=quot;${status.errorMessage}quot;/gt;lt;/tdgt;   lt;/trgt;
lt;/c:if testgt;   lt;trgt;       lt;tdgt;First name:lt;/tdgt;       lt;tdgt;lt;input type=quot;textquot; name=quot;${status.expression}quot; value=quot;lt;cut value=quot;${status.value}quot;/gt;quot;/gt;lt;/tdgt;   lt;/trgt;
lt;/string:bindgt;
... lt;!-- do surname tags too, as above --gt;
lt;spring:bind path=quot;employeeForm.rankquot;gt;   lt;trgt;       lt;tdgt;Rank:lt;/tdgt;       lt;tdgt;lt;select name=quot;${status.expression}quot;gt;
lt;c:forEach var=quot;rankquot; items=quot;${listValues}quot;gt;    lt;option value=quot;${rank}quot;lt;c:if test=quot;${rank == status.value}quot;gt; selected=quot;selectedquot;lt;/c:ifgt;gt;${rank}lt;/optiongt;
lt;/c:forEachgt;lt;/selectgt;lt;/tdgt;   lt;/trgt;
lt;/spring:bindgt;
lt;/formgt;
Hope this helps get you started.

Mike

Mike,

That definitely helps!

Thanks!

Ron

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

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.

Hi difranr,

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

Cheers

Mike

It should be noted that Spring 2.0 includes handy JSP taglibs for XHTML form elements.  So, Spring will finally have tags like lt;html:inputgt; and lt;html:selectgt;.  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.

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 likeCode:
lt;%@taglib uri=quot;/WEB-INF/struts-bean.tldquot; prefix=quot;beanquot;%gt;
I want to do the same but i want to  import TLDS from Spring.
there are libraries in Springframeworks
like org..web.servlet.tags.form that talks about form tags but I dont know how  to use it.
¥
Back Forum Reply New