Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring控制器的数据绑定期间,如何将请求中的两个字符串参数绑定到一个字段类型中?_Java_Spring_Spring Mvc_Data Binding_Controller - Fatal编程技术网

Java 在spring控制器的数据绑定期间,如何将请求中的两个字符串参数绑定到一个字段类型中?

Java 在spring控制器的数据绑定期间,如何将请求中的两个字符串参数绑定到一个字段类型中?,java,spring,spring-mvc,data-binding,controller,Java,Spring,Spring Mvc,Data Binding,Controller,作为我的自定义类型(注意,没有设置器,因为这是一个不可变的值对象): 和我的控制器: @Controller class MyController { public ModelAndView processSubmittedForm(@Valid CustomType myObject, BindingResult result) {..} } 和我的jsp视图表单: <form> <input id="prefixField" name="prefix" val

作为我的自定义类型(注意,没有设置器,因为这是一个不可变的值对象):

和我的控制器:

@Controller
class MyController {
  public ModelAndView processSubmittedForm(@Valid CustomType myObject, BindingResult result) {..}
}
和我的jsp视图表单:

<form>
    <input id="prefixField" name="prefix" value="756." type="hidden">
    <input id="suffixField" name="suffix" type="text">
...
<form/>

...
考虑到此视图将发送两个POST参数
前缀
后缀
,假设Spring将使用非空值对其进行验证,我需要做什么才能将这两个参数绑定到单个对象
myObject

我可以通过自定义WebDataBinder、InitBinder、注册格式化程序、自定义编辑器或其他方式来实现这一点吗

非常感谢你的帮助

编辑: 以下是我在谷歌上搜索的相关文章,但没有找到一个与我自己的问题相匹配的解决方案:


@fabien7474,您可以使用PropertiesEditor,因为如果我们稍微看看HTTP协议,请求中的所有参数都是字符串,当您需要执行某种类型转换或验证时,spring会为您提供一种初始化绑定器的方法

例如:

@Controller
class MyController {
    public ModelAndView processSubmittedForm(@Valid MyObject myObject, BindingResult result) {..}

    @InitBinder
    public void initBinder (WebDataBinder binder) {
         binder.registerCustomEditor(MyObject.class, new CustomObjectEditor());
    }

}


class CustomObjectEditor {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
            MyObject ob = new MyObject();
            CustomType ct = new CustomType();
            ct.setValue(text);
            ob.setCustomType(ct);

            super.setValue(ob);

    }
}

通过这个例子,您可以看到一种类型的对话。希望这对您有所帮助。

@fabien7474,您可以使用PropertiesEditor,因为如果我们稍微看看HTTP协议,请求中的所有参数都是字符串,当您需要执行某种类型转换或验证时,spring会为您提供一种初始化绑定器的方法

<form>
<input id="prefixField" name="prefix" value="756." type="hidden">
<input id="suffixField" name="prefix" type="text">
例如:

@Controller
class MyController {
    public ModelAndView processSubmittedForm(@Valid MyObject myObject, BindingResult result) {..}

    @InitBinder
    public void initBinder (WebDataBinder binder) {
         binder.registerCustomEditor(MyObject.class, new CustomObjectEditor());
    }

}


class CustomObjectEditor {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
            MyObject ob = new MyObject();
            CustomType ct = new CustomType();
            ct.setValue(text);
            ob.setCustomType(ct);

            super.setValue(ob);

    }
}

通过这个例子,您可以看到一种类型的对话。希望这对您有所帮助。

Hi Tiarê:这行不通,因为setAsText只接受一个字符串。HTTP发送的参数的形式为:customType.prefix=756。&customType.suffix=29389&name=foo当您需要在URL中获取参数如?param1=info¶m2=info3时,您需要使用@RequestParam(),记住您可以定义required=false,并且可以使用上面的相同示例,因为他考虑要转换的对象类型,当绑定器中注册的对象类型相同时,spring将为您转换信息。HTTP发送的参数的形式为:customType.prefix=756。&customType.suffix=29389&name=foo当您需要在URL中获取参数如?param1=info¶m2=info3时,您需要使用@RequestParam(),记住您可以定义required=false,并且可以使用上面的相同示例,因为他考虑要转换的对象类型,当在活页夹中注册的对象类型相同时,spring将为您转换信息。这应该按原样工作-我认为您只需要为
customType
@Biju-Thx提供getter和setter。但这就是重点:我没有前缀和后缀的setter,因为这是一个不可变的值对象。我唯一拥有的是一个用于创建customType实例的构造函数。它应该按原样工作-我认为您只需要为您的
customType
@Biju-Thx提供getter和setter。但这就是重点:我没有前缀和后缀的setter,因为这是一个不可变的值对象。我唯一拥有的是一个用于创建CustomTypeInstance的构造函数谢谢您回答我的问题。请添加解释,使答案对其他人更有用。感谢您回答问题。请添加解释,使答案对其他人更有用。
<form>
<input id="prefixField" name="prefix" value="756." type="hidden">
<input id="suffixField" name="prefix" type="text">