表单中的字段:选择不使用Spring MVC在表单提交的modelAttribute中设置

时间:2016-02-10 16:23:34

标签: spring forms modelattribute

表单类

 public class DocumentCaptureForm {
        private final static Logger logger = Logger.getLogger(DocumentCaptureForm.class);

        private String artifactCaptureJSON;
        private String isBackside;

        public String getArtifactCaptureJSON() {
            return artifactCaptureJSON;
        }

        public void setArtifactCaptureJSON(String artifactCaptureJSON) {
            this.artifactCaptureJSON = artifactCaptureJSON;
        }

        public String getIsBackside() {
            return isBackside;
        }

        public void setIsBackside(String isBackside) {
            this.isBackside = isBackside;
        }

JSP

<form:form modelAttribute="documentCaptureForm"  action="/capture" method="post" autocomplete="off">

        <div><form:hidden path="artifactCaptureJSON" id="artifactCaptureJSON"/></div>

        Document Side<form:select id="isBackside" path="isBackside">
            <form:option selected="selected" value="Front">Front</form:option>
            <form:option value="Back">Back</form:option>
        </form:select>

        <input type="submit"  id="capturesave" value="Save" />  
</form:form>

控制器

@RequestMapping(value="/capture", method=RequestMethod.POST)
public ModelAndView captureArtifacts(@PathVariable long applicationPKId,
        @ModelAttribute("documentCaptureForm") DocumentCaptureForm documentCaptureForm, BindingResult result, 
        HttpServletRequest request){
    logger.debug("Entering captureArtifacts ...");

    ModelAndView mav = new ModelAndView(DOCUMENT_CAPTURE_VIEW);
    mav.addObject("title", "Capture Document");
    ArtifactCaptureData artifactData = documentCaptureForm.convertToJava();

    boolean isBackside = documentCaptureForm.getIsBackside().equals("Back");

工件捕获JSON在用户在页面上执行某些操作后,在javascript函数中设置了它的值。将表单发布到控制器时,正确填充该值。然而,表单选择中的字段似乎被忽略。无论我尝试什么,isBackside都是null

1 个答案:

答案 0 :(得分:0)

<form:select>标记中,除了&#39;路径&#39;属性,您可以添加&#39;项目&#39;传递给它的属性包含多个选项的列表。所以你可以这样做:

<form:select path="isBackside" items="${['Front','Back']}" />

这是最简单的方法。