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>
如果用逗号(,)分隔,textarea中的单词将单独添加到模型中的数组中。
但是,我想使用其他分隔符,如“;”,“:”和新行。我怎么能这样做?
答案 0 :(得分:0)
我有解决方案:
创建一个实现Converter
的新类然后添加此方法
List<String> result = new ArrayList<String>();
for (String part : source.split("[\n\t:;,]")) {
if (!part.trim().isEmpty()) {
result.add(part.trim());
}
}
return result;
然后将其添加到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>