Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Spring MVC中更改模型的显示名称_Spring_Spring Mvc_Spring Roo - Fatal编程技术网

如何在Spring MVC中更改模型的显示名称

如何在Spring MVC中更改模型的显示名称,spring,spring-mvc,spring-roo,Spring,Spring Mvc,Spring Roo,我正在处理一个SpringMVC项目,我不知道如何更改视图中模型的字符串表示形式 我有一个客户模型,它与工作订单模型有一对多的关系。在workorders/show.jspx上,客户显示为一个字符串,该字符串是连在一起的姓名、电子邮件地址和电话号码 我如何改变这个?我以为我可以在客户身上更改toString方法,但这不起作用。一个解决方案是将show()更改为WorkOrderController,将渲染视图标记映射到您希望看到的内容 @RequestMapping(value = "/{id}

我正在处理一个SpringMVC项目,我不知道如何更改视图中模型的字符串表示形式

我有一个客户模型,它与工作订单模型有一对多的关系。在workorders/show.jspx上,客户显示为一个字符串,该字符串是连在一起的姓名、电子邮件地址和电话号码


我如何改变这个?我以为我可以在客户身上更改toString方法,但这不起作用。

一个解决方案是将show()更改为WorkOrderController,将渲染视图标记映射到您希望看到的内容

@RequestMapping(value = "/{id}", produces = "text/html")
public String show(
    @PathVariable("id") Long id, 
    Model uiModel)
{
    final WorkOrder workOrder = WorkOrder.findWorkOrder(id);
    uiModel.addAttribute("workOrder", workOrder);
    uiModel.addAttribute("itemId", id);
    // Everything but this next line is just ripped out from the aspectJ/roo stuff.
    // Write a method that returns a formatted string for the customer name, 
    // and a customer accessor for WorkOrder.
    uiModel.addAttribute("customerDisplay", workOrder.getCustomer().getDisplayName());
    return "workorders/show";
}
在i18n/application.properties文件中为customerDisplay放置/定义标签

然后在show.jspx中,您可以使用以下内容访问映射:。。。(其他视图的技巧与此类似。)



我是新手,所以我希望看到更好的答案。

我们找到了一个很好的解决方案。ApplicationConversionServiceFactoryBean\u Roo\u ConversionService.aj中的所有模型都有toString()方法

您只需将所需模型的方法推送到ApplicationConversionServiceFactoryBean.java中并对其进行修改。在我的案例中,我添加了以下内容:

public Converter<Customer, String> getCustomerToStringConverter() {
    return new org.springframework.core.convert.converter.Converter<com.eg.egmedia.bizapp.model.Customer, java.lang.String>() {
        public String convert(Customer customer) {
            return new StringBuilder().append(customer.getId()).append(' ').append(customer.getFirstName()).append(' ').append(customer.getLastName()).toString();
        }
    };
}
公共转换器getCustomerToStringConverter(){ 返回新的org.springframework.core.convert.converter.converter(){ 公共字符串转换(客户){ 返回新的StringBuilder().append(customer.getId()).append(“”).append(customer.getFirstName()).append(“”).append(customer.getLastName()).toString(); } }; }
Spring在所有的视图页面上都使用了这个选项,因此这将改变整个应用程序中模型的字符串表示形式

我认为您不应该使用toString,而应该使用类似${customer.firstName}${customer.lastName}等的内容。。。但这当然取决于模型和视图的外观。
public Converter<Customer, String> getCustomerToStringConverter() {
    return new org.springframework.core.convert.converter.Converter<com.eg.egmedia.bizapp.model.Customer, java.lang.String>() {
        public String convert(Customer customer) {
            return new StringBuilder().append(customer.getId()).append(' ').append(customer.getFirstName()).append(' ').append(customer.getLastName()).toString();
        }
    };
}