0

我有一个与 CampaignCities 城市建立 OneToOne 关系的实体营销活动。反过来,CampaignCities 包含一个 Set 城市;

竞选实体

    @Entity 
    @javax.persistence.Table(uniqueConstraints={@UniqueConstraint(columnNames={"name","company_id"}), @UniqueConstraint(columnNames={"id"})})
    public class Campaign implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        private Long id;

        @NotEmpty
        @Size(min=1, max=100)
        private String name;

        private Date startDate;

        private Date endDate;

        @Valid
        private Deal deal;

        @Valid
        private Company company;

        @OneToOne
        private CampaignCities cities = new CampaignCities();

CampaignCities 实体

@Entity
public class CampaignCities {
    private long id;
    private Set<City> cities = new HashSet<City>();

    @Id
    @javax.persistence.GeneratedValue(strategy=GenerationType.AUTO)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @OneToMany
    public Set<City> getCities() {
        return cities;
    }

    public void setCities(Set<City> cities) {
        this.cities = cities;
    }
}

市实体:

@Entity
public class City implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String name;
    private Long id;

    @javax.persistence.Id
    @javax.persistence.GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

我的新活动控制器

@SessionAttributes(value={"campaign", "campaignCities"})
@Controller
public class NewCampaignController {
    //private static final Logger logger = LoggerFactory.getLogger(NewDealController.class);

    @Autowired
    private CampaignManager campaignManager;
    @Autowired
    private CityManager cityManager;
    @Autowired
    SimpleDateFormat dateFormat;
    @Autowired
    CustomDateEditor dateEditor;

    @RequestMapping(value = "campaign/new", method = RequestMethod.GET)
    public String showForm(Map<String, Object> model) {
        //List<Campaign> campaigns = campaignManager.getCampaigns();
        Campaign campaignForm = new Campaign();
        CampaignCities cities = new CampaignCities();
        cities.setCities(new HashSet<City>(cityManager.getCity()));
        //campaignForm.setCities(cities);
        model.put("campaignCities", cities);
        model.put("campaign", campaignForm);
        return "campaign/new";
    }

    @RequestMapping(value = "campaign/new", method = RequestMethod.POST)
    public String processForm(@Valid Campaign campaignForm, BindingResult result, Map<String,Object> model) {
        new CampaignValidator().validate(campaignForm, result);
        if (result.hasErrors()) {
            return "campaign/new";
        }
        this.campaignManager.saveCampaign(campaignForm);
        model.put("campaign", campaignForm);
        model.put("campaigns", this.campaignManager.getCampaigns());
        return "campaign/added";
    }

我已经能够让活动以表格形式呈现,并且我已经使用以下方法成功呈现了城市列表:

<form:checkboxes items="${campaignCities.cities}" path="cities" itemLabel="name" itemValue="id" delimiter="<br/>" />

但是,当我提交表单时,我收到以下验证错误。

字段“城市”的对象“活动”中的字段错误:拒绝值 [2,1];代码 [typeMismatch.campaign.cities,typeMismatch.cities,typeMismatch.com.groupdealclone.app.domain.CampaignCities,typeMismatch]; 参数 [org.springframework.context.support.DefaultMessageSourceResolvable:代码 [campaign.cities,cities];论据 []; 默认消息[城市]];默认消息 [无法将类型“java.lang.String[]”的属性值转换为属性“城市”所需的类型“com.groupdealclone.app.domain.CampaignCities”;嵌套异常是 java.lang.IllegalStateException:无法将类型 [java.lang.String[]] 的值转换为属性“城市”所需的类型 [com.groupdealclone.app.domain.CampaignCities]:找不到匹配的编辑器或转换策略]

我试图找出一种在 SpringMVC 3 中处理这个问题的方法,但我已经被困了一天多没有成功。我只是想要在表单上选中的列表或集合或城市提交给控制器并添加到活动中。如何解决可以将返回的 String[] 转换为 List 或 Set of Cities 的转换问题。

我正在做的项目是一个公共的 GitHub 项目,如果你喜欢这个项目叫做Group-Deal-Clone ,你可以下载源代码并使用 Maven 进行设置

4

1 回答 1

1

差不多 2 天后,答案比我预期的要简单。感谢这个线程,我被引导到答案。

在我的 NewCampaignController 中,我做了:

@InitBinder
public void initBinder(WebDataBinder binder) {
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, dateEditor);

    binder.registerCustomEditor(CampaignCities.class, "cities", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            String [] ids = text.split(",");
            CampaignCities cities = null;
            for(String id:ids){
                if(cities == null)
                    cities = new CampaignCities();
                City city = cityManager.getCity(new Long(id));
                if(city != null)
                    cities.getCities().add(city);

            }
            if(cities != null){
                cities.setId(null);
                setValue(cities);
            }
        }
    });
于 2011-08-08T15:30:17.907 回答