java 两个提交按钮做两件不同的事情

xfyts7mz  于 2024-01-05  发布在  Java
关注(0)|答案(2)|浏览(157)

使用Thymeleaf,Spring-boot和Java。当我点击这些按钮中的任何一个(在同一个表单/页面上列出)时,它提交到/send路径而不是其定向路径。想法?一个按钮需要去发送,一个需要去/addPolicy并向页面添加一些输入项。

  1. <form class="ui form" th:object="${directBind}" method="post" th:action="@{/send}" style="padding:0 10px;">
  2. <h4>Customer Setup</h4>
  3. <div class="row">
  4. <label >Contact Name (First/Last):</label>
  5. <input type="text" th:field="*{contactName}" required="true" />
  6. </div>
  7. <div class="row">
  8. <label for="formAddress">Address:</label>
  9. <input type="text" id="formAddress" th:field="*{formAddress}" required="true"/>
  10. </div>
  11. <div class="row">
  12. <label for="phoneNumber">Phone Number:</label>
  13. <input type="text" id="phoneNumber" th:field="*{phoneNumber}" required="true"/>
  14. </div>
  15. <div class="row">
  16. <label for="email">Email:</label>
  17. <input type="email" id="email" th:field="*{email}" required="true"/>
  18. </div>
  19. <div class="row">
  20. <label for="website" style="margin-top: 1em;"> Website:</label>
  21. <input type="text" id="website" th:field="*{website}" required="true"/>
  22. </div>
  23. <div class="row">
  24. <label for="nameInsured">Name Insured:</label>
  25. <input type="text" id="nameInsured" th:field="*{insuredName}" required="true"/>
  26. </div>
  27. <div class="row">
  28. <label>Business with Agency:</label>
  29. <div th:each="businessAgency : ${businessAgencies}">
  30. <input type="checkbox" th:field="*{businessAgencyList}" th:value="${businessAgency}"/>
  31. <label th:text="${businessAgency}">Business with Agency</label>
  32. </div>
  33. </div>
  34. <div class="row">
  35. <label for="executive">Executive:</label>
  36. <input type="text" id="executive" th:field="*{executive}" required="true"/>
  37. </div>
  38. <div class="row" style="display: inline-block;">
  39. <p>Service Level:</p>
  40. <ul>
  41. <li>
  42. <label class="forCheckbox">
  43. <input type="checkbox" th:field="*{serviceLevel}" required="true"/>
  44. Affiliate Serviced</label>
  45. </li>
  46. </ul>
  47. </div>
  48. <div class="row">
  49. <label for="affiliateRep">Affiliate Designated Representative:</label>
  50. <input type="text" id="affiliateRep" th:field="*{affiliateRep}" required="true" />
  51. </div>
  52. <div>
  53. <h4 style="display: inline;">AMS360 Policy Setup</h4>
  54. <button id="addPolicy" type="submit" name="addPolicy" class="btn btn-default" style="margin-left: 1rem; margin-bottom: 1rem;"><span class="fa fa-plus"></span></button>
  55. </div>
  56. <div class="col-sm-12">
  57. <hr/>
  58. <table class="table table-striped" data-classes="table-no-bordered" data-striped="true" data-show-columns="true" data-pagination="true">
  59. <thead>
  60. <tr>
  61. <th>Policy Number</th>
  62. <th>Policy Term Start Date</th>
  63. <th>Policy Term End Date</th>
  64. <th>Line of Coverage</th>
  65. <th>Parent Company</th>
  66. <th>Writing Company</th>
  67. <th>Bill Type</th>
  68. <th>Quote Premium</th>
  69. <th>Commission</th>
  70. </tr>
  71. </thead>
  72. <tbody>
  73. <tr th:each="ams360Policy,stat : *{ams360Policies}">
  74. <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].policyNumber}"/></td>
  75. <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].policyTermStartDate}"/></td>
  76. <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].policyTermEndDate}"/></td>
  77. <td><input type="text" id="lineOfCoverage" th:field="*{ams360Policies[__${stat.index}__].lineOfCoverage}" /></td>
  78. <td><input type="text" id="parentCompany" th:field="*{ams360Policies[__${stat.index}__].parentCompany}" /></td>
  79. <td><input type="text" id="writingCompany" th:field="*{ams360Policies[__${stat.index}__].writingCompany}" /></td>
  80. <td> <div th:each="billType : ${billTypeList}">
  81. <input type="checkbox" th:field="*{billTypeOptions}" th:value="${billType}"/>
  82. <label th:text="${billType}">Bill Types</label>
  83. </div></td>
  84. <td><input type="text" id="quotePremium" th:field="*{ams360Policies[__${stat.index}__].quotePremium}" /></td>
  85. <td><input type="text" id="commission" th:field="*{ams360Policies[__${stat.index}__].commission}" /></td>
  86. </tr>
  87. </tbody>
  88. </table>
  89. </div>
  90. <h4>PMA Setup</h4>
  91. <div class="row" sec:authorize="hasAnyRole('ADMIN','USER')">
  92. <label>Sub Brand:</label>
  93. <th:block th:switch="${loggedUser==null or loggedUser.client==null }">
  94. <span th:case="false" th:text="${loggedUser.client.getLegalName()}" ></span>
  95. <span th:case="true">Not Applicable</span>
  96. </th:block>
  97. <th:block th:switch="${loggedUser==null or loggedUser.client==null}">
  98. <input th:case="false" type="hidden" name="subBrand" id="subBrand" th:value="${loggedUser.client.getLegalName()}" />
  99. <input th:case="true" type="hidden" name="subBrand" id="subBrand" value="Not Applicable" />
  100. </th:block>
  101. </div>
  102. <!-- Business Class Drop Down Field Below: -->
  103. <div class="row">
  104. <label for="businessClass" >Business Class: </label>
  105. <select class="select-2 form-control" th:field="*{businessClass}" id="businessClass" >
  106. <option value="Animal Services">Animal Services</option>
  107. <option value="Arts Entertainment">Arts &amp; Entertainment</option>
  108. <option value="Auto Services">Auto Services</option>
  109. <option value="Bonds">Bonds</option>
  110. <option value="Carpet Furniture or Upholstery Cleaning">Carpet, furniture or upholstery cleaning</option>
  111. <option value="Clock Making/Repair">Clock Making/Repair</option>
  112. <option value="Contractor - Appliance Install/Repair">Contractor - Appliance Install/Repair</option>
  113. <option value="Contractor - Carpentry (Exterior)">Contractor - Carpentry (Exterior)</option>
  114. <option value="Contractor - Carpentry (Interior)">Contractor - Carpentry (Interior)</option>
  115. <option value="Contractor - Concrete (No Foundations)">Contractor - Concrete (No Foundations)</option>
  116. <option value="Contractor - Debris Removal">Contractor - Debris Removal</option>
  117. <option value="Contractor - Doors and Windows">Contractor - Doors &amp; Windows</option>
  118. <option value="Contractor - Drywall/Plastering /Stucco">Contractor - Drywall/Plastering /Stucco</option>
  119. <option value="Contractor - Electrical">Contractor - Electrical</option>
  120. <option value="Contractor - Excavating">Contractor - Excavating</option>
  121. <option value="Contractor - Fence Installation/Repair">Contractor - Fence Installation/Repair</option>
  122. <option value="Contractor - Floor Covering (Not tile or stone)">Contractor - Floor Covering (Not tile or stone)</option>
  123. <option value="Contractor - General/Builder/New Construction">Contractor - General/Builder/New Construction</option>
  124. <option value="Contractor - Glass Install/Repair (Non-Auto)">Contractor - Glass Install/Repair (Non-Auto)</option>
  125. <option value="Contractor - Grading/Snow Removal ">Contractor - Grading/Snow Removal </option>
  126. <option value="Contractor - Handyman/Remodeling">Contractor - Handyman/Remodeling</option>
  127. <option value="Contractor - Heating and Air">Contractor - Heating &amp; Air</option>
  128. <option value="Contractor - Interior Finish Work">Contractor - Interior Finish Work</option>
  129. <option value="Contractor - Masonry">Contractor - Masonry</option>
  130. <option value="Contractor - Other">Contractor - Other</option>
  131. <option value="Contractor - Painting (Exterior)">Contractor - Painting (Exterior)</option>
  132. <option value="Contractor - Painting (Interior)">Contractor - Painting (Interior)</option>
  133. <option value="Contractor - Painting (Interior/Exterior)">Contractor - Painting (Interior/Exterior)</option>
  134. <option value="Contractor - Paving">Contractor - Paving</option>
  135. <option value="Contractor - Plumbing (Commercial)">Contractor - Plumbing (Commercial)</option>
  136. <option value="Contractor - Plumbing (residential)">Contractor - Plumbing (residential)</option>
  137. <option value="Contractor - Plumbing (Residential/Commercial)">Contractor - Plumbing (Residential/Commercial)</option>
  138. <option value="Contractor - Siding and Gutter Install">Contractor - Siding &amp; Gutter Install</option>
  139. <option value="Contractor - Tile/Stone/Marble/Mosaic/Terrazzo">Contractor - Tile/Stone/Marble/Mosaic/Terrazzo</option>
  140. <option value="Daycares">Daycares</option>
  141. <option value="Domestic Workers">Domestic Workers</option>
  142. <option value="Educational Services">Educational Services</option>
  143. <option value="Farm">Farm</option>
  144. <option value="Food and Beverage">Food &amp; Beverage</option>
  145. <option value="Generic BOP/Package (GL/Property)">Generic BOP/Package (GL/Property)</option>
  146. <option value="Homecare">Homecare</option>
  147. <option value="Homeowners Association">Homeowners Association</option>
  148. <option value="Hotel/Motel">Hotel/Motel</option>
  149. <option value="Janitorial">Janitorial</option>
  150. <option value="Landscaping">Landscaping</option>
  151. <option value="Lessors Risk">Lessors Risk</option>
  152. <option value="Life Science">Life Science</option>
  153. <option value="Locksmith">Locksmith</option>
  154. <option value="Manufacturing">Manufacturing</option>
  155. <option value="Medical Services">Medical Services</option>
  156. <option value="Miscellaneous Services">Miscellaneous Services (Not Construction Related)</option>
  157. <option value="Non-Profit">Non-Profit</option>
  158. <option value="Other">Other</option>
  159. <option value="Personal Lines (Non-Commercial)">Personal Lines (Non-Commercial)</option>
  160. <option value="Personal Services (Beauty, Wellness, Etc.)">Personal Services (Beauty, Wellness, Etc.)</option>
  161. <option value="Personal Trainers/Fitness Instructors/Yoga Instructors">Personal Trainers/Fitness Instructors/Yoga Instructors</option>
  162. <option value="Photography/Videography">Photography/Videography</option>
  163. <option value="Professional Services">Professional Services</option>
  164. <option value="Religious Organizations">Religious Organizations</option>
  165. <option value="Retail/Wholesale">Retail/Wholesale</option>
  166. <option value="Security Firms (Including Private Investigators)">Security Firms (Including Private Investigators)</option>
  167. <option value="Sign painting/lettering (Exterior)">Sign painting/lettering (Exterior)</option>
  168. <option value="Sign painting/lettering (Interior)">Sign painting/lettering (Interior)</option>
  169. <option value="Solar Energy">Solar energy</option>
  170. <option value="Sports/Recreation/Leisure (Other than Fitness and Trainers)">Sports/Recreation/Leisure (Other than Fitness &amp; Trainers)</option>
  171. <option value="Storage Facilities">Storage Facilities</option>
  172. <option value="Technology">Technology</option>
  173. <option value="Trucking/Transportation">Trucking/Transportation</option>
  174. <option value="Upholstery Work">Upholstery Work</option>
  175. <option value="Window Cleaning">Window Cleaning</option>
  176. <option value="Workers Compensation">Workers Compensation</option>
  177. </select>
  178. </div>
  179. <div class="row">
  180. <label for="descriptionOfOps">Description of Operations:</label>
  181. <input type="text" id="descriptionOfOps" th:field="*{descriptionOfOps}" required="true" />
  182. </div>
  183. <div class="formFooter">
  184. <input id="send" type="submit" value="send" name="send" class="btn btn-success finish" data-loading-text="Sent!"/>
  185. </div>
  186. </form>

