ModelAttribute值不填充值

时间:2015-08-04 12:02:29

标签: java jsp spring-mvc modelattribute

我的表格如下所示

    <!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>

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

@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中,我可以输入一些textarea字段(json string)。提交后,request.getHomeId()的值将变为null。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

  1. 创建HomeRequestDTO 例如

    public class HomeRequestDto {
      private Integer homeId;
      private String homeName;
     //your getter and setter
    }
    
  2. 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.提交您的控制器

    @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";
    }