两个提交按钮可以做两种不同的事情

时间:2018-10-02 16:43:45

标签: java spring-boot thymeleaf

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

    <form class="ui form" th:object="${directBind}"  method="post"  th:action="@{/send}" style="padding:0 10px;">
        <h4>Customer Setup</h4>
        <div class="row">
            <label >Contact Name (First/Last):</label>
            <input type="text" th:field="*{contactName}" required="true" />
        </div>
        <div class="row">
            <label for="formAddress">Address:</label>
            <input type="text" id="formAddress" th:field="*{formAddress}" required="true"/>
        </div>
        <div class="row">
            <label for="phoneNumber">Phone Number:</label>
            <input type="text" id="phoneNumber" th:field="*{phoneNumber}" required="true"/>
        </div>
        <div class="row">
            <label for="email">Email:</label>
            <input type="email" id="email" th:field="*{email}" required="true"/>
        </div>
        <div class="row">
            <label for="website" style="margin-top: 1em;"> Website:</label>
            <input type="text" id="website" th:field="*{website}" required="true"/>
        </div>
        <div class="row">
            <label for="nameInsured">Name Insured:</label>
            <input type="text" id="nameInsured" th:field="*{insuredName}" required="true"/>
        </div>
        <div class="row">
            <label>Business with Agency:</label>
            <div th:each="businessAgency : ${businessAgencies}">
                <input type="checkbox" th:field="*{businessAgencyList}" th:value="${businessAgency}"/>
                <label th:text="${businessAgency}">Business with Agency</label>
            </div>

        </div>
        <div class="row">
            <label for="executive">Executive:</label>
            <input type="text" id="executive" th:field="*{executive}" required="true"/>
        </div>
        <div class="row" style="display: inline-block;">
            <p>Service Level:</p>
            <ul>
                <li>
                    <label class="forCheckbox">
                        <input type="checkbox" th:field="*{serviceLevel}" required="true"/>
                        Affiliate Serviced</label>
                 </li>
            </ul>
        </div>
        <div class="row">
            <label for="affiliateRep">Affiliate Designated Representative:</label>
            <input type="text" id="affiliateRep" th:field="*{affiliateRep}" required="true" />
        </div>

        <div>
             <h4 style="display: inline;">AMS360 Policy Setup</h4>
             <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>
        </div>
        <div class="col-sm-12">
            <hr/>

            <table class="table table-striped" data-classes="table-no-bordered" data-striped="true" data-show-columns="true" data-pagination="true">
                <thead>
                <tr>
                    <th>Policy Number</th>
                    <th>Policy Term Start Date</th>
                    <th>Policy Term End Date</th>
                    <th>Line of Coverage</th>
                    <th>Parent Company</th>
                    <th>Writing Company</th>
                    <th>Bill Type</th>
                    <th>Quote Premium</th>
                    <th>Commission</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="ams360Policy,stat : *{ams360Policies}">
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].policyNumber}"/></td>
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].policyTermStartDate}"/></td>
                    <td> <input type="text" class="form-control" th:field="*{ams360Policies[__${stat.index}__].policyTermEndDate}"/></td>
                    <td><input type="text" id="lineOfCoverage" th:field="*{ams360Policies[__${stat.index}__].lineOfCoverage}" /></td>
                    <td><input type="text" id="parentCompany" th:field="*{ams360Policies[__${stat.index}__].parentCompany}" /></td>
                    <td><input type="text" id="writingCompany" th:field="*{ams360Policies[__${stat.index}__].writingCompany}" /></td>
                    <td> <div th:each="billType : ${billTypeList}">
                        <input type="checkbox" th:field="*{billTypeOptions}" th:value="${billType}"/>
                        <label th:text="${billType}">Bill Types</label>
                    </div></td>
                    <td><input type="text" id="quotePremium" th:field="*{ams360Policies[__${stat.index}__].quotePremium}" /></td>
                    <td><input type="text" id="commission" th:field="*{ams360Policies[__${stat.index}__].commission}" /></td>
                </tr>
                </tbody>
            </table>
        </div>


        <h4>PMA Setup</h4>
        <div class="row" sec:authorize="hasAnyRole('ADMIN','USER')">
            <label>Sub Brand:</label>
            <th:block th:switch="${loggedUser==null or loggedUser.client==null }">
                <span th:case="false" th:text="${loggedUser.client.getLegalName()}" ></span>
                <span th:case="true">Not Applicable</span>
            </th:block>
            <th:block th:switch="${loggedUser==null or loggedUser.client==null}">
                <input th:case="false" type="hidden" name="subBrand"  id="subBrand" th:value="${loggedUser.client.getLegalName()}" />
                <input th:case="true" type="hidden" name="subBrand"   id="subBrand" value="Not Applicable" />
            </th:block>
        </div>

        <!-- Business Class Drop Down Field Below: -->
        <div class="row">
            <label for="businessClass" >Business Class: </label>
            <select class="select-2 form-control" th:field="*{businessClass}" id="businessClass" >
                    <option value="Animal Services">Animal Services</option>
                    <option value="Arts Entertainment">Arts &amp; Entertainment</option>
                    <option value="Auto Services">Auto Services</option>
                    <option value="Bonds">Bonds</option>
                    <option value="Carpet Furniture or Upholstery Cleaning">Carpet, furniture or upholstery cleaning</option>
                    <option value="Clock Making/Repair">Clock Making/Repair</option>
                    <option value="Contractor - Appliance Install/Repair">Contractor - Appliance Install/Repair</option>
                    <option value="Contractor - Carpentry (Exterior)">Contractor - Carpentry (Exterior)</option>
                    <option value="Contractor - Carpentry (Interior)">Contractor - Carpentry (Interior)</option>
                    <option value="Contractor - Concrete (No Foundations)">Contractor - Concrete (No Foundations)</option>
                    <option value="Contractor - Debris Removal">Contractor - Debris Removal</option>
                    <option value="Contractor - Doors and Windows">Contractor - Doors &amp; Windows</option>
                    <option value="Contractor - Drywall/Plastering /Stucco">Contractor - Drywall/Plastering /Stucco</option>
                    <option value="Contractor - Electrical">Contractor - Electrical</option>
                    <option value="Contractor - Excavating">Contractor - Excavating</option>
                    <option value="Contractor - Fence Installation/Repair">Contractor - Fence Installation/Repair</option>
                    <option value="Contractor - Floor Covering (Not tile or stone)">Contractor - Floor Covering (Not tile or stone)</option>
                    <option value="Contractor - General/Builder/New Construction">Contractor - General/Builder/New Construction</option>
                    <option value="Contractor - Glass Install/Repair (Non-Auto)">Contractor - Glass Install/Repair (Non-Auto)</option>
                    <option value="Contractor - Grading/Snow Removal ">Contractor - Grading/Snow Removal </option>
                    <option value="Contractor - Handyman/Remodeling">Contractor - Handyman/Remodeling</option>
                    <option value="Contractor - Heating and Air">Contractor - Heating &amp; Air</option>
                    <option value="Contractor - Interior Finish Work">Contractor - Interior Finish Work</option>
                    <option value="Contractor - Masonry">Contractor - Masonry</option>
                    <option value="Contractor - Other">Contractor - Other</option>
                    <option value="Contractor - Painting (Exterior)">Contractor - Painting (Exterior)</option>
                    <option value="Contractor - Painting (Interior)">Contractor - Painting (Interior)</option>
                    <option value="Contractor - Painting (Interior/Exterior)">Contractor - Painting (Interior/Exterior)</option>
                    <option value="Contractor - Paving">Contractor - Paving</option>
                    <option value="Contractor - Plumbing (Commercial)">Contractor - Plumbing (Commercial)</option>
                    <option value="Contractor - Plumbing (residential)">Contractor - Plumbing (residential)</option>
                    <option value="Contractor - Plumbing (Residential/Commercial)">Contractor - Plumbing (Residential/Commercial)</option>
                    <option value="Contractor - Siding and Gutter Install">Contractor - Siding &amp; Gutter Install</option>
                    <option value="Contractor - Tile/Stone/Marble/Mosaic/Terrazzo">Contractor - Tile/Stone/Marble/Mosaic/Terrazzo</option>
                    <option value="Daycares">Daycares</option>
                    <option value="Domestic Workers">Domestic Workers</option>
                    <option value="Educational Services">Educational Services</option>
                    <option value="Farm">Farm</option>
                    <option value="Food and Beverage">Food &amp; Beverage</option>
                    <option value="Generic BOP/Package (GL/Property)">Generic BOP/Package (GL/Property)</option>
                    <option value="Homecare">Homecare</option>
                    <option value="Homeowners Association">Homeowners Association</option>
                    <option value="Hotel/Motel">Hotel/Motel</option>
                    <option value="Janitorial">Janitorial</option>
                    <option value="Landscaping">Landscaping</option>
                    <option value="Lessors Risk">Lessors Risk</option>
                    <option value="Life Science">Life Science</option>
                    <option value="Locksmith">Locksmith</option>
                    <option value="Manufacturing">Manufacturing</option>
                    <option value="Medical Services">Medical Services</option>
                    <option value="Miscellaneous Services">Miscellaneous Services (Not Construction Related)</option>
                    <option value="Non-Profit">Non-Profit</option>
                    <option value="Other">Other</option>
                    <option value="Personal Lines (Non-Commercial)">Personal Lines (Non-Commercial)</option>
                    <option value="Personal Services (Beauty, Wellness, Etc.)">Personal Services (Beauty, Wellness, Etc.)</option>
                    <option value="Personal Trainers/Fitness Instructors/Yoga Instructors">Personal Trainers/Fitness Instructors/Yoga Instructors</option>
                    <option value="Photography/Videography">Photography/Videography</option>
                    <option value="Professional Services">Professional Services</option>
                    <option value="Religious Organizations">Religious Organizations</option>
                    <option value="Retail/Wholesale">Retail/Wholesale</option>
                    <option value="Security Firms (Including Private Investigators)">Security Firms (Including Private Investigators)</option>
                    <option value="Sign painting/lettering (Exterior)">Sign painting/lettering (Exterior)</option>
                    <option value="Sign painting/lettering (Interior)">Sign painting/lettering (Interior)</option>
                    <option value="Solar Energy">Solar energy</option>
                    <option value="Sports/Recreation/Leisure (Other than Fitness and Trainers)">Sports/Recreation/Leisure (Other than Fitness &amp; Trainers)</option>
                    <option value="Storage Facilities">Storage Facilities</option>
                    <option value="Technology">Technology</option>
                    <option value="Trucking/Transportation">Trucking/Transportation</option>
                    <option value="Upholstery Work">Upholstery Work</option>
                    <option value="Window Cleaning">Window Cleaning</option>
                    <option value="Workers Compensation">Workers Compensation</option>
                    </select>


        </div>
        <div class="row">
            <label for="descriptionOfOps">Description of Operations:</label>
            <input type="text" id="descriptionOfOps" th:field="*{descriptionOfOps}" required="true" />
        </div>
        <div class="formFooter">
           <input id="send"  type="submit" value="send" name="send" class="btn btn-success finish" data-loading-text="Sent!"/>
        </div>
    </form>

