2014-10-01 3 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; 
    } 

} 

The 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

Это если вы используете контроллер RESTfull или успокоительные на основе установки? – Aeseir

+0

Я не уверен. Как это узнать? – SmartJill

+0

ok покажите мне код для вашего контроллера и просмотрите – Aeseir

ответ

1

попробуйте изменить OnClick кнопки «Сброс» для:

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

Вам также нужно добавить идентификатор «Myform» в форме.

ИЛИ попробовать:

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

, если я установил onclick, прежде чем делать вызов контроллера, он не должен быть сброшен, тогда никакое значение не поступит в сервисную часть, я хочу очистить поле после вызова servie «Как это можно сделать? пожалуйста, помогите – SmartJill

+0

, а затем попробуйте добавить метод userDetailsForm, который устанавливает все переменные в этом классе пустым. –

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:button type="reset" class="btn btn-danger btn-lg btn-block"> 
    RESET 
</form:button> 
Смежные вопросы