简体   繁体   English

Spring 3 MVC:是否可以使用没有'commandName'绑定的弹簧形式?

[英]Spring 3 MVC: is it possible to have a spring form without 'commandName' binding?

My question is, Is it necessary to have a ' commandName ' binding for forms and ' path ' bindings for inputs? 我的问题是,是否有必要对表单进行' commandName '绑定,对输入进行' path '绑定?

I have a work flow which needs users to select data from a series of ajaxy select boxes. 我有一个工作流程,需要用户从一系列的ajaxy选择框中选择数据。 So the form doesn't exactly map to any model directly. 因此表单不能直接映射到任何模型。 It seems rather pointless to add another model just to accommodate this form, while everything on this form will be driven by ajax. 为了适应这种形式而添加另一个模型似乎毫无意义,而这个表单上的所有内容都将由ajax驱动。 Now I know I can always write a classic form and select tags, but I would like to take advantage of spring tags here for example: 现在我知道我总是可以写一个经典的表单并选择标签,但我想在这里利用spring标签,例如:

<form:options items="${list}" itemLabel="name" itemValue="id"/>

which allows me to easily populate by form items. 这使我可以轻松填写​​表单项。 And plus I would like to maintain uniformity in the project and use spring tags through. 而且我想保持项目的一致性并使用spring标签。

I would like to know thoughts and opinions on this topic. 我想知道关于这个主题的想法和意见。

Thanks! 谢谢!

PS: I am coming from ruby on rails background and am used to lot of syntactic sugar :P forgive me if the question sounds silly or answer obvious. PS:我来自铁轨背景上的红宝石,并习惯了大量的语法糖:如果问题听起来很愚蠢或回答明显,请原谅我。

I don't know if it's useful, but here it goes: 我不知道它是否有用,但它在这里:

If you don't set a 'commandName' in the form, the default value for this property will be set as 'command'. 如果未在表单中设置“commandName”,则此属性的默认值将设置为“command”。 So, if you don't set it, the bound data will have the name 'command'. 因此,如果您不设置它,绑定数据将具有名称'command'。

If you want, you can set it with the name of your bound data. 如果需要,可以使用绑定数据的名称进行设置。

========================================================================== ================================================== ========================

A solution without using data binding would be: 不使用数据绑定的解决方案是:

I would add a HttpServletRequest request parameter to the controller method and get the parameter like servlets do. 我会向控制器方法添加一个HttpServletRequest请求参数,并像servlet一样获取参数。

@Controller 
public class SomeController {

    @RequestMapping(value = "/formAction", method = RequestMethod.POST)
    public String controllerMethod(HttpServletRequest request){  
        // this way you get value of the input you want
        String pathValue1 = request.getParameter("path1");
        String pathValue2 = request.getParameter("path2");
        return "successfulView";
    }

}

PS.: the 'path1' and 'path2' are the path names you set in the inputs. PS:'path1'和'path2'是您在输入中设置的路径名。 I know it seems do not use the Spring properly, but its a hack that Spring let us to use. 我知道它似乎没有正确使用Spring,但它是Spring让我们使用的hack。

The form would be this way: 表格将是这样的:

<form:form method="post" action="/formAction">
    <form:input path="path1" />
    <form:input path="path2" />
    <input type="submit" value="Submit"/>
</form:form>

Hope its useful. 希望它有用。

If we are not adding the commandName to spring form tags an exception willl be raised as 如果我们不将commandName添加到spring form标签,则会引发异常
IllegalStateException: Neither BindingResult nor plain target object "command" available as request parameter... IllegalStateException:BindingResult和普通目标对象“command”都不可用作请求参数...

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

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