这是控制器,应该将addPolicy按钮转到/ addPolicy链接,应该将发送按钮转到/ send链接。

    @GetMapping("/directBind")
    public String getDirectBind(Model model){
        List<String> businessAgencies = new ArrayList<String>();
        businessAgencies.add("Personal");
        businessAgencies.add("Commercial");
        businessAgencies.add("Life");
        businessAgencies.add("Benefits");
        businessAgencies.add("Health");
        businessAgencies.add("Non P and C");
        model.addAttribute("businessAgencies", businessAgencies);

        Ams360Policy ams360Policy = new Ams360Policy();
        List<String> billTypeList = new ArrayList<String>();
        billTypeList.add("Direct Bill");
        billTypeList.add("Agency Bill");
        model.addAttribute("billTypeList", billTypeList);
        ams360Policy.setBillTypeOptions(billTypeList);

        DirectBind directBind = new DirectBind();

        List<String> businessAgencyList = new ArrayList<String>();
        directBind.setBusinessAgencyList(businessAgencyList);

        model.addAttribute("directBind", directBind);

        return "directBind";
    }

    @RequestMapping(value="/addPolicy", params="addPolicy")
    public String addPolicy(final DirectBind directBind, Model model){
        directBind.getAms360Policies().add(new Ams360Policy());
        model.addAttribute("directBind", directBind);
        return "addPolicy";
    }

    @RequestMapping(value="/send", params="send")
        public String send(Model model, @ModelAttribute(value="directBind") DirectBind directBind){
        List<String> businessAgencyList = directBind.getBusinessAgencyList();

        Mail mail = new Mail();
        mail.setFrom("no-reply@hgitservices.com");
        mail.setTo(new String[]{"stacief@hgitservices.com"});
        mail.setSubject("Oli Affiliate - AMS360 & PMA Data Checklist");

        Map<String, Object> mailModel = new HashMap<String, Object>();
        mail.setModel(mailModel);

        try {
            emailService.sendSimpleMessage(mail, directBind);
        } catch (Exception e) {
            e.printStackTrace();
            return ("redirect:/?sentMessageFail");
        }
        return ("redirect:/?sentMessage");
    }

    @RequestMapping(value="/email")
    public String email(){
        return "emailMessage";
    }
}

2 个答案:

答案 0 :(得分:0)

如果您使用的是jQuery之类的JavaScript或js框架,则可以将按钮绑定到一个函数,该函数会将表单提交给指定的操作。

有关详细信息,请参见Process a Form Submit with Multiple Submit Buttons in Javascript

答案 1 :(得分:0)

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

  • 您可以为两个html输入按钮使用相同的名称和不同的值属性。按钮名称和值属性作为请求参数发送到控制器。这将使控制器能够确定单击了哪个按钮并采取适当的措施。

    <input id="send_id"  type="submit" name="action" value="send"/>
    <input id="addPolicy_id"  type="submit" name="action" value="addPolicy"/>

    @RequestMapping(value="/directBind")
    public String doExecute(@RequestParam("action") String action, Model model, @ModelAttribute(value="directBind") DirectBind directBind){
    if (action=="send"){
    //do something
    send();
    }
    if (action=="addPolicy"){
    //do something
    addPolicy();
    }
}