【问题标题】:In Spring MVC How to get form object in JSP from controller ?在 Spring MVC 中,如何从控制器中获取 JSP 中的表单对象?
【发布时间】:2013-10-07 13:35:13
【问题描述】:

我正在尝试从控制器获取 jsp 中的表单对象。 JSP::updateuser.jsp

  <form name="user" action="updateuser" method="post">
        <ul>
        <li>
            <label>User Name</label> <input type="text" name="userName" />
        </li>
        <li>
            <label>Password</label> <input type="password" name="password" />
        </li>
        <li>
            <label>FirstName</label> <input type="text" name="firstName" />
        </li>
        <li>
            <label>LastName</label> <input type="text" name="lastName" />
        </li>
        </ul>
        <button type="submit">Update</button>
</form>

控制器::UpdateUserController.java

  @RequestMapping("/updateuser")
    public class UpdateUserController {
        @RequestMapping(method = RequestMethod.POST)
        public ModelAndView update(@ModelAttribute("user") User user,
                                       ModelMap model,HttpServletRequest request) {

            return new ModelAndView("updateuser","user",model);
        }
    }

在任何字段中添加值并单击更新按钮后。表单被提交并且在映射的 POJO 中我得到了价值。 现在我想在字段中显示这些值而不将所有字段一一添加到模型对象(即model.addAttribute("userName", user.getUserName())中)。我也不想使用 Spring 标签库。我如何填充表单中的所有值??

【问题讨论】:

    标签: jsp spring-mvc


    【解决方案1】:

    使用@ModelAttribute,您已经将User 对象放入Model,您无需再次将其添加到返回的ModelAndView。模型属性最终被添加为请求属性,因此您可以使用 EL 在jsp 中解析它们。

    <c:out value="${user.userName}" />  
    

    【讨论】:

      【解决方案2】:
        @RequestMapping(value="/update" , method=RequestMethod.POST)
           public ModelAndView submitupdatevalue(@ModelAttribute("update") User update){
            ModelAndView model=new ModelAndView("DisplayPageName");
             return model
          }
      
      // ......DisplayPageName.jsp
       <td> ${update.AttributName}</td>
        //.... it should be same AttributName as submitFormpage in  DisplayPageName 
      

      【讨论】:

        猜你喜欢
        • 2019-09-05
        • 2016-11-12
        • 1970-01-01
        • 2017-11-01
        • 2016-03-21
        • 1970-01-01
        • 2017-12-02
        • 2020-10-13
        • 1970-01-01
        相关资源
        最近更新 更多