没有找到能够从类型[java.util.Set <! - ? - >]转换为类型[java.lang.String]的转换器

时间:2016-05-04 09:01:15

标签: java spring hibernate

我正在使用SPRING MVC 4和Hibernate 4.5。

sudo

Epic和PM之间存在ManyToMany关系。

**Model:**
Public class Epic implements java.io.Serializable{

    private int IDEPIC;

    @NotNull
    private String name;
    @Size(max=200)
    private String description;
    private int priority;
    @NotNull
    private int estimation;
    private Set<PM> projetMetiers;

    public Epic() {
        this.projetMetiers = new HashSet<PM>();
    }
   // getters & setters
}

我有addEpic.jsp和editEpic.jsp,其中包含以下代码片段,以显示添加/编辑Epic时要选择的Pms列表。

**Mapping:** <hibernate-mapping> <class name="com.model.Epic" table="Epic"> <id name="IDEPIC" column="IDEPIC" > <generator class="increment" /> </id> <property name="name" type="string" column="nameEpic" /> <property name="description" type="string" column="descriptionEpic" /> <property name="priority" type="int" column="priorityEpic" /> <property name="estimation" type="int" column="estimationEpic" /> <set name="projetMetiers" cascade="save-update" lazy="false" table="PM_Epic" > <key column="IDEPIC"/> <many-to`enter code here`-many column="IDPM" class="com.model.PM"/> </set> </class>`enter code here` </hibernate-mapping> 效果很好,我还有一个将字符串ID转换为PM对象的转换器。

addAction

查看:

**Controller:**
@RequestMapping("/edit/{id}")
public String editEpic(@PathVariable("id") int id, Model model) {

    model.addAttribute("epic", this.epicService.getEpicById(id));
    model.addAttribute("projets",this.pmService.listPMs());
        return "/epic/epicEdit";
}

** the converter* 
@InitBinder
public void initBinder(WebDataBinder binder) {
   binder.registerCustomEditor(PM.class, "projetMetiers", new PMEditor(pmService));
}

//PMEditor:
public class PMEditor extends PropertyEditorSupport{

    private PMService pmService;
    public PMEditor(PMService pmService) {
       this.pmService = pmService;
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
         if (text.equals("0")) {
            this.setValue(null);
         } else {
             PM pm = pmService.getPMById(Integer.parseInt(text));
             this.setValue(pm);
         }
     }

     @Override
     public String getAsText() {
        PM pm = new PM();
        if (this.getValue() != null) {
           pm = (PM) this.getValue();
        }
        return "";
      }
   }

Stacktrace:有根本原因

<div class="form-group">  
    <form:label path="projetMetiers" class="col-sm-2 control-label">
        <spring:message text="Projets métiers :"/>
    </form:label>
    <div class="col-sm-10"> 
        <form:select class="form-control" multiple="true" path="projetMetiers" itemLabel="name" itemValue="IDPM" > 
        </form:select>
       <springForm:errors path="projetMetiers" cssClass="error" />
    </div>
 </div>

<div class="form-group"> 
   <input type="submit" class="btn btn-default" value="<spring:message text="Modifier"/>" />
</div>

我无法获取所选对象,似乎spring无法将set转换为string。请帮助,谢谢。

0 个答案:

没有答案