使用 Thymeleaf 将字符串传递给 Spring Web Flow
Passing String to Spring Web Flow with Thymeleaf
我在将 String
从 <input>
传递到 webflow
时遇到了基本问题
<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>
百里香
<input type="text" th:field="${newTag}"/>
我遇到异常
The expression string to parse is required and must not be empty
所以我解决了我的问题,只是使用了这样的转换器
我自己创建了这样的转换器
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}" />
我在将 String
从 <input>
传递到 webflow
<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>
百里香
<input type="text" th:field="${newTag}"/>
我遇到异常
The expression string to parse is required and must not be empty
所以我解决了我的问题,只是使用了这样的转换器
我自己创建了这样的转换器
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}" />