2014-12-03 59 views
0

我最近添加一些自定义的方言和处理器到我的春天启动的应用程序,但是当我把它们放在页面那样:Thymeleaf不评价定制方言/处理器

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns:form="http://form" xmlns:field="http://field"> 
<body> 
    <form:form> 
    ... 
    </form:form> 
</body> 
</html> 

并在打开的页面浏览器中,标签不会被评估为正确的值。该FormDialect是这样的:

public class FormDialect extends AbstractDialect { 

    public FormDialect() { 
    super(); 
    } 

    // 
    // All of this dialect's attributes and/or tags 
    // will start with 'hello:' 
    // 
    public String getPrefix() { 
    return "form"; 
    } 


    // 
    // The processors. 
    // 
    @Override 
    public Set<IProcessor> getProcessors() { 
    final Set<IProcessor> processor = new HashSet<IProcessor>(); 
    processor.add(new Form()); 
    return processor; 
    } 

} 

和FormProcessor是:

public class Form extends AbstractProcessor { 
    @Override 
    public ProcessorResult doProcess(Arguments arguments,ProcessorMatchingContext context,Node node) { 
    Element form = new Element("form"); 
    node.setProcessable(true); 
    node.getParent().insertBefore(node, form); 
    return ProcessorResult.OK; 
    } 

    @Override 
    public int getPrecedence() { 
    return 0; 
    } 

    @Override 
    public IProcessorMatcher<? extends Node> getMatcher() { 
    return new ElementNameProcessorMatcher("form"); 
    } 
} 

什么,我做错了什么?

+0

此示例(http://www.thymeleaf.org/doc/extendingthymeleaf.html#extrathyme-a-website-for-thymelands-football-league)处理<分数:标题attr =“...” – 2014-12-06 19:28:21

+0

@PatrickLC这个例子并不完全符合我的代码,因为使用AbstractMarkupSubstitutionElementProcessor和AbstractAttributeModifierAttrProcessor而不是AbstractProcessor。 – 2014-12-07 11:30:40

+0

也许,你忘了注册方言...就像:'SpringTemplateEngine engine = new SpringTemplateEngine(); engine.addDialect(新的FormDialect());' – 2014-12-08 14:47:50

回答

1

我得到了我的web配置与注册的方言:

@Configuration 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { 
    @Bean 
    public FormDialect formDialect() { 
     return new FormDialect(); 
    } 
} 

因为我与你的代码试图休息和方言还是登记:

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org" xmlns:form=""> 
    <form:form> 
    ... 
    </form:form> 
</html> 

方言:

import java.util.HashSet; 
import java.util.Set; 
import org.pgg.photodb.web.thymeleaf.processor.Form; 
import org.thymeleaf.dialect.AbstractDialect; 
import org.thymeleaf.processor.IProcessor; 

    public class FormDialect extends AbstractDialect { 
     @Override 
     public String getPrefix() { 
      return "form"; 
     } 

     @Override 
     public Set<IProcessor> getProcessors() { 
      final Set<IProcessor> processor = new HashSet<IProcessor>(); 
      processor.add(new Form()); 
      return processor; 
     } 
    } 

and Processor

import org.thymeleaf.Arguments; 
import org.thymeleaf.dom.Element; 
import org.thymeleaf.dom.Node; 
import org.thymeleaf.processor.AbstractProcessor; 
import org.thymeleaf.processor.ElementNameProcessorMatcher; 
import org.thymeleaf.processor.IProcessorMatcher; 
import org.thymeleaf.processor.ProcessorMatchingContext; 
import org.thymeleaf.processor.ProcessorResult; 

public class Form extends AbstractProcessor { 
    @Override 
    public ProcessorResult doProcess(Arguments arguments, ProcessorMatchingContext context, Node node) { 
     Element form = new Element("form"); 
     node.setProcessable(true); 
     node.getParent().insertBefore(node, form); 

     return ProcessorResult.OK; 
    } 

    @Override 
    public int getPrecedence() { 
     return 0; 
    } 

    @Override 
    public IProcessorMatcher<? extends Node> getMatcher() { 
     return new ElementNameProcessorMatcher("form"); 
    } 
}