jodd form 改版成velocity

packagevelocity;

importjava.io.IOException;

importjava.io.StringWriter;

importjava.io.Writer;

importjodd.servlet.HtmlEncoder;

importjodd.servlet.HtmlTag;

importjodd.servlet.tag.FormTag.FieldResolver;

importjodd.util.StringUtil;

importorg.apache.velocity.context.Context;

importorg.apache.velocity.context.InternalContextAdapter;

importorg.apache.velocity.exception.MethodInvocationException;

importorg.apache.velocity.exception.ParseErrorException;

importorg.apache.velocity.exception.ResourceNotFoundException;

importorg.apache.velocity.runtime.directive.Directive;

importorg.apache.velocity.runtime.parser.node.Node;

publicclassFormDirectiveextendsDirective{

privatestaticfinalStringINPUT="input";

privatestaticfinalStringTYPE="type";

privatestaticfinalStringVALUE="value";

privatestaticfinalStringTEXT="text";

privatestaticfinalStringSELECT="select";

privatestaticfinalStringHIDDEN="hidden";

privatestaticfinalStringIMAGE="image";

privatestaticfinalStringPASSWORD="password";

privatestaticfinalStringCHECKBOX="checkbox";

privatestaticfinalStringTRUE="true";

privatestaticfinalStringCHECKED="checked";

privatestaticfinalStringRADIO="radio";

privatestaticfinalStringTEXTAREA="textarea";

privatestaticfinalStringname="name";

privatestaticfinalStringOPTION="option";

privatestaticfinalStringSELECTED="selected";

publicstaticStringrender(StringBufferhtml,finalContextcontext){

returnpopulateForm(html.toString(),newFieldResolver(){

publicObjectvalue(Stringname){

returnVelocityValueResolver.resolveProperty(name,context);

}

});

}

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

/**

*PopulatesHTMLform.

*/

protectedstaticStringpopulateForm(Stringhtml,FieldResolverresolver){

ints=0;

StringBuilderresult=newStringBuilder((int)(html.length()*1.2));

StringcurrentSelectName=null;

HtmlTagtag=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();

StringtagName=tag.getTagName();

//processendtags

if(tag.isEndTag()){

if(tagName.equals(SELECT)){

currentSelectName=null;

}

continue;

}

if(tagName.equals(INPUT)==true){

//INPUT

StringtagType=tag.getAttribute(TYPE);

if(tagType==null){

continue;

}

Stringname=tag.getAttribute(NAME);

if(name==null){

continue;

}

ObjectvalueObject=resolver.value(name);

if(valueObject==null){

continue;

}

Stringvalue=valueObject.toString();

tagType=tagType.toLowerCase();

if(tagType.equals(TEXT)){

tag.setAttribute(VALUE,value);

}elseif(tagType.equals(HIDDEN)){

tag.setAttribute(VALUE,value);

}elseif(tagType.equals(IMAGE)){

tag.setAttribute(VALUE,value);

}elseif(tagType.equals(PASSWORD)){

tag.setAttribute(VALUE,value);

}elseif(tagType.equals(CHECKBOX)){

StringtagValue=tag.getAttribute(VALUE);

if(tagValue==null){

tagValue=TRUE;

}

if(valueObject.getClass().isArray()){

//checkboxgroup

Stringvs[]=StringUtil.toStringArray(valueObject);

for(Stringvsk:vs){

if((vsk!=null)&&(vsk.equals(tagValue))){

tag.setAttribute(CHECKED);

}

}

}elseif(tagValue.equals(value)){

tag.setAttribute(CHECKED);

}

}elseif(tagType.equals(RADIO)){

StringtagValue=tag.getAttribute(VALUE);

if(tagValue!=null){

if(tagValue.equals(value)){

tag.setAttribute(CHECKED);

}

}

}

}elseif(tagName.equals(TEXTAREA)){

Stringname=tag.getAttribute(NAME);

ObjectvalueObject=resolver.value(name);

if(valueObject!=null){

tag.setSuffixText(HtmlEncoder.text(valueObject.toString()));

}

}elseif(tagName.equals(SELECT)){

currentSelectName=tag.getAttribute(NAME);

}elseif(tagName.equals(OPTION)){

if(currentSelectName==null){

continue;

}

StringtagValue=tag.getAttribute(VALUE);

if(tagValue!=null){

Objectvals=resolver.value(currentSelectName);

if(vals==null){

continue;

}

if(vals.getClass().isArray()){

Stringvs[]=StringUtil.toStringArray(vals);

for(Stringvsk:vs){

if((vsk!=null)&&(vsk.equals(tagValue))){

tag.setAttribute(SELECTED);

}

}

}else{

Stringvalue=StringUtil.toString(vals);

if(value.equals(tagValue)){

tag.setAttribute(SELECTED);

}

}

}

}

}

returnresult.toString();

}

@Override

publicStringgetName(){

return"form";

}

@Override

publicintgetType(){

returnBLOCK;

}

@Override

publicbooleanrender(InternalContextAdaptercontext,Writerwriter,

Nodenode)throwsIOException,ResourceNotFoundException,

ParseErrorException,MethodInvocationException{

Stringname=Utils.getRequiredArgument(context,node,0,getName());

NodeoutputNode=getOverrideNode(context,name);

if(outputNode==null){

outputNode=node.jjtGetChild(1);

}

StringWriterwrit=newStringWriter();

outputNode.render(context,writ);

writer.write(render(writ.getBuffer(),context));

returntrue;

}

privateNodegetOverrideNode(InternalContextAdaptercontext,Stringname){

return(Node)context.get(Utils.getOverrideVariableName(name));

}

}

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

properties加userdirective=velocity.FormDirective

packagevelocity;

importorg.apache.velocity.context.InternalContextAdapter;

importorg.apache.velocity.exception.ParseErrorException;

importorg.apache.velocity.runtime.parser.node.Node;

importorg.apache.velocity.runtime.parser.node.SimpleNode;

/**

*@authorbadqiu

*/

classUtils{

staticStringBLOCK="__vm_override__";

staticStringgetOverrideVariableName(Stringname){

returnBLOCK+name;

}

staticStringgetRequiredArgument(InternalContextAdaptercontext,Nodenode,intargumentIndex,Stringdirective)throwsParseErrorException{

SimpleNodesn_value=(SimpleNode)node.jjtGetChild(argumentIndex);

if(sn_value==null){

thrownewParseErrorException("requiredargumentisnullwithdirective:#"+directive+"()");

}

Stringvalue=(String)sn_value.value(context);

if(value==null){

thrownewParseErrorException("requiredargumentisnullwithdirective:#"+directive+"()");

}

returnvalue;

}

}

在页面中调用#form('')<inputtype="text"name="user"/>#end会自动赋值form元素

相关推荐