嗨,我完全来自移动背景,所以Spring对我来说是新手,我目前有一个名为as的表单 这里是BusinessForm的内容
public class BusinessForm
{
private String selectedBusinessId; //getters and setters included
private List businessNameList; //getters and setters included - list of Business class Objects
private List businessIdList; //getters and setters included - lisy of Business class Objects
//Business class defined below
}
这是我的控制器
@Controller
public class HomeController{
@RequestMapping(value = "/showHome", method = RequestMethod.GET)
@ExceptionHandler({CutsomException.class})
public ModelAndView showHome()
{
//Init BusinessForm class, its defined above...
//set values of businessNameList...
//set values of businessIdList...
BusinessForm businessForm = new BusinessForm();
businessForm.setBusinessNameList(....);
businessForm.setBusinessIdList(....);
return ModelAndView("MyView","businessForm", businessForm)
}
}
以下是我的观点(我将仅显示表格,以避免显示其他所有内容)
MyView.jsp
<form:form action="blah" method="post" modelAttribute="businessForm">
<form:select path="selectedBusinessId">
<form:option value="">Select ID</form:option>
<form:options items="${businessForm.businessIdList}" item/>
</form:select>
</form:form>
所以现在我看到的businessIdList高于form的代码:options items属性是&#34; Business&#34;对象和Business对象具有私有变量businessName和businessId with getter and setters
public class Business
{
private String businessId; //with getters and setter
private String businessName; //with getters and setters
}
所以在上面的表格中,一旦我打开下拉列表,它实际上显示了一个列表,但该String只是Business类的toString()函数。所以我的下拉项目看起来像com.xxx.Business@c291000。 忽略业务类的toString(),如何让表单在表单列表的下拉列表中显示实际的businessId。原因是我想获得另一种形式:选择并显示另一个businessName列表。请帮忙。感谢。
答案 0 :(得分:2)
您需要更改
<form:select path="selectedBusinessId">
<form:option value="">Select ID</form:option>
<form:options items="${businessForm.businessIdList}" item/>
</form:select>
到
<form:select path="selectedBusinessId">
<form:option value="">Select ID</form:option>
<form:options items="${businessForm.businessIdList}" itemValue="businessId" itemLabel="businessName" />
</form:select>