• Spring MVC----JSP标签库


     Spring MVC 表单标签库:

    声明表单标签库

      在使用 SpringMVC 的时候我们可以使用 Spring 封装的一系列表单标签,这些标签都可以访问到 ModelMap 中的内容。我们需要先在 JSP 中声明使用的标签,具体做法是在 JSP 文件的顶部加入以下指令:

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    

    表单标签

    <form:form />
    

      使用 Spring MVC 的 form 标签主要有两个作用,第一是它会自动的绑定来自 Model 中的一个属性值到当前 form 对应的实体对象,默认是 command 属性,这样我们就可以在 form 表单体里面方便的使用该对象的属性了。第二是它支持我们在提交表单的时候使用除 GET 和 POST 之外的其他方法进行提交,包括 DELETE 和 PUT 等。

    <form:form action="formTag/form.do" method="post">  
        <table>  
            <tr>  
                <td>Name:</td><td><form:input path="name"/></td>    //path支持级联属性
            </tr>  
            <tr>  
                <td>Age:</td><td><form:input path="age"/></td>  
            </tr>  
            <tr>  
                <td colspan="2"><input type="submit" value="提交"/></td>  
            </tr>  
        </table>  
    </form:form>  
    

      

    注意:

    上面的示例中form表单中没有modelAttribute属性,默认从request 域对象中读取command的表单bean,如果该属性值也不存在,则会发生错误。

    所以我们需要使用modelAttribute属性指定绑定的模型属性。

    html

    <form:form action="/useradd" method="post" modelAttribute="user"> //通过modelAttribute绑定模型 

    Controller

    1、如果没有@ModelAttribute,我们需要手动将user对象添加到request请求域中

        public String userform(Model model){
            User user = new User();
            model.addAttribute("user",user);
            return "userForm";
        }

    2、如果有@ModelAttribute (利用他,在请求一开始,就将user对象放到request请求域中)

        @ModelAttribute
        public void userinit(@RequestParam(value = "id",required = false) String  id, Model model){
            if (id==null){
                User user = new User("小明");
                model.addAttribute("abc",user);
            }else {
                //从数据库通过id获取user对象,在添加到request请求域中
            }
        }
        @RequestMapping(value = "/useradd",method = RequestMethod.GET)
        public String userform(){
            return "userForm";
        }
    

      

    文本框

    使用 <form:input path="name" /> 标签来渲染一个 HTML 文本框,等同于:
    <input id="name" name="name" type="text" value=""/>
    

      

    密码框

    使用 <form:password path="password" /> 标签来渲染一个 HTML 密码框,等同于:
    <input id="password" name="password" type="password" value=""/>
    

      

    文本域

    使用 <form:textarea path="address" rows="5" cols="30" /> 标签来渲染一个 HTML 文本域,等同于:
    <textarea id="address" name="address" rows="5" cols="30">
    

      

    复选框

    使用 <form:checkbox path="receivePaper" /> 标签来渲染一个 HTML 复选框,等同于:

    <input id="receivePaper1" name="receivePaper" type="checkbox" value="true"/>
    <input type="hidden" name="_receivePaper" value="on"/>
    

      

    复选框(多选)

    使用 <form:checkboxes items="${webFrameworkList}" path="favoriteFrameworks" /> 标签来渲染一个 HTML 多选复选框,等同于:

    <span>
        <input id="favoriteFrameworks1" name="favoriteFrameworks" type="checkbox" value="Spring MVC" checked="checked"/>
        <label for="favoriteFrameworks1">Spring MVC</label>
    </span>
    <span>
        <input id="favoriteFrameworks2" name="favoriteFrameworks" type="checkbox" value="Struts 1"/>
        <label for="favoriteFrameworks2">Struts 1</label>
    </span>
    <span>
        <input id="favoriteFrameworks3" name="favoriteFrameworks" type="checkbox" value="Struts 2" checked="checked"/>
        <label for="favoriteFrameworks3">Struts 2</label>
    </span>
    <span>
        <input id="favoriteFrameworks4" name="favoriteFrameworks" type="checkbox" value="Apache Wicket"/>
        <label for="favoriteFrameworks4">Apache Wicket</label>
    </span>
    <input type="hidden" name="_favoriteFrameworks" value="on"/>
    

      

    单选按钮

    使用 <form:radiobutton /> 标签来渲染一个 HTML 单选按钮,等同于:
    <form:radiobutton path="gender" value="M" label="男" />
    <form:radiobutton path="gender" value="F" label="女" />
    
    <input id="gender1" name="gender" type="radio" value="M" checked="checked"/><label for="gender1">男</label>
    <input id="gender2" name="gender" type="radio" value="F"/><label for="gender2">女</label>
    

      

    单选按钮(多选)

    使用 <form:radiobuttons path="favoriteNumber" items="${numbersList}" /> 标签来渲染一个 HTML 多项单选按钮,等同于:

    <span>
        <input id="favoriteNumber1" name="favoriteNumber" type="radio" value="1"/>
        <label for="favoriteNumber1">1</label>
    </span>
    <span>
        <input id="favoriteNumber2" name="favoriteNumber" type="radio" value="2"/>
        <label for="favoriteNumber2">2</label>
    </span>
    <span>
        <input id="favoriteNumber3" name="favoriteNumber" type="radio" value="3"/>
        <label for="favoriteNumber3">3</label>
    </span>
    <span>
        <input id="favoriteNumber4" name="favoriteNumber" type="radio" value="4"/>
        <label for="favoriteNumber4">4</label>
    </span>
    

      

    下拉列表

    使用 <form:select /><form:option /><form:options /> 标签来渲染一个 HTML 下拉列表

    Controller

    model.addAttribute("addressList",addressList);
    model.addAttribute("user",user);
    

      

    <form:form action="/useradd" method="post" modelAttribute="user">
        <form:input path="username"></form:input>
        <form:select path="address.id">
            <form:options items="${addressList}" itemLabel="addrname" itemValue="id"/> //itemLabel指addressList中的每一个元素.addrname(同理itemValue)
        </form:select>
        <input type="submit" value="提交">
    </form:form>

      

    下拉列表(多选)

    使用 <form:select /> 标签及其属性 multiple=true 来渲染一个 HTML 多选下拉列表,等同于:

    <form:select path="skills" items="${skillsList}" multiple="true" />
    

      

    <select id="skills" name="skills" multiple="multiple">
       <option value="Struts">Struts</option>
       <option value="Hibernate">Hibernate</option>
       <option value="Apache Wicket">Apache Hadoop</option>
       <option value="Spring">Spring</option>
    </select>
    <input type="hidden" name="_skills" value="1"/>
    

      

    隐藏字段域

    使用 <form:hidden path="id" value="1000"/> 标签来渲染一个 HTML 隐藏字段域,等同于:

    <input id="id" name="id" type="hidden" value="1000"/>
    

      

    示例:

    <form:form action="/user/save" cssClass="form-horizontal" method="post" modelAttribute="tbuser">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="username" class="col-sm-2 control-label">用户名</label>
                                <div class="col-sm-10">
                                    <form:input path="username" cssClass="form-control" placeholder="用户名"></form:input>
                                </div>
                            </div>e
                            <div class="form-group">
                                <label for="password" class="col-sm-2 control-label">密码</label>
    
                                <div class="col-sm-10">
                                    <form:password path="password"  cssClass="form-control"  placeholder="密码"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="phone" class="col-sm-2 control-label">手机号</label>
                                <div class="col-sm-10">
                                    <form:input path="phone" cssClass="form-control" placeholder="手机号"></form:input>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="email" class="col-sm-2 control-label">邮箱</label>
                                <div class="col-sm-10">
                                    <form:input path="email" cssClass="form-control" placeholder="邮箱"></form:input>
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->
                        <div class="box-footer">
                            <button type="button" class="btn btn-default" onclick="javascript:history.go(-1)">返回</button>
                            <button type="submit" class="btn btn-info pull-right">添加</button>
                        </div>
                    </form:form>
    

    参数

    modelAttribute="tbuser" :必须加上,表单提交后保存默认值;然后无论是get和post请求中都需要加上 model.addAttribute("tbuser",tbUser);获者使用@ModelAttribute注解(下面有详细)
    <form:input path="username" cssClass="form-control" placeholder="用户名"></form:input> :表单提交后,保存的是 tbuser.username -->所以path中的值必须是tbuser中的字段
    名称


    相当于
    <form class="form-horizontal" action="/user/save" method="post">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="inputusername" class="col-sm-2 control-label">用户名</label>
                                <div class="col-sm-10">
                                    <input name="username" value="${tbuser.username}" type="username" class="form-control" id="inputusername" placeholder="用户名">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputPassword" class="col-sm-2 control-label">密码</label>
    
                                <div class="col-sm-10">
                                    <input name="password" value="${tbuser.password}" type="password" class="form-control" id="inputPassword" placeholder="密码">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputphone" class="col-sm-2 control-label">手机号</label>
                                <div class="col-sm-10">
                                    <input name="phone" value="${tbuser.phone}" type="text" class="form-control" id="inputphone" placeholder="手机号">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="inputemail" class="col-sm-2 control-label">邮箱</label>
                                <div class="col-sm-10">
                                    <input name="email" value="${tbuser.email}"  type="text" class="form-control" id="inputemail" placeholder="邮箱">
                                </div>
                            </div>
                        </div>
                        <!-- /.box-body -->
                        <div class="box-footer">
                            <button type="button" class="btn btn-default" onclick="javascript:history.go(-1)">返回</button>
                            <button type="submit" class="btn btn-info pull-right">添加</button>
                        </div>
                        <!-- /.box-footer -->
                    </form>
    

      

    @ModelAttribute 具有如下三个作用:

    • 绑定请求参数到命令对象:放在功能处理方法的入参上时,用于将多个请求参数绑定到一个命令对象,从而简化绑定流程,而且自动暴露为模型数据用于视图页面展示时使用
    • 暴露 @RequestMapping 方法返回值为模型数据:放在功能处理方法的返回值上时,是暴露功能处理方法的返回值为模型数据,用于视图页面展示时使用
    • 暴露表单引用对象为模型数据:放在处理器的一般方法(非功能处理方法)上时,是为表单准备要展示的表单引用对象,如注册时需要选择的所在城市等,而且在执行功能处理方法(@RequestMapping 注解的方法)之前,自动添加到模型对象中,用于视图页面展示时使用

     在执行所有的请求之前(当前类的所有的请求),都要先执行 @ModelAttribute 下面的方法,并且如果有返回值的话,直接将返回值添加到model(相当于)中

    model.addAttribute("tbUser",返回的对象);  tbUser就是返回方法名(函数的放回类型)
    可以使用TbUser来接受数据;

    参考:https://www.funtl.com/zh/spring-mvc/Spring-MVC-%E8%A1%A8%E5%8D%95%E6%A0%87%E7%AD%BE%E5%BA%93.html#%E5%A3%B0%E6%98%8E%E8%A1%A8%E5%8D%95%E6%A0%87%E7%AD%BE%E5%BA%93

  • 相关阅读:
    [转] STM32 FSMC学习笔记
    【转】嵌入式系统 Boot Loader 技术内幕
    mini2440 使用 JLink V8 直接烧写 Nor flash
    S3C6410移植uboot(一)
    2440的RTC时钟
    关闭2440 屏幕背光
    基于十级流水线的开立方根算法
    Visual Studio 2008配置SystemC开发环境
    Linux C 中字符串化操作符#
    linux 中 timeval结构体
  • 原文地址:https://www.cnblogs.com/yanxiaoge/p/10880769.html
Copyright © 2020-2023  润新知