Form标签表单回显与提交

Posted Zenz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Form标签表单回显与提交相关的知识,希望对你有一定的参考价值。

SpringMVC虽然没有struts2那样强大的标签库,但是form标签也同样实用

1.javabean片段

public class User implements Serializable{
    private static final long serialVersionUID = -1684752473484153239L;
    private String id;
    private String dept;
    private String account;
    private String name;
    private String password;
    private String headImg;
    private Boolean gender;
    private String state;
    private String mobile;
    private String email;
    private Date birthday;
    private String memo;
    //用户状态:0-无效,1-有效
    public static String USER_STATE_VALID = "1";
    public static String USER_STATE_INVALID = "0";
}

 2.回显方法

 //跳转到编辑页面
    @RequestMapping(value="/editUI")
    public String editUI(String id, Model model){
        boolean headImgExist = false;
        if (id != null) {
            User user = userService.findObjectById(id);
            //回显数据
            model.addAttribute("user", user);
            if (user.getHeadImg().length() > 32){
                headImgExist = true;
                //回显头像
                model.addAttribute("headImg",   request.getContextPath() //
                          +"/"+dirs+"/"+user.getHeadImg());
            }
            model.addAttribute("headImgExist",headImgExist);
        }
        return "editUI";
    }

 3.回显页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>
<html>
<head>
    <title>用户管理</title>
    <c:set var="basePath" value="${pageContext.request.contextPath}" />
    <link rel="stylesheet" type="text/css"
          href="${basePath}/js/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="${basePath}/js/easyui/themes/icon.css">
    <script type="text/javascript" src="${basePath}/js/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="${basePath}/js/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="${basePath}/js/easyui/easyui-lang-zh_CN.js">  
         </script>
    <link href="${basePath}/css/skin1.css" rel="stylesheet" type="text/css" />
</head>
<body class="rightBody">
<sf:form id="form" name="form" action="${basePath}/user/edit.action" method="post"
          modelAttribute="user" enctype="multipart/form-data">
    <div class="p_d_1">
        <div class="p_d_1_1">
            <div class="content_info">
                <div class="c_crumbs">
                    <div><b></b><strong>用户管理</strong> - 编辑用户</div>
                </div>
                <div class="tableH2">编辑用户</div>
                <table id="baseInfo" width="100%" align="center" class="list" border="0" 
                       cellpadding="0" cellspacing="0">
                    <tr>
                        <td class="tdBg" width="200px">所属部门:</td>
                        <td>
                            <sf:select path="dept">
                                <sf:option value="部门A" label="部门A"/>
                                <sf:option value="部门B" label="部门B"/>
                            </sf:select>
                        </td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">头像:</td>
                        <td>
                           <%--判断头像是否存在,存在则显示,不存在则隐藏--%>
                            <c:choose>
                                <c:when test="${headImgExist}">
                                    <img src="${headImg}" width="100" height="100"/>
                               </c:when>
                                <c:otherwise/>
                            </c:choose>
                            <input type="file" name="headImage"/>
                        </td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">用户名:</td>
                        <td><sf:input path="name"/></td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">帐号:</td>
                        <td><sf:input path="account"/></td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">密码:</td>
                        <td><sf:input path="password"/></td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">性别:</td>
                        <td>
                            <sf:radiobutton path="gender" value="true" label="男"/>
                            <sf:radiobutton path="gender" value="false" label="女"/>
                        </td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">角色:</td>
                        <td></td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">电子邮箱:</td>
                        <td><sf:input path="email"/></td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">手机号:</td>
                        <td><sf:input path="mobile"/></td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">生日:</td>
                        <td>
                           <sf:input class="easyui-datebox" editable="false" path="birthday"/>
                        </td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">状态:</td>
                        <td>
                            <sf:radiobutton path="state" value="1" label="有效"/>
                            <sf:radiobutton path="state" value="0" label="无效"/>
                        </td>
                    </tr>
                    <tr>
                        <td class="tdBg" width="200px">备注:</td>
                        <td><sf:textarea path="memo"/></td>
                    </tr>
                </table>
                <sf:hidden path="id"/>
                <div class="tc mt20">
                    <input type="submit" class="btnB2" value="保存"/>
                    <input type="button" onclick="javascript:history.go(-1)" class="btnB2" value="返回"/>
                </div>
            </div>
        </div>
    </div>
</sf:form>
</body>
</html>

 

以上是关于Form标签表单回显与提交的主要内容,如果未能解决你的问题,请参考以下文章

SpringMVC表单标签&处理静态资源

提交表单时如何打印所有 POST 结果? [复制]

关于SpringMVC的几件小事

jsp中怎样获得表单中radio的回显信息?

页面回显与URL模板映射

Java反序列化回显与内存马注入