Not having much luck getting help from any of you Spring gurus on here. Hopefully this time I word the question better so it makes more sense. I’ve got a fairly basic Spring app using a Mysql database. Basically I just want to know the proper way to load data into a SELECT object on a form, but I need the entire process.
Here is a bit more depth. I’ve got a Job object that is stored in a Job table in the DB. It’s tied to a form that a user can fill out to populate the Job object. The Job object also has a Many to One relationship with a Filter object that is stored in a seperate table in the DB. So when the user is creating a Job object with the form, there is a SELECT box that is loaded with Filter objects for them to select. So far all of my code to do that works fine. The filter objects are displayed and then stored in the DB properly so I believe I have that part down right. The problem comes when a user attempts to edit a Job object. I load the form and all the data from the Job object is correctly pre-populated into the various boxes, except that the SELECT box is not automatically set to the Job objects stored choice. The SELECT object is getting loaded with Filter objects, but it’s always set to the first choice instead of say the 4th or whatever happens to be stored in the Job object. So clearly I’m not doing something right or missing some vital aspect of this process. If anyone can help me out here I would truly appreciate it. This is a bug in an application for work that I’m trying to fix and I cannot seem to find the solution. Anyway, below I’ve posted what I believe to be all the relevant code.
Job Object
@Entity
public class Job implements Runnable {
@Id @GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(name="filterId")
private Filter filter;
//getters and setters
}
Filter Editor (for translating the id passed from the form into a Filter object)
public class FilterEditor extends PropertyEditorSupport {
private FilterService filterService;
public FilterEditor(FilterService filterService) {
this.filterService = filterService;
}
public void setAsText(String value) {
long filterId = Long.parseLong(value);
Filter f = filterService.getById(provisionId);
setValue(f);
}
}
Job Controller (relevant parts)
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new DatePropertyEditor());
binder.registerCustomEditor(Filter.class, new FilterEditor(filterService));
}
@RequestMapping(value="/job", method=RequestMethod.GET)
public void showJobForm(Model model) {
model.addAttribute(new Job());
model.addAttribute("filters", filterService.getAll());
}
@RequestMapping(value="/job/edit", method=RequestMethod.POST)
public String editJob(@RequestParam("jobHiddenList") String list, Model model) {
log.info("Edit List: " + list);
Job job = jobService.getById(Long.valueOf(list.trim()));
model.addAttribute("job", job);
model.addAttribute("filters", filterService.getAll());
return "job";
}
And the relevant parts of the JSP
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<s:url value="/job" var="jobPost_url"/>
<s:url value="/job/provData" var="provData_url"/>
<sf:form method="POST" modelAttribute="job" dojoType="dijit.form.Form" action="${jobPost_url}">
<script type="dojo/method" event="onSubmit">
if (!this.validate()) {
return false;
}
return true;
</script>
<sf:hidden path="id" />
<table>
<tr><td align="right">Customer:</td><td>
<sf:input path="customer" dojoType="dijit.form.ValidationTextBox" trim="true" required="true"/><br/>
<sf:errors path="customer" cssClass="error"/>
</td></tr>
<tr><td align="right">Project:</td><td>
<sf:input path="project" dojoType="dijit.form.ValidationTextBox" trim="true" required="true"/><br/>
<sf:errors path="project" cssClass="error"/>
</td></tr>
<tr><td align="right">Date:</td><td>
<sf:input path="date" dojoType="dijit.form.DateTextBox" required="true"
constraints="{datePattern:'MMM d, y'}" /><br/>
<sf:errors path="date" cssClass="error"/>
</td></tr>
<tr><td align="right">Filter:</td><td><sf:select id="filter" path="filter" items="${filters}" itemValue="id"
itemLabel="programName"/></td>
</tr>
<tr><td colspan="2" align="right">
<button dojoType="dijit.form.Button" type="submit">Submit</button>
</td></tr>
</table>
</sf:form>
If I can clarify anything or provide anything else please do let me know. I have been trying to fix this for days and am at a loss. Thanks again.
Shot in the dark, because I’m not an expert in Spring MVC, and don’t know if you’re using Open Session In View or not.
If you’re not using Open Session In View, and if Spring uses
equalsto compare the selected filter with every filter in the select box, then the problem might come from the lack of proper redefinition of equals (and hashCode) in the Filter entity. Indeed, you’ll have two different Filter instances, having the same ID, but being different regardingequals.