当前位置: 首页 > 知识库问答 >
问题:

如何将输入值赋给来自controller类的对象?

左丘成业
2023-03-14
@RequestMapping(value="/createRequest.html", method= RequestMethod.GET)
public ModelAndView add() throws ServiceException {
    DatabaseUser dbUser = new DatabaseUser();
    ModelAndView mav = new ModelAndView("index");
    mav.addObject("user", dbUser);
    return mav;
}

我只想从表单输入中获取字符串,将其分配给dbUser对象的address字段,并将该对象发送给save()方法。

@RequestMapping(value="/saveRequest.html", method=RequestMethod.POST)
public ModelAndView save(@ModelAttribute("user") DatabaseUser dbUser,
                         BindingResult bindingResult) throws ServiceException{
    if(bindingResult.hasErrors()){
        LOGGER.debug("Binding error count: " + bindingResult.getErrorCount());
    }
    if(dbUser.getBaseUser.getUserId()==0){
        BaseUser baseUser = new BaseUser();
        baseUser.setAddres(dbUser.getBaseUser().getAddress());
        baseUser.setEmail(dbUser.getBaseUser().getEmail());
    }
    return add();

我的ftl文件中试图满足这种情况的部分:

<div class="row">
    <div class="col-sm-12 col-lg-6">
        <form action="<@spring.url '/saveRequest.html' />"
             modelAttribute="user" method="post" id="form3">
             <input type="hidden" name="user" value="user"/>
<div class="form-group">
    <label for="address"><@spring.message "translationrequest.documentNumber" /></label>
    <input type="text" id="address" name="address" class="form-control"
          maxlength="250" value="${(getBaseuser().setAddress())!}"/>
</div>

共有1个答案

严琨
2023-03-14

以下是我在代码中注意到的事情:

请求映射映射此控制器的REST url,因此您需要将其修复为以下内容:

@RequestMapping(value="/save", method=RequestMethod.POST)

然后确保您的表单在用户提交时击中它:

<form action="<@spring.url '/save' />"
 类似资料: