springboot接收多对象_springmvc接受多对象的处理spring mvc感觉很好⽤,尤其是对接收对象参数的⾃动绑定⾮常简便,但对于同时传多个对象时有些困扰。同时项⽬并没有直接使⽤spring的formtag。从⽹上学来的多对象传值,⾃⼰优化了下,原⽂不到出处了这⾥记录下。
⾸先声明⼀个注解类,⽤于对传值对象的声明
/**
* 处理spring mvc 对象绑定注解
* @author lee
*
*/
mvc的controller
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBean {
String value() default "_def_param_name";
}
然后是⼀个对WebArgumentResolver的实现类,对象参数绑定主要是这个类来处理
/**
* 对象传值的参数绑定处理
* @author lee
*
*/
public class BeanArgumentResolver implements WebArgumentResolver {
@SuppressWarnings("rawtypes")
public Object resolveArgument(MethodParameter param, NativeWebRequest request){
RequestBean requestBean = ParameterAnnotation(RequestBean.class);
try{
if (requestBean != null) {
String _param = requestBean.value();
if (_param.equals("_def_param_name")) {
_param = ParameterName();
}
Class clazz = ParameterType();
Object object = wInstance();
HashMap paramsMap = new HashMap();
Iterator itor = ParameterNames();
while (itor.hasNext()) {
String webParam = (String) ();
String[] webValue = ParameterValues(webParam);
List webValueList = new ArrayList();
for(int i = 0;i
if(webValue[i]!=null&&!"".equals(webValue[i])){
webValueList.add(webValue[i]);
}
}
if (webParam.startsWith(_param)&&!webValueList.isEmpty()) {
paramsMap.put(webParam, Array(new String[webValueList.size()]));
}
}
BeanWrapper obj = new BeanWrapperImpl(object);
for (String propName : paramsMap.keySet()) {
Object propVals = (propName);
String[] props = propName.split("\\.");
if (props.length == 2) {
obj.setPropertyValue(props[1], propVals);
} else if (props.length == 3) {
Object tmpObj = PropertyValue(props[1]);
if (tmpObj == null)
obj.setPropertyValue(props[1], PropertyType(props[1]).newInstance());
obj.setPropertyValue(props[1] + "." + props[2], propVals);
}
}
return object;
}
}catch(Exception e){
e.printStackTrace();
}
return WebArgumentResolver.UNRESOLVED;
}
}
两个类写好后对mvc配置⽂件进⾏配置
接下来就是使⽤了mvc的controller⽅法如下
@RequestMapping(value="/saveEvent")
@ResponseBody
public AjaxResult saveEvent(@RequestBean Event event){
event =eventService.saveTemporaryEvent(event);
return AjaxResult.objectResult(event);
}
页⾯form表单代码
客户代码:客户电话:
————————————————————-分割线—————————————————————-
经过⼀段时间的使⽤,发现此⽅法对于传值有很多限制,复杂情况处理很不理想。特意补充官⽅推荐⽤法。controller端直接使⽤注解声明
@Controller
@RequestMapping("/demo/formbean")
public class FormBeanController {
@Autowired
private FormBeanService formBeanService;
@InitBinder("formBean1")
public void initBinderFormBean1(WebDataBinder binder) {
binder.setFieldDefaultPrefix("formBean1.");
}
@InitBinder("formBean2")
public void initBinderFormBean2(WebDataBinder binder) {
binder.setFieldDefaultPrefix("formBean2.");
}
@RequestMapping("/save12")
@ResponseBody
public ModelAndView save12(FormBean1 formBean1, FormBean2 formBean2){
formBean2 = formBeanService.saveFromBean12(formBean1, formBean2);
ModelAndView mav = new ModelAndView();
mav.setViewName("redirect:/demo/formbean/edit12/"+Id()); return mav;
}
}
html⽅式相差不⼤
FB1编号
FB1名称
FB2编号
FB2名称
提交
官⽅⽅法是⽤性更强,对于复杂情况如集合多级嵌套对象等处理完美,计较推荐。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。