SpringMVC的form:form表单的使用

  为什么要使用SpringMVC的form:form表单,有两个原因:一是可以更加快捷的完成表单的开发,比如会替你做好数据类型装换等本来需要你自己动手的工作。其次就是能够更加方便的实现表单回显。
首先要在顶部加上这样一行,用以引入form:form的类库。
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

剩下的页面部分就是一个简单的form:form表单。把代码的解释直接写到注释里。

<!-- 类似普通的form表单,其中modelAttribute是用来绑定一个类;即是form表单提交后对应的实体类。 -->
		<form:form action="addSto" method="post" modelAttribute="storageInformation">
			<!-- 这个格式只不过是在常用标签的前面加了一个form:,然后path属性要对应此便签所对应的绑定类的相应属性 ;下边其他便签的使用都和这个类似,不另外解释-->
			<form:input path="teacherId" name="teacherId" value="教师编号" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '教师编号';}"/>
			<form:input path="subjectId" name="subjectId" value="科目编号 " onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '科目编号';}"/>
			<form:input path="storageName"  name="storageName" value="名称" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '名称';}"/>
			<form:radiobutton path="category" name="category" value="0" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '类别';}" />科目共享&nbsp;
			<form:radiobutton path="category" name="category" value="1" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = '类别';}" />个人独有
			<br>
			<div class="forgot">
				
		    	<input type="submit" value="创建" >
		    </div>
		</form:form>


可以通过 modelAttribute 属性指定绑定的模型属性,若没有指定该属性,则默认从 request 域对象中读取 command 的表单 bean。如果该属性值也不存在,则会发生错误。
最后还有一点要注意的是:如果从一个页面跳转到绑定类的jsp页面则需要进行给其提供一个form:form对应的绑定类的对象。(不确定这点说的是不是准确)。可以在后台的跳转逻辑这样写:
StorageInformation storageInformation= new StorageInformation();
		return new ModelAndView("creat_storage").addObject(storageInformation);


也就是给其提供一个空的绑定类的对象,这样就能避免出现上面的问题。

猜你喜欢

转载自251098199.iteye.com/blog/2370937