单击按钮时弹出MVC重置表单

时间:2014-10-01 22:23:05

标签: java spring-mvc

我发布了我的视图和控制器请好好看一下。帮我解决一下按钮点击后重置所有字段的问题。

package com.lv.rs.accessautomation.view.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.lv.rs.accessautomation.business.service.AccessService;
import com.lv.rs.accessautomation.common.constants.ViewConstants;
import com.lv.rs.accessautomation.common.util.ViewUtils;
import com.lv.rs.accessautomation.common.vo.ResUserVO;
import com.lv.rs.accessautomation.view.form.UserDetailsForm;

/**
 * @author tp50148
 *
 */
@Controller
public class AutomationController {

    @Autowired
    private AccessService accessService;

    @RequestMapping(value = ViewConstants.ACCESS_REQUEST, method = RequestMethod.POST)
    public String doActions(@ModelAttribute UserDetailsForm userDetailsForm, BindingResult result, @RequestParam String action, Map<String, Object> map,Model model){

        try {
            ResUserVO resuser  = null;
            switch(action.toLowerCase()){

            case ViewConstants.SEARCH_USER:
                resuser = accessService.getEquoteUser(userDetailsForm.getUserId().toUpperCase());
                if(resuser != null) 
                {
                    map.put("existingData", true);
                    ViewUtils.getInstance().setExistingUSer(ViewConstants.TRUE);
                    ViewUtils.getInstance().setExistingAppUser(ViewConstants.E_QUOTE);
                } else {
                    map.put("existingData", false);
                    ViewUtils.getInstance().setExistingUSer(ViewConstants.FALSE);
                }
                map.put("existingUser", resuser);
                map.put("printersMap", ViewUtils.getInstance().getPrinterList()); 

                break;


            case ViewConstants.SEARCH_QUOTE:
                resuser = accessService.getEquoteUser(userDetailsForm.getQuoteId().toUpperCase());
                if(resuser != null) 
                {
                    String printerName = ViewUtils.getInstance().getPrinterDetails().get(resuser.getPrinterId());
                    ViewUtils.getInstance().setQuoteUserId(resuser.getUserId());
                    ViewUtils.getInstance().setPrinterId(resuser.getPrinterId());
                    ViewUtils.getInstance().setSamQuoteId(resuser.getWindowId());
                    map.put("printersMap", ViewUtils.getInstance().getPrinterList());
                    map.put("printerName", printerName);
                }
                map.put("existingData", ViewUtils.getInstance().getExistingUSer());
                map.put("existingQuoteUser", resuser);
                break;

            case ViewConstants.SEARCH_CLAND:
                accessService.getClanadUser(userDetailsForm.getClanadId().toUpperCase());
                break;

            case ViewConstants.SAVE_USER:
                boolean save = false;
                if((ViewUtils.getInstance().getExistingAppUser() != null && ViewUtils.getInstance().getExistingAppUser().equals(ViewConstants.E_QUOTE)) || userDetailsForm.isQuoteAccess())
                     save = accessService.saveUser(userDetailsForm);
                break;

            case ViewConstants.DELETE_USER:
                if(ViewUtils.getInstance().getExistingAppUser().equals(ViewConstants.E_QUOTE)) {
                    accessService.deleteUser(userDetailsForm);
                }
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ViewConstants.ACCESS_VIEW;
    }

}

观点:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<HTML>
<HEAD>
<TITLE>Retirement Solutions - Access Management | Home</TITLE>
<link href="style/style.css" rel="stylesheet">
<script type="text/javascript"
    src="<c:url value="scripts/accesscontrol.js" />"></script>
</HEAD>

<body onload="validateTable();" onunload="">
    <form:form action="accessrequest" method="post" commandName="userDetailsForm">
        <table id="maintable" align="left" border="0" cellpadding="1"
            cellspacing="1" style="height: 100%; width: 100%;">
            <tbody>
                <tr>
                    <td colspan="2">
                        <table align="left" border="0" cellpadding="1" cellspacing="1"
                            style="width: 100%;">
                            <tbody>
                                <tr>
                                    <td align='left' width="10%"><input type="image"
                                        src="images/LV.gif" align="left" onclick="return false" /></td>
                                    <td align="center" class="bannerhead" width="90%"><font
                                        class="personal_note"><b>Retirement Solutions -
                                                Access Management</b></font></td>
                                </tr>
                            </tbody>
                        </table>

                        <p>&nbsp;</p>
                    </td>
                </tr>
                <tr height="1%">
                    <td colspan="2" align="left" class="hearderbar">Add/Edit User</td>
                </tr>
                <tr>
                    <td width="50%">
                        <table id="userDetailsMainTable" class="tableborder" align="right"
                            border="1">
                            <tbody>
                                <tr>
                                    <td class="hearderbar">User Details</td>
                                </tr>

                                <tr>
                                    <td>

                                        <table id="userDetailsTable" align="center" border="0"
                                            cellpadding="1" cellspacing="1" height="328" width="470">
                                            <tbody>
                                                <tr>
                                                    <td>&nbsp;</td>
                                                    <td width = 100%>
                                                        <div class="authentication">Application Access</div>
                                                    </td>
                                                    <td class="fieldlabel" align='center' width='2%'>:</td>
                                                    <td class="authentication"><form:checkbox path="quoteAccess" onclick="validateTable()" />E-quote</td>
                                                    <td class="authentication"><form:checkbox path="clanadAccess" onclick="validateTable()" />Clanad</td>
                                                </tr>
                                                <tr>
                                                    <td class="mandataory">*</td>
                                                    <td class="fieldlabel"><form:label path="userId">UserID</form:label></td>
                                                    <td class="fieldlabel" align='center' width='15%'>:</td>
                                                    <td><form:input path="userId" size="22" class="fieldlabel"/></td>
                                                    <td><input name="action" type="submit"
                                                        class="aamButton" value="SearchUser"
                                                        onmouseover="this.style.background='#3F5201'"
                                                        onmouseout="this.style.background='#9dce2c'"
                                                        onclick="return searchExistingUser();" /></td>
                                                </tr>
                                                <tr>
                                                    <td class="mandataory">*</td>
                                                    <td class="fieldlabel"><form:label path="firstName">First Name</form:label></td>
                                                    <td class="fieldlabel" align='center' width='15%'>:</td>
                                                    <td class="fieldlabel"><form:input path="firstName" size="22" class="fieldlabel" value = "${existingUser.firstname}"/></td>
                                                    <td>&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td class="mandataory">*</td>
                                                    <td class="fieldlabel"><form:label path="lastName">Last Name</form:label></td>
                                                    <td class="fieldlabel" align='center' width='15%'>:</td>
                                                    <td class="fieldlabel"><form:input path="lastName" size="22" class="fieldlabel" value = "${existingUser.lastname}"/></td>
                                                    <td>&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td>&nbsp;</td>
                                                    <td class="fieldlabel"><form:label path="email">E-Mail</form:label></td>
                                                    <td class="fieldlabel" align='center' width='15%'>:</td>
                                                    <td class="fieldlabel"><form:input path="email" size="22" class="fieldlabel" value = "${existingUser.email}"/></td>
                                                    <td class="fieldlabel">@lv.com</td>

                                                </tr>
                                                <tr>
                                                    <td class="mandataory">*</td>
                                                    <td class="fieldlabel">Printer</td>
                                                    <td class="fieldlabel" align='center' width='15%'>:</td>
                                                    <td class="fieldlabel" width = '100%'>
                                                    <form:select path="printer" style="width: 170px;">  
                                                      <c:forEach items="${printersMap}" var="theObject" >
                                                       <option value="${theObject.printerId}" class="fieldlabel" <c:if test="${theObject.printerId == existingUser.printerId}">selected</c:if>>
                                                       ${theObject.printerName}</option>
                                                     </c:forEach>
                                                     </form:select>
                                                 </td>


                                                    <%-- <form:option value="-" class="fieldlabel" label="Select an Printer "></form:option> 
                                                    <form:options items="${printersMap}" class="fieldlabel"/>
                                                    </form:select>
                                                 </td>--%>




                                                    <td>&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td>&nbsp;</td>
                                                    <td class="fieldlabel"><form:label path="telephone">Telephone</form:label></td>
                                                    <td class="fieldlabel" align='center' width='15%'>:</td>
                                                    <td class="fieldlabel"><form:input path="telephone" size="22" class="fieldlabel" value = "${existingUser.telephone}"/></td>
                                                    <td>&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td>&nbsp;</td>
                                                    <td class="fieldlabel">Activate</td>
                                                    <td class="fieldlabel" align='center' width='15%'>:</td>
                                                    <td class="fieldlabel">
                                                    <%-- <input type="radio" path="activate" value="Y" <c:if test="${(existingUser.active eq 'Y') or (existingUser.active == null)}">checked</c:if>>Yes --%>
                                                     <form:radiobutton path="activate" value="y" />Yes 
                                                    </td>
                                                    <td class="fieldlabel">
                                                   <%--  <input type="radio" path="activate" value="N" <c:if test="${existingUser.active eq 'N'}">checked</c:if>>No --%>
                                                    <form:radiobutton path="activate" value="N" />No
                                                    </td>

                                                </tr>
                                                <tr>
                                                    <td height="10%" colspan="5"></td>
                                                </tr>
                                                <tr>
                                                    <td colspan="5" align="center" rowspan="1"
                                                        style="vertical-align: middle;"><input name="action" type="submit"
                                                        value="Save/update" class="aamButton"
                                                        onmouseover="this.style.background='#3F5201'"
                                                        onmouseout="this.style.background='#9dce2c'"
                                                        onclick="return saveUser(${existingData});" /> 
                                                        <input name="action" type="submit" value="Delete"
                                                        class="aamButton"
                                                        onmouseover="this.style.background='#3F5201'"
                                                        onmouseout="this.style.background='#9dce2c'"
                                                        onclick="return deleteUser();"/> 
                                                        <input type="button"
                                                        value="Reset" class="aamButton"
                                                        onmouseover="this.style.background='#3F5201'"
                                                        onmouseout="this.style.background='#9dce2c'"
                                                        onclick="this.form.reset();" /></td>

                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                        </table>
                        <p>&nbsp;</p>
                    </td>
                    <td width="25%">
                        <table id="empty" height="20%" width="90%">
                            <tr></tr>
                            <td>&nbsp;</td>
                        </table>
                        <table height="60%" width="70%">
                            <tr>
                                <td width="100%">
                                    <table id="quoteTable" align="left" class="tableborder"
                                        border="1" style="vertical-align: middle;" cellpadding="1"
                                        cellspacing="1">
                                        <tbody>
                                            <tr>
                                                <td class="hearderbar">Sample E-quote User</span></td>
                                            </tr>
                                            <tr>
                                                <td style="text-align: center; vertical-align: middle;">
                                                    <table align="right" border="0" cellpadding="1"
                                                        cellspacing="1" width=400px;>
                                                        <tbody>
                                                            <tr>
                                                                <td class="fieldlabel"><form:label path="quoteId">Enter user ID</form:label></td>
                                                                <td><form:input path="quoteId" size="22" class="fieldlabel"/></td>
                                                                <td><input name="action" class="aamButton"
                                                                    type="submit" value="SearchQuote"
                                                                    onmouseover="this.style.background='#3F5201'"
                                                                    onmouseout="this.style.background='#9dce2c'" onclick=" return searchQuoteUser();"/></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="fieldlabel"><form:label path="quoteFname">First Name</form:label></td>
                                                                <td><form:input path="quoteFname" size="22" readonly="true" class="fieldlabel" value = "${existingQuoteUser.firstname}"/></td>
                                                                <td>&nbsp;</td>
                                                            </tr>
                                                            <tr>
                                                                <td class="fieldlabel"><form:label path="quoteLname">Last Name</form:label></td>
                                                                <td><form:input path="quoteLname" size="22" readonly="true" class="fieldlabel" value = "${existingQuoteUser.lastname}"/></td>
                                                                <td>&nbsp;</td>
                                                            </tr>
                                                            <tr>
                                                                <td class="fieldlabel"><form:label path="quotePrinter">Printer</form:label></td>
                                                                <td><form:input path="quotePrinter" size="22" readonly="true" class="fieldlabel" value = "${printerName}"/></td>

                                                                <td>&nbsp;</td>
                                                            </tr>
                                                        </tbody>
                                                    </table>

                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>


                                    <table id="clanadTable" align="left" class="tableborder"
                                        border="1" cellpadding="1" cellspacing="1">
                                        <tbody>
                                            <tr>
                                                <td class="hearderbar">Sample Clanad User</span></td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <table align="center" border="0" cellpadding="1"
                                                        cellspacing="1" width=400px>
                                                        <tbody>
                                                            <tr>
                                                                <td class="fieldlabel"><form:label path="clanadId">Enter user ID</form:label></td>
                                                                <td><form:input path="clanadId" size="22" class="fieldlabel"/></td>
                                                                <td><input name="action" class="aamButton"
                                                                    type="button" value="SearchClanad"
                                                                    onmouseover="this.style.background='#3F5201'"
                                                                    onmouseout="this.style.background='#9dce2c'"
                                                                    onclick="searchClanadUser();" /></td>
                                                            </tr>
                                                            <tr>
                                                                <td class="fieldlabel"><form:label path="clanadFname">First Name</form:label></td>
                                                                <td><form:input path="clanadFname" size="22" readonly="true" class="fieldlabel"/></td>
                                                                <td>&nbsp;</td>
                                                            </tr>
                                                            <tr>
                                                                <td class="fieldlabel"
                                                                    style="text-align: left; vertical-align: middle;">
                                                                    <form:label path="clanadLname">Last Name</form:label></td>
                                                                <td><form:input path="clanadLname" size="22" readonly="true" class="fieldlabel"/></td>
                                                                <td>&nbsp;</td>
                                                            </tr>
                                                        </tbody>
                                                    </table>
                                                    <p>&nbsp;</p>

                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </tbody>
        </table>

    </form:form>
</body>
</html>

点击按钮后,调用Spring MVC控制器后,如何清除Spring MVC表单中的所有标签?

2 个答案:

答案 0 :(得分:1)

尝试更改“重置”按钮的onclick:

document.getElementById("myForm").reset();

您还需要在表单中添加id“myForm”。

或尝试:

document.forms[0].reset();

答案 1 :(得分:0)

在您的代码中,

<input 
type="button"
value="Reset" 
class="aamButton"
onmouseover="this.style.background='#3F5201'"
onmouseout="this.style.background='#9dce2c'"
onclick="this.form.reset();" />

如果有效,请尝试将type="button"更改为type="reset"

尝试重置Spring form:form

时,我的代码中有些内容
<form:button type="reset" class="btn btn-danger btn-lg btn-block">
    RESET
</form:button>