开发者

Spring-Hibernate: How to define default values when using related objects

I have a Spring 3 MVC app sing Hibernate as ORM. All works fine. But a question:

I have a web interface to create new objects, say a new CUSTOMER. Each CUSTOMER has a STATUS. The STATUS-Table has predefined values. The CUSTOMER-Table has a status_id field to joind the information.

When d开发者_高级运维isplaying a list of CUSTOMERS, I can access the CUSTOMER.STATUS property and get the right object baed on the id.

BUT: How can I define a standard status for NEW CUSTOMERS? I have a web interface with a dropdown.

<form:select path="status">
<form:option value="0" label="- Please Select -" />
<form:options items="${customerStatuses}" itemValue="id" />
</form:select>

This always shows the "Please Select" text because it seems like the value is always 0??? How can I change this to my individually selected standard status?


You controller needs to take care of this. I would do it like this:

@RequestMapping(value = { "/new-customer-form" })
public String showNewCustomerForm(ModelMap model) {
    Customer newCustomer = new Customer();
    newCustomer.setStatus(statusRepository.getDefaultStatus());
    model.addAttribute("customer", newCustomer);
    model.addAttribute("customerStatuses", statusRepository.getAllStatuses());
    return "customer-form";
}

As a matter of fact, the "- Please Select -" label will not show anymore because your status is always filled.

(I hope I understood your question correctly this time.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