简体   繁体   English

绑定形式——freemarker + Spring MVC

[英]Binding form - freemarker + Spring MVC

I have a problem with binding my object in ftl form.我在以 ftl 形式绑定 object 时遇到问题。 Here is my controller method:这是我的 controller 方法:

@RequestMapping(method = RequestMethod.POST)
public String saveConfigProperties(@ModelAttribute("configCmdList") ConfigCmdList configCmdList, BindingResult bindingResult) {
    configurationDao.setConfigValues(configCmdList.getConfigurations());
    return "config";
}

and here is part of my ftl:这是我的 ftl 的一部分:

<form action="" method="POST">
            <@spring.bind "configCmdList" />                    
            <#list configCmdList.configurations as config>
                ${config.name}
            </#list>                            
            <input type="submit" value="submit"/>
        </form>

I have an access to my list of objects which I sent previous using GET method in my ftl, but my object list is null after sending object without modifications back to controller. I have an access to my list of objects which I sent previous using GET method in my ftl, but my object list is null after sending object without modifications back to controller. I tried to bind my configCmdList.configurations and also bind separately each element of that list in loop but without success.我尝试绑定我的 configCmdList.configurations 并在循环中分别绑定该列表的每个元素,但没有成功。 What I'm missing?我错过了什么?

VairalPatel web page is down and I remember that he wrote good example about freemarker form and spring mvc. VairalPatel web 页面已关闭,我记得他写了关于 freemarker 表单和 spring mvc 的好例子。

Thanks in advance for your help.在此先感谢您的帮助。

Ok, I resolved an issue. 好的,我解决了一个问题。 I had to bind each list element and it parameters separately in loop using ${spring.status.expression} and ${spring.status.value}. 我必须使用$ {spring.status.expression}和$ {spring.status.value}分别循环绑定每个列表元素及其参数。 Here is my code: 这是我的代码:

<form action="" method="POST">  
            <#list configCmdList.configurations as config>    
                <@spring.bind path="configCmdList.configurations[${config_index}].id"/>
                <input type="hidden" name="${spring.status.expression}" value="${spring.status.value}" />

                <@spring.bind path="configCmdList.configurations[${config_index}].name"/>
                <input type="text" name="${spring.status.expression}" value="${spring.status.value}" />

                <@spring.bind path="configCmdList.configurations[${config_index}].value"/>
                <input type="text" name="${spring.status.expression}" value="${spring.status.value}" />
            </#list>                    
            <input type="submit" value="submit"/>
        </form>

Thank you for your help :) 谢谢您的帮助 :)

This is another way to write Caro's solution : 这是编写Caro解决方案的另一种方式:

<form action="" method="POST">  
        <#list configCmdList.configurations as config>    
            <input type="hidden" name="configurations[${config_index}].id" value="${config.id}"/>

            <input type="text" name="configurations[${config_index}].name" value="${config.name}"/>

            <input type="text" name="configurations[${config_index}].value" value="${config.value}" />
        </#list>                    
        <input type="submit" value="submit"/>
    </form>

Well this is not an answer for your post but I had similar issue so continuing to reply to have the reference attached for my question.好吧,这不是您帖子的答案,但我遇到了类似的问题,因此请继续回复以附上我的问题的参考资料。

Can we use square brackets inside spring bind?我们可以在 spring 绑定中使用方括号吗? I read in the documentation that square brackets can also be used as freemarker tags.我在文档中读到方括号也可以用作 freemarker 标记。 So when I use square brackets inside spring.bind tag it is considered as tag inside a tag?因此,当我在 spring.bind 标签内使用方括号时,它是否被视为标签内的标签? Even my editor colors say that by ending tag at square bracket.甚至我的编辑 colors 也通过在方括号中结束标记来表示。 Did it work for you?它对你有用吗? my editor image showing color difference after square bracket see attached image for color difference.我的编辑器图像在方括号后显示色差,请参阅附件图像以了解色差。

<@spring.bind path="adminAutoSaleProdForm.frm_bankProdDetails[${id}].frm_productId"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM