使用Thymeleaf将字符串传递给Spring Web Flow

时间:2015-11-28 14:58:57

标签: java thymeleaf spring-webflow

我将String<input>传递到我的视图中的基本问题

<view-state id="tagsSubflow" view="places/add-flow/tags">
    <var name="newTag" class="java.lang.String"/>
    <on-entry>
        <set name="viewScope.newTag" value="new java.lang.String()"/>
    </on-entry>
    <transition on="next" to="save"/>
    <on-exit>
        <evaluate expression="place.addTag(newTag)"/>
    </on-exit>
</view-state>

我的例子<input> Thymeleaf

<input type="text" th:field="${newTag}"/>

我得到了例外 The expression string to parse is required and must not be empty

1 个答案:

答案 0 :(得分:0)

所以我解决了我的问题只是使用了像这样的转换器

我已经创建了自己的转换器

public class TagsUtils implements Converter {

public static Set<Tag> getTagsFromString(String tags) {
    Set<Tag> tagSet = new HashSet<>();
    String[] splittedTags = tags.split(",");
    for (String stringTag : splittedTags) {
        stringTag = stringTag.trim();
        Tag tag = new Tag(stringTag);
        tagSet.add(tag);
    }
    return tagSet;
}

@Override
public Class<?> getSourceClass() {
    return String.class;
}

@Override
public Class<?> getTargetClass() {
    return Set.class;
}

@Override
public Object convertSourceToTargetClass(Object o, Class<?> aClass) throws Exception {
    return getTagsFromString((String) o);
}
}

我在视图状态中添加了一个活页夹

    <binder>
        <binding property="tags" converter="tagConverter"/>
    </binder>

并创建配置

@Service("MyConversionService")
public class ApplicationConversionService extends DefaultConversionService {

public ApplicationConversionService() {
    addDefaultConverters();
    addDefaultAliases();
    addConverter("tagConverter", new TagsUtils());
}
}

,最后一步是将此自定义转换服务添加到webflow配置,如此

<flow:flow-builder-services id="flowBuilderServices"
                            conversion-service="MyConversionService"
                            view-factory-creator="mvcViewFactoryCreator"
                            development="true"/>

我的html输入看起来就像这个<input class="form-group" type="text" th:field="${place.tags}" />