ModelAttribute 值未填充值

ModelAttribute Values are not populating values

我有如下所示的表格

    <!DOCTYPE html>
<html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<head>
    <meta charset="utf-8" />
    <title>Dealer Details</title>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
    <link href="css/app.css" rel="stylesheet" />
    <script src="//code.jquery.com/jquery-2.0.3.min.js"></script>
    <script type="text/javascript">
        function onChange() {
            var value = ($("#jsonTextArea").text());
            alert(value);
            document.getElementById("jsonTextHidden").value = value;
        };
    </script>
</head>
<body class="body-bg">
    <form modelAttribute="jsonString" method="post" action="postHome">
        <div class="container">
            <div class="col-sm-8 col-sm-offset-2">
                <div class="row">
                    <div class="col-md-6 col-md-offset-3">
                        <div class="centerediv">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <h3 class="panel-title">Select Home</h3>
                                </div>
                                <div class="panel-body">
                                    <div class="form-group">
                                        <textarea rows="4" cols="40" name="jsonTextArea"></textarea>
                                    </div>
                                    <input class="btn btn-danger" type="button" value="Cancel">
                                    <input class="btn btn-success" type="submit" value="Ok" onclick="onChange();">
                                    <input type=hidden id="jsonTextHidden" name="jsonTextHiddenField" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</body>
</html>

我有一个控制器 class,如下所示。

@Controller
public class HomeController {

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String callHomePage() {
        return "home";
    }

    @POST
    @Consumes({MediaType.APPLICATION_XML})
    @RequestMapping(value = "/postHome")
    public ModelAndView postHomeList(@ModelAttribute("jsonString") HomeRequest request, @Context HttpServletRequest servletReq, @Context HttpServletResponse servletRes) {
        ModelAndView mav = null;
        System.out.println(request.getHomeId());
        return mav;
    }

}

在此 home.jsp 中有一个文本区域字段,我可以在其中输入一些值(json 字符串)。提交后 request.getHomeId() 的值为空。谁能帮我解决这个问题?

  1. 创建一个 HomeRequestDTO 例如

    public class HomeRequestDto {
      private Integer homeId;
      private String homeName;
     //your getter and setter
    }
    

2.

@RequestMapping(value = "/home", method = RequestMethod.GET)
public String callHomePage(final Model model) {
    HomeRequestDto home = new HomeRequestDto();
    home.setHomeName("Princess Home");
    home.setHomeId(123);
    model.addAttribute("homeRequest", home);
    return "home";
}

3。 你的 home.jsp

<form:form commandName="homeRequest" name="homeRequest" method="post" enctype="multipart/form-data" action="postHome" >

<spring:bind path="homeRequest.homeName">
    <form:textarea maxlength="100" path="${status.expression}"/>
</spring:bind>
<spring:bind path="homeRequest.homeId">
   <form:textarea maxlength="100" path="${status.expression}"/>
</spring:bind>              
<input type="submit" value="Submit" />          

4.on 提交你的控制器

@POST
@RequestMapping(value = "/postHome")
public String postHomeList(@ModelAttribute HomeRequestDto home,
        @Context HttpServletRequest servletReq, @Context HttpServletResponse servletRes) {
    System.out.println(home.getHomeId());
    System.out.println(home.getHomeName());
    return "home";
}