zoukankan      html  css  js  c++  java
  • 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);
    
    

    也就是给其提供一个空的绑定类的对象,这样就能避免出现上面的问题。
  • 相关阅读:
    矩阵学习摘记,欢迎指正
    [poj1363]Rails_模拟_栈
    JLOI2018 记
    [poj3321]Apple Tree_dfs序_树状数组
    [poj3974]Palindrome_Manacher
    [poj1062]昂贵的聘礼_最短路_离散化
    STL:字符串用法详解
    C++ Primer 有感(管理类的指针成员)
    C++ Primer 有感(标准库set类型)
    C++ Primer 有感(标准库pair)
  • 原文地址:https://www.cnblogs.com/mingbai/p/6885956.html
Copyright © 2011-2022 走看看