Pregunta

Spring Model:

@Service
public class Test {
    private List<String> keyphrases;
}

Thymeleaf html:

<form role="form" action="#" method="post" th:object="${test} th:action="@{/test}">
    <div class="form-group">
        <label for="words" class="control-label">Keyphrases</label>
        <textarea type="text" class="form-control" id="keyphrases" placeholder="Zoektermen" th:field="*{keyphrases}"/>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary pull-right" name="add">
            Go
        </button>
    </div>
</form>

The words in the textarea will be separately added to the array in the model, if they are separated with the comma (,).

However, i would like to use Other separators like " ; ", " : " and a new line. How can I do this?

¿Fue útil?

Solución

I have the solution:

make a new class that implements Converter

then add this method

 List<String> result = new ArrayList<String>();
    for (String part : source.split("[\n\t:;,]")) {            
        if (!part.trim().isEmpty()) {
            result.add(part.trim());
        }
    }
    return result;

then add this to the spring servlet:

<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="YOUR.PACKAGE.TextAreaSetConverter" />
        </list>
    </property> 
</bean>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top