2014-03-07 70 views
11

我完全改變這個問題,因爲它的一部分在Avnish的大力幫助下回答了here! 湯姆把我送到了正確的方向,所以謝謝湯姆!百里香多次選編輯

我的問題是,我不知道如何告訴Thymeleaf在編輯時預先選擇對象元素。

讓我告訴你:

looks like this

此解決方案:

 <select class="form-control" id="parts" name="parts" multiple="multiple" > 
      <option th:each="part : ${partsAtribute}" 
        th:selected="${servisAttribute.parts.contains(part)}" 
        th:value="${part.id}" 
        th:text="${part.name}">Part name</option> 
     </select> 

我已經試過這樣:

  <select class="form-control" th:field="*{parts}" multiple="multiple" > 
      <option th:each="part : ${partsAtribute}" 
        th:field="*{parts}" 
        th:value="${part.id}" 
        th:text="${part.name}">Part name</option> 
      </select> 

沒有工作。我也試過這個:

  <select class="form-control" th:field="*{{parts}}" multiple="multiple" > 
      <option th:each="part : ${partsAtribute}" 
        th:field="*{parts}" 
        th:value="${part.id}" 
        th:text="${part.name}">Part name</option> 
      </select> 

也沒有工作。我已經嘗試從選項標記中刪除th:field =「* {parts}」,結果相同。

如果我將th:value更改爲$ {part},但它不會返回字符串像[2,4,5,6,...],但部分實例像[部分@ 43b45j,部分@ we43y7,...] ...

更新:我只是注意到,這隻適用於一部分選擇:

<select class="form-control" th:field="*{parts}" multiple="multiple" > 
      <option th:each="part : ${partsAtribute}" 
        th:field="*{parts}" 
        th:value="${part.id}" 
        th:text="${part.name}">Part name</option> 
      </select> 

如果選擇多個部件,這是行不通的......

回答

11

在Thymeleaf論壇討論之後,我在 https://github.com/jmiguelsamper/thymeleafexamples-selectmultiple

實現一個完整的工作的例子,我想,以你的最終代碼的唯一問題是,你必須使用雙括號的語法來調用conversionService:

th:value="${{part}}" 

在Part類中實現正確的equals()和hashcode()方法以確保正確比較也很重要。

我希望我的例子能夠幫助其他未來有類似問題的用戶。

+4

這是必須的:「在Part類中實現正確的equals()和hashcode()方法以確保正確比較也很重要。」 – Blejzer

7

使用01時,您不需要正常。 Thymeleaf會自動檢查每個<option>值在<select>,哪怕是multiple

問題在於價值。您正在迭代parts,但每個選項的值爲part.id。因此,您正在比較零件的實例與零件的ID(據我所知)。

但是,百里香也考慮到PropertyEditor(它重用org.springframework.web.servlet.tags.form.SelectedValueComparator)的情況。

這將用於比較對象與選項的值。它會將對象轉換爲它們的文本值(它們的id)並將其與該值進行比較。

<select class="form-control" th:field="*{parts}" multiple="multiple" > 
     <option th:each="part : ${partsAttribute}" 
       <!-- 
        Enable the SpringOptionFieldAttrProcessor . 
        th:field value of option must be equal to that of the select tag 
       --> 
       th:field="*{parts}" 
       th:value="${part.id}" 
       th:text="${part.name} + ${part.serial}">Part name and serial No.      
     </option> 
</select> 

屬性編輯器

定義零件一個PropertyEditor。在比較這些值時以及將這些部分綁定到表單時,將調用PropertyEditor。

@Controller 
public class PartsController { 
    @Autowired 
    private VehicleService vehicleService; 

    @InitBinder(value="parts") 
    protected void initBinder(final WebDataBinder binder) { 
     binder.registerCustomEditor(Part.class, new PartPropertyEditor()); 
    } 

    private static class PartPropertyEditor extends PropertyEditorSupport { 
     @Override 
     public void setAsText(String partId) { 
      final Part part = ...; // Get part based on the id 
      setValue(part); 
     } 

     /** 
     * This is called when checking if an option is selected 
     */ 
     @Override 
     public String getAsText() { 
      return ((Part)getValue()).getId(); // don't forget null checking 
     } 
    } 
} 

另請參閱ConvertingPropertyEditorAdapterConverter現在在Spring中更優選在conversionService中註冊的實例。

+0

謝謝你爲這個偉大的答案。我已經做出調整並實施了您的建議,當然它仍然不起作用。我沒有得到任何錯誤或任何東西,只是...你能告訴我在哪裏尋找轉換器的例子或文檔,我發現所有的是Spring API ...只是這樣我才能理解這個定製自定義編輯器如何做它是什麼應該這樣做... – Blejzer

+0

我最終實現了轉換器。它花了一些時間,但它的工作。這次真是萬分感謝。我嘗試使用自定義屬性編輯器,它也可以正常工作。我不能讓百里香去工作。我將編輯我的問題,以便您和其他人可以看到...... – Blejzer

0
<select th:field="*{groupId}" > 
    <option th:each="group :${grouptype}" 
      th:value="${{group.groupId}}" 
      th:text="${group.Desc}"> 

    </option> 
</select> 

簡單的選擇例如

0

這個工作對我來說:

獸醫有很多特色菜。

控制器:

@RequestMapping(value = "/vets/{vetId}/edit", method = RequestMethod.GET) 
public ModelAndView editVet(@PathVariable("vetId") int ownerId/*, Model model*/) { 

    ModelAndView mav = new ModelAndView("vets/vetEdit"); 

    mav.addObject("vet", this.vets.findById(ownerId)); 

    mav.addObject("allSpecialties", this.specialities.findAll());   

    return mav;  
} 

視圖(使用日:選擇):

<select id="specialities" class="form-control" multiple> 
      <option th:each="s : ${allSpecialties}"           
        th:value="${s.id}" 
        th:text="${s.name}" 
        th:selected="${vet.specialties.contains(s)}"> 
      </option> 
     </select> 

視圖(使用日:字段):

<form th:object="${vet}" class="form-horizontal" id="add-vet-form" method="post"> 
    <div class="form-group has-feedback"> 
     <select th:field="*{specialties}" class="form-control" multiple> 
      <option th:each="s : ${allSpecialties}"           
        th:value="${s.id}" 
        th:text="${s.name}" 
        >    
      </option> 
     </select>   
    </div> 

我必須定義Specialty findOne(@Param("id") Integer id) throws DataAccessException;在SpecialtyRepository,否則會引發以下異常:「java.lang.IllegalStateException:Repository doe沒有發現一種方法!「

package org.springframework.samples.petclinic.vet; 

import java.util.Collection; 

import org.springframework.dao.DataAccessException; 
import org.springframework.data.repository.Repository; 
import org.springframework.data.repository.query.Param; 
import org.springframework.transaction.annotation.Transactional; 

public interface SpecialtyRepository extends Repository<Specialty, Integer> { 

    @Transactional(readOnly = true) 
    Collection<Specialty> findAll() throws DataAccessException; 

    Specialty findOne(@Param("id") Integer id) throws DataAccessException; 
} 
0

我是這樣做的:

<select th:field="*{influenceIds}" ID="txtCategoryName" class="m-wrap large" multiple="multiple"> 
    <option th:each="influence : ${influences}" th:value="${influence.get('id')}" th:text="${influence.get('influence')}" ></option> 
</select> 

我的DTO包含:

private List<String> influenceIds;