thymeleaf的AbstractMarkupSubstitutionElementProcessor怎么用?

jackclchan 发布于 2015/11/23 17:08
阅读 763
收藏 0

在html里自定义了一个标签<s:allhidden />

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"

xmlns:th="http://www.thymeleaf.org"

xmlns:s="">

<head>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />

<title>button</title>

</head>

<body>

<form action="" th:action="@{list}" method="post"

th:object="${buttonForm}" id="buttonForm">

<div class="pagging">

<ul class="pager">

<li><a href="#" onclick="button.listConfirm('list');">上一页</a></li>

<li><a href="#" onclick="button.listConfirm('list');">下一页</a></li>

</ul>

</div>

<input type="hidden" name="action" id="action"></input>

<s:allhidden form="${buttonForm}" formId="buttonForm" />

</form>

<script type="text/javascript" th:inline="javascript">

//<![CDATA[

var button = {};

button.listConfirm = function(action) {

rbEx.form.confirm('buttonForm', action);

};

//]]>

</script>

</body>

</html>

FormAllInputHiddenElementProcessor继承了AbstractMarkupSubstitutionElementProcessor


@SingletonComponent

public class FormAllInputHiddenElementProcessor extends AbstractMarkupSubstitutionElementProcessor {


public FormAllInputHiddenElementProcessor() {

super("allhidden");

}


public FormAllInputHiddenElementProcessor(String elementName) {

super(elementName);

}


@Override

protected List<Node> getMarkupSubstitutes(Arguments arguments, Element element) {


Object form = getValue("form", element, arguments);

String formId = getStringValue("formId", element, arguments);

boolean nest = getNest(element, arguments);


final Element container = new Element("span");


ConversionService conversionService = getConversionService(arguments);

BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(form);

bw.setConversionService(conversionService);


Document document = arguments.getDocument();

PropertyDescriptor[] pds = bw.getPropertyDescriptors();

for (PropertyDescriptor pd : pds) {


String id = pd.getName();


if (isTargetProperty(bw, pd) && !alreadyDefineInputElement(document, formId, id)) {


Class<?> sourceType = pd.getPropertyType();

if (isTargetType(conversionService, sourceType, nest)) {


if (isPrimitiveArray(sourceType)) {

List<Element> hiddens = createPrimitiveArrayProperty(bw, pd, document, formId);

if (!ListUtils.isEmpty(hiddens)) {

for (Element hidden : hiddens) {

container.addChild(hidden);

}

}

} else if (isList(sourceType)) {

List<Element> hiddens = createListProperty(conversionService, bw, pd, document, formId);

if (!ListUtils.isEmpty(hiddens)) {

for (Element hidden : hiddens) {

container.addChild(hidden);

}

}

} else {

Element hidden = createSimpleProperty(bw, pd, document);

container.addChild(hidden);

}


}


}


}


final List<Node> nodes = new ArrayList<Node>();

nodes.add(container);


return nodes;

}


@Override

public int getPrecedence() {

return 1000;

}


protected Element createSimpleProperty(BeanWrapper bw, PropertyDescriptor pd, Document document) {


final Element hidden = new Element("input");

hidden.setAttribute("id", pd.getName());

hidden.setAttribute("name", pd.getName());

hidden.setAttribute("type", "hidden");

Object value = bw.getPropertyValue(pd.getName());

String result = bw.convertIfNecessary(value, String.class);

hidden.setAttribute("value", result);


return hidden;


}


protected List<Element> createPrimitiveArrayProperty(BeanWrapper bw, PropertyDescriptor pd, Document document,

String formId) {


List<Element> hiddens = new ArrayList<Element>();


Object value = bw.getPropertyValue(pd.getName());

Object[] array = (Object[]) value;


if (array == null || array.length == 0) {

return null;

}


String name = pd.getName();


int idx = 0;

for (Object o : array) {


StringBuilder sb = new StringBuilder();

sb.append(name).append("[").append(idx).append("]");


String id = sb.toString();


if (!alreadyDefineInputElement(document, formId, id)) {


final Element hidden = new Element("input");

hidden.setAttribute("id", id);

hidden.setAttribute("name", name);

hidden.setAttribute("type", "hidden");

String result = bw.convertIfNecessary(o, String.class);

hidden.setAttribute("value", result);


hiddens.add(hidden);

}


idx++;

}


return hiddens;


}


protected List<Element> createListProperty(ConversionService conversionService, BeanWrapper bw,

PropertyDescriptor pd, Document document, String formId) {


List<Element> hiddens = new ArrayList<Element>();


Object value = bw.getPropertyValue(pd.getName());

List<?> list = (List<?>) value;


if (ListUtils.isEmpty(list)) {

return null;

}


String name = pd.getName();


int idx = 0;

for (Object o : list) {


BeanWrapper elementBw = PropertyAccessorFactory.forBeanPropertyAccess(o);

elementBw.setConversionService(conversionService);

PropertyDescriptor[] pds = elementBw.getPropertyDescriptors();


StringBuilder sb = new StringBuilder();

sb.append(name).append("[").append(idx).append("].");


String baseName = sb.toString();


for (PropertyDescriptor elementPd : pds) {


if (conversionService.canConvert(elementPd.getPropertyType(), String.class)) {


String inputName = baseName + elementPd.getName();


if (!alreadyDefineInputElement(document, formId, inputName)) {

final Element hidden = new Element("input");

hidden.setAttribute("id", inputName);

hidden.setAttribute("name", inputName);

hidden.setAttribute("type", "hidden");

Object elementPropetyValue = elementBw.getPropertyValue(elementPd.getName());

String result = bw.convertIfNecessary(elementPropetyValue, String.class);

hidden.setAttribute("value", result);


hiddens.add(hidden);


}

}

}


idx++;

}


return hiddens;

}


protected boolean getNest(Element element, Arguments arguments) {


String nest = getStringValue("nest", element, arguments);


if (StringUtils.hasText(nest)) {

return Boolean.valueOf(nest).booleanValue();

}


return false;

}


protected boolean alreadyDefineInputElement(Document document, String formId, String id) {


Element element = null;

if (StringUtils.hasText(formId)) {

element = byIdOrNameWithFormId(document, id, formId, "input", "select", "button", "textarea");

} else {

element = byIdOrName(document, id, "input", "select", "button", "textarea");

}


return element != null;

}


protected boolean isTargetProperty(BeanWrapper bw, PropertyDescriptor pd) {

return true;

}


protected boolean isTargetType(ConversionService conversionService, Class<?> sourceType, boolean nest) {


if (isPrimitiveArray(sourceType) || isList(sourceType)) {

if (nest) {

return true;

} else {

return false;

}

}

if (conversionService.canConvert(sourceType, String.class)) {

return true;

}


return false;


}


protected boolean isList(Class<?> clazz) {

return List.class.isAssignableFrom(clazz);

}


protected boolean isPrimitiveArray(Class<?> clazz) {

return ClassUtils.isPrimitiveArray(clazz) || ClassUtils.isPrimitiveWrapperArray(clazz)

|| clazz.equals(String[].class);

}


protected ConversionService getConversionService(Arguments arguments) {

ApplicationContext context = ThymeleafUtils.getApplicationContext(arguments);

ConversionService c = context.getBean(ConversionService.class);

return c;


}


}

不知道为什么<s:allhidden />没起作用,是还需要什么配置吗?

加载中
返回顶部
顶部