jodd form 改版成velocity

package velocity;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import jodd.servlet.HtmlEncoder;
import jodd.servlet.HtmlTag;
import jodd.servlet.tag.FormTag.FieldResolver;
import jodd.util.StringUtil;

import org.apache.velocity.context.Context;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;

public class FormDirective extends Directive {

private static final String INPUT = "input";
private static final String TYPE = "type";
private static final String VALUE = "value";
private static final String TEXT = "text";
private static final String SELECT = "select";
private static final String HIDDEN = "hidden";
private static final String IMAGE = "image";
private static final String PASSWORD = "password";
private static final String CHECKBOX = "checkbox";
private static final String TRUE = "true";
private static final String CHECKED = "checked";
private static final String RADIO = "radio";
private static final String TEXTAREA = "textarea";
private static final String NAME = "name";
private static final String OPTION = "option";
private static final String SELECTED = "selected";

public static String render(StringBuffer html,final Context context) {
return populateForm(html.toString(), new FieldResolver() {
public Object value(String name) {
return VelocityValueResolver.resolveProperty(name, context);
}
});
}

// ---------------------------------------------------------------- populate

/**
* Populates HTML form.
*/
protected static String populateForm(String html, FieldResolver resolver) {
int s = 0;
StringBuilder result = new StringBuilder((int) (html.length() * 1.2));
String currentSelectName = null;
HtmlTag tag = null;
while (true) {
if (tag != null) {
result.append(tag);
}
tag = HtmlTag.locateNextTag(html, s);
if (tag == null) {
result.append(html.substring(s));
break;
}
result.append(html.substring(s, tag.getStartIndex()));
s = tag.getNextIndex();

String tagName = tag.getTagName();
// process end tags
if (tag.isEndTag()) {
if (tagName.equals(SELECT)) {
currentSelectName = null;
}
continue;
}

if (tagName.equals(INPUT) == true) {
// INPUT
String tagType = tag.getAttribute(TYPE);
if (tagType == null) {
continue;
}
String name = tag.getAttribute(NAME);
if (name == null) {
continue;
}
Object valueObject = resolver.value(name);
if (valueObject == null) {
continue;
}
String value = valueObject.toString();
tagType = tagType.toLowerCase();

if (tagType.equals(TEXT)) {
tag.setAttribute(VALUE, value);
} else if (tagType.equals(HIDDEN)) {
tag.setAttribute(VALUE, value);
} else if (tagType.equals(IMAGE)) {
tag.setAttribute(VALUE, value);
} else if (tagType.equals(PASSWORD)) {
tag.setAttribute(VALUE, value);
} else if (tagType.equals(CHECKBOX)) {
String tagValue = tag.getAttribute(VALUE);
if (tagValue == null) {
tagValue = TRUE;
}
if (valueObject.getClass().isArray()) {
// checkbox group
String vs[] = StringUtil.toStringArray(valueObject);
for (String vsk : vs) {
if ((vsk != null) && (vsk.equals(tagValue))) {
tag.setAttribute(CHECKED);
}
}
} else if (tagValue.equals(value)) {
tag.setAttribute(CHECKED);
}
} else if (tagType.equals(RADIO)) {
String tagValue = tag.getAttribute(VALUE);
if (tagValue != null) {
if (tagValue.equals(value)) {
tag.setAttribute(CHECKED);
}
}
}
} else if (tagName.equals(TEXTAREA)) {
String name = tag.getAttribute(NAME);
Object valueObject = resolver.value(name);
if (valueObject != null) {
tag.setSuffixText(HtmlEncoder.text(valueObject.toString()));
}
} else if (tagName.equals(SELECT)) {
currentSelectName = tag.getAttribute(NAME);
} else if (tagName.equals(OPTION)) {
if (currentSelectName == null) {
continue;
}
String tagValue = tag.getAttribute(VALUE);
if (tagValue != null) {
Object vals = resolver.value(currentSelectName);
if (vals == null) {
continue;
}
if (vals.getClass().isArray()) {
String vs[] = StringUtil.toStringArray(vals);
for (String vsk : vs) {
if ((vsk != null) && (vsk.equals(tagValue))) {
tag.setAttribute(SELECTED);
}
}
} else {
String value = StringUtil.toString(vals);
if (value.equals(tagValue)) {
tag.setAttribute(SELECTED);
}
}
}
}
}
return result.toString();
}

@Override
public String getName() {
return "form";
}

@Override
public int getType() {
return BLOCK;
}

@Override
public boolean render(InternalContextAdapter context, Writer writer,
Node node) throws IOException, ResourceNotFoundException,
ParseErrorException, MethodInvocationException {
String name = Utils.getRequiredArgument(context, node, 0,getName());
Node outputNode = getOverrideNode(context,name);
if(outputNode == null) {
outputNode = node.jjtGetChild(1);
}
StringWriter writ = new StringWriter();
outputNode.render(context, writ);
writer.write(render(writ.getBuffer(), context));
return true;
}

private Node getOverrideNode(InternalContextAdapter context,String name) {
return (Node)context.get(Utils.getOverrideVariableName(name));
}
}


配置文件 org.springframework.web.servlet.view.velocity.VelocityConfigurer

properties 加 userdirective = velocity.FormDirective


package velocity;

import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.runtime.parser.node.Node;
import org.apache.velocity.runtime.parser.node.SimpleNode;

/**
* @author badqiu
*/
class Utils {

static String BLOCK = "__vm_override__";

static String getOverrideVariableName(String name) {
return BLOCK + name;
}

static String getRequiredArgument(InternalContextAdapter context,Node node,int argumentIndex,String directive) throws ParseErrorException {
SimpleNode sn_value = (SimpleNode)node.jjtGetChild(argumentIndex);
if ( sn_value == null){
throw new ParseErrorException("required argument is null with directive:#"+directive+"()");
}

String value = (String)sn_value.value(context);
if ( value == null){
throw new ParseErrorException("required argument is null with directive:#"+directive+"()");
}
return value;
}

}


在页面中调用#form('') <input type="text" name="user"/> #end 会自动赋值 form元素
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值