1

嗨,我完全来自移动背景,所以 Spring 对我来说是新的,我目前有一个名为 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)
    }
}

这是我的观点(我将只显示表格以避免显示其他所有内容)

我的视图.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 属性是“Business”对象的列表,Business 对象具有私有变量 businessName 和 businessId 以及 getter 和 setter

public class Business
{
  private String businessId; //with getters and setter 
  private String businessName; //with getters and setters 
}

所以在上面的表格中,一旦我打开下拉列表,它实际上会显示一个列表,但那个 String 只不过是 Business 类的 toString() 函数。所以我的下拉项目看起来像 com.xxx.Business@c291000。如果不覆盖 Business 类的 toString(), 我如何让我的表单在表单列表的下拉列表中显示实际的 businessId。原因是我想获得另一种形式:选择并显示另一个业务名称列表。请帮忙。谢谢。

4

1 回答 1

3

你需要改变你的

<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>
于 2014-09-30T00:55:13.270 回答