Can I make HTTP POST request from Thymeleaf table in Spring Boot application

前端 未结 2 1276
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 03:57

I have a Thymeleaf template in a simple Spring Boot application. The template contains a list in a table as follows:

There are

2条回答
  •  情深已故
    2020-12-16 04:02

    Does anyone know if Thymeleaf allow a POST request per row of a table? Or do I have to write a simple HTML form per row?

    HTML doesn't support POST request with links and you have to use forms (as Rayweb_on explained). But Thymeleaf allows you to define custom tags which helps a lot :

    Edit
    

    ... which would generate following HTML (assuming jQuery is available) :

    Edit
    

    Custom tag definition (without error checking to keep it simple) :

    /**
     * Custom attribute processor that allows to specify which method (get or post) is used on a standard link.
     */
    public class LinkMethodAttrProcessor extends AbstractAttributeTagProcessor {
    
        private static final String ATTR_NAME = "linkMethod";
        private static final int PRECEDENCE = 10000;
    
        public LinkMethodAttrProcessor(final String dialectPrefix) {
            super(
                    TemplateMode.HTML, // This processor will apply only to HTML mode
                    dialectPrefix,     // Prefix to be applied to name for matching
                    null,              // No tag name: match any tag name
                    false,             // No prefix to be applied to tag name
                    ATTR_NAME,         // Name of the attribute that will be matched
                    true,              // Apply dialect prefix to attribute name
                    PRECEDENCE,        // Precedence (inside dialect's own precedence)
                    true);             // Remove the matched attribute afterwards
        }
    
        @Override
        protected void doProcess(final ITemplateContext context, final IProcessableElementTag tag,
                                 final AttributeName attributeName, final String attributeValue,
                                 final IElementTagStructureHandler structureHandler) {
    
            // get the method name (tag parameter)
            final IEngineConfiguration configuration = context.getConfiguration();
            final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);
            final IStandardExpression expression = parser.parseExpression(context, attributeValue);
            final String method = (String) expression.execute(context);
    
            // add custom javascript to change link method
            final String link = tag.getAttribute("href").getValue();
            final String action = "$('
    ').appendTo('body').submit(); return false;"; structureHandler.setAttribute("onclick", action); structureHandler.setAttribute("href", "#"); } }

    See the Thymelead documentation for example of how this custom attribute needs to be registered.

提交回复
热议问题