2014-10-01 71 views
0

我已經發布了我的視圖和控制器請親切看看它。幫助我提供一個解決方案,在點擊按鈕之後提供所有字段的重置。按鈕點擊彈出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的表單中的所有標籤?

+0

這是假設你使用REST類型的控制器或基於寧靜的設置? – Aeseir 2014-10-02 04:30:27

+0

Iam不確定。如何找到它? – SmartJill 2014-10-02 09:16:31

+0

確定顯示您的控制器的代碼和查看請 – Aeseir 2014-10-02 11:49:22

回答

1

嘗試改變按鈕的onclick「復位」:

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

您還需要一個id爲「myForm的」添加到窗體。

或嘗試:

document.forms[0].reset(); 
+0

如果我在onclick設置,在使控制器調用之前它不應該重置,那麼沒有價值將會去服務部分,我想在服務調用後清除字段?如何做到這一點。請幫助 – SmartJill 2014-10-03 15:14:05

+0

,然後嘗試向userDetailsForm添加一個方法,將該類中的所有變量設置爲空。 – 2014-10-04 05:27:14

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",是否可行。

下面是一些我的代碼,同時嘗試重置春季形式:形式

<form:button type="reset" class="btn btn-danger btn-lg btn-block"> 
    RESET 
</form:button>