2014-12-02 2 views
0

스프링 폼 바인딩 문제가 발생했습니다. 두 모델 카테고리 및 제품이 있습니다. 양식을 제출하는 동안스프링 폼 바인딩 드롭 다운 개체

<form:select path="category" > 
<form:options items="${categoryList}"/> 
</form:select> 

: 컨트롤러에서

@Entity 
@Table(name="PRODUCT") 
public class Product 
{ 
@Id 
@GeneratedValue 
private long productID; 

@ManyToOne 
@JoinColumn(name="categoryID") 
private Category category; 

//Getters and setters  

} 


@Entity 
@Table(name = "CATEGORY") 
public class Category { 

    @Id 
    @GeneratedValue 
    private long categoryID; 

    private String categoryName; 
} 

내가 코드 아래 사용하여 JSP 페이지에서 카테고리의 값 드롭 다운을 채울 수 있어요 제품 페이지를

@RequestMapping(value = "/productpage", method=RequestMethod.GET) 
    private ModelAndView getAddProductPage(){ 
     ModelAndView modelAndView = new ModelAndView("add-product","product",new Product()); 
     Map<Category,String> categoriesMap = new HashMap<Category, String>(); 
     List<Category> categories = categoryService.getAllCategories(); 
     if(categories != null && !categories.isEmpty()) 
     { 
      for(Category category : categories) 
      { 
       categoriesMap.put(category, category.getCategoryName()); 
      } 
     } 
     modelAndView.addObject("categoryList",categories); 
     return modelAndView; 
    } 

을 추가 렌더링하기 400 오류가 발생했습니다. 클라이언트가 보낸 요청의 구문이 올바르지 않습니다. Failed to convert property value of type 'java.lang.String' to required type 'com.example.model.Category' for property 'category'. 각 옵션 카테고리의 페이지 소스를 올바르게 보면 할당되어 있습니다. 그러나 봄을 잘못 던지는 것을 이해하지 못한다. 도움이 필요하다. 미리 감사드립니다!

for(Category category : categories) 
{ 
    categoriesMap.put(category.geCategorytId(), category.getCategoryName()); 
} 

및보기의 변화 :

답변

0

당신은 당신의 컨트롤러의 액션에서 다음과 같이 변경해야

<form:select path="category.categoryID" > 

셀렉트 드롭 다운이 표시 텍스트로 카테고리 이름을 갖게됩니다 및 카테고리 ID를 값으로 사용하십시오.

+0

감사하지만 그것은 작동하지 않았다는 메시지와 함께 잘못을 던졌습니다 속성 유형의 값 필수 유형 'java.lang.String의'긴 변환하지 못했습니다 'for 속성'category.categoryID '; – Rohit

0

이것은 나를 위해 일했습니다!

<form:select path="category.categoryID" > 
<form:options items="${categoryList}" itemValue="categoryID" /> 
</form:select> 
0
<form:select path="category"> 
    <c:forEach items="${categoryList}" var="category"> 
    <form:option value="${category}">${category.categoryName}</form:option> 
    </c:forEach> 
</form:select> 

또는 응답

<form:select class="form-control" path="site"> 
    <form:option value="-1">Select...</form:option> 
    <form:options items="${categoryList}" itemValue="categoryID" itemLabel="categoryName"/>      
</form:select> 
관련 문제