Spring 3 MVC:是否可以有一个没有'commandName'绑定的spring表单? [英] Spring 3 MVC: is it possible to have a spring form without 'commandName' binding?

查看:96
本文介绍了Spring 3 MVC:是否可以有一个没有'commandName'绑定的spring表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,是否有必要为表单使用' commandName '绑定并为输入提供' path '绑定?

My question is, Is it necessary to have a 'commandName' binding for forms and 'path' bindings for inputs?

我有一个工作流程,需要用户从一系列ajaxy选择框中选择数据.因此,表单无法完全直接映射到任何模型.添加另一个模型来容纳此表单似乎毫无意义,而此表单上的所有内容都将由ajax驱动. 现在,我知道我总是可以编写经典表格并选择标签,但是我想在这里利用spring标签:

I have a work flow which needs users to select data from a series of ajaxy select boxes. 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. 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:

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

这使我可以轻松地按表单项进行填充.另外,我想保持项目的一致性,并使用spring标签.

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.

我想知道关于这个话题的想法和意见.

I would like to know thoughts and opinions on this topic.

谢谢!

PS:我来自于红宝石,并且习惯于使用很多语法糖:P如果问题听起来很傻或答案很明显,请原谅我.

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.

推荐答案

我不知道它是否有用,但是在这里:

I don't know if it's useful, but here it goes:

如果未在表单中设置"commandName",则此属性的默认值将设置为"command".因此,如果您不设置它,则绑定的数据将被命名为命令".

If you don't set a 'commandName' in the form, the default value for this property will be set as 'command'. So, if you don't set it, the bound data will have the name 'command'.

如果需要,可以使用绑定数据的名称进行设置.

If you want, you can set it with the name of your bound data.

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

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

不使用数据绑定的解决方案是:

我将向控制器方法添加HttpServletRequest请求参数,并像servlet一样获取参数.

I would add a HttpServletRequest request parameter to the controller method and get the parameter like servlets do.

@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.:"path1"和"path2"是您在输入中设置的路径名. 我知道似乎没有正确使用Spring,但是Spring让我们使用了它.

PS.: the 'path1' and 'path2' are the path names you set in the inputs. I know it seems do not use the Spring properly, but its a hack that Spring let us to use.

表单将是这样的:

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

希望它有用.

这篇关于Spring 3 MVC:是否可以有一个没有'commandName'绑定的spring表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