字符串
这是控制器,addPolicy按钮应该转到/addPolicy链接,send按钮应该转到/send链接。

  1. @GetMapping("/directBind")
  2. public String getDirectBind(Model model){
  3. List<String> businessAgencies = new ArrayList<String>();
  4. businessAgencies.add("Personal");
  5. businessAgencies.add("Commercial");
  6. businessAgencies.add("Life");
  7. businessAgencies.add("Benefits");
  8. businessAgencies.add("Health");
  9. businessAgencies.add("Non P and C");
  10. model.addAttribute("businessAgencies", businessAgencies);
  11. Ams360Policy ams360Policy = new Ams360Policy();
  12. List<String> billTypeList = new ArrayList<String>();
  13. billTypeList.add("Direct Bill");
  14. billTypeList.add("Agency Bill");
  15. model.addAttribute("billTypeList", billTypeList);
  16. ams360Policy.setBillTypeOptions(billTypeList);
  17. DirectBind directBind = new DirectBind();
  18. List<String> businessAgencyList = new ArrayList<String>();
  19. directBind.setBusinessAgencyList(businessAgencyList);
  20. model.addAttribute("directBind", directBind);
  21. return "directBind";
  22. }
  23. @RequestMapping(value="/addPolicy", params="addPolicy")
  24. public String addPolicy(final DirectBind directBind, Model model){
  25. directBind.getAms360Policies().add(new Ams360Policy());
  26. model.addAttribute("directBind", directBind);
  27. return "addPolicy";
  28. }
  29. @RequestMapping(value="/send", params="send")
  30. public String send(Model model, @ModelAttribute(value="directBind") DirectBind directBind){
  31. List<String> businessAgencyList = directBind.getBusinessAgencyList();
  32. Mail mail = new Mail();
  33. mail.setFrom("[email protected]");
  34. mail.setTo(new String[]{"[email protected]"});
  35. mail.setSubject("Oli Affiliate - AMS360 & PMA Data Checklist");
  36. Map<String, Object> mailModel = new HashMap<String, Object>();
  37. mail.setModel(mailModel);
  38. try {
  39. emailService.sendSimpleMessage(mail, directBind);
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. return ("redirect:/?sentMessageFail");
  43. }
  44. return ("redirect:/?sentMessage");
  45. }
  46. @RequestMapping(value="/email")
  47. public String email(){
  48. return "emailMessage";
  49. }
  50. }

qyuhtwio

qyuhtwio1#

如果你使用的是JavaScript或者js框架,比如jQuery,你可以将按钮绑定到一个函数上,这个函数会将表单提交给指定的动作。
详情请参见Process a Form Submit with Multiple Submit Buttons in Javascript

brc7rcf0

brc7rcf02#

我推断,你的意图是使用一个spring控制器来处理来自两个提交按钮的提交操作。

  • 你可以为两个html输入按钮使用相同的名称和不同的值属性。按钮的名称和值属性作为请求参数发送给控制器。这将允许控制器识别哪个按钮被点击并采取适当的操作。
  1. <input id="send_id" type="submit" name="action" value="send"/>
  2. <input id="addPolicy_id" type="submit" name="action" value="addPolicy"/>

字符串

  1. @RequestMapping(value="/directBind")
  2. public String doExecute(@RequestParam("action") String action, Model model, @ModelAttribute(value="directBind") DirectBind directBind){
  3. if (action=="send"){
  4. //do something
  5. send();
  6. }
  7. if (action=="addPolicy"){
  8. //do something
  9. addPolicy();
  10. }
  11. }

展开查看全部

相关问题