如果选中复选框,则调用服务方法

时间:2018-10-25 12:48:03

标签: spring spring-mvc checkbox thymeleaf

我几乎没有服务,并且如果按下正确的复选框,我想在控制器中调用它们的方法。例如。如果按下3个复选框,则它应在一个请求中从3个服务中调用特定方法。

我应该如何做到?

下面有一个示例:

@Controller
@RequestMapping(value={"invoke"})
public class Controller {

@Autowired
private ServiceOne serviceOne;

@Autowired
private ServiceTwo serviceTwo;

@Autowired
private ServiceThree serviceThree;

@RequestMapping(value = "", method = RequestMethod.GET)
public String show(Model model){
    AllJobsForm allJobsForm = new AllJobsForm();
    model.addAttribute("jobsList", allJobsForm.getJobFormList());
    model.addAttribute("jobForm", new JobForm());
    return "show";
}

@RequestMapping(value = "operation", method = RequestMethod.POST)
public String Export(Model model, AllJobsForm allJobsForm) {
    //serviceOne.doSomething();
    //serviceTwo.doSomething();        
    //serviceThree.doSomething();
    return "redirect:/home";
}

在我的表单类下方,带有复选框的名称

public class AllJobsForm {

private List<JobForm> jobFormList = new ArrayList<>();

public AllJobsForm() {
    create();
}

private void create(){
    jobFormList.add(new JobForm("serviceOne"));
    jobFormList.add(new JobForm("serviceTwo"));
    jobFormList.add(new JobForm("serviceThree"));
}

public List<JobForm> getJobFormList() {
    return jobFormList;
}

public void setJobFormList(List<JobForm> jobFormList) {
    this.jobFormList = jobFormList;
}

}

还有我的html页面

<div layout:fragment="content_container">
<form action="#"  class="form-static" th:action="/invoke/operation" method="post">
    <div class="form-static-body">
        <th:block th:replace="templates/jobs"></th:block>
    </div>

    <div class="form-static-footer">
        <div class="container-fluid">
            <div class="form-group">
                <button type="submit" name="submit" value="submit" class="btn btn-primary">doOperation</button>
            </div>
        </div>
    </div>
</form>

和jobs.html文件

<div class="panel panel-default">
<div class="panel-heading">
    <strong>Lorem Ipsum...</strong>
</div>
<div class="panel-body">
    <p>
        Lorem Ipsum...
    </p>
    <div class="table-responsive">
        <table class="table table-hover">
            <thead>
            <tr th:each="jobForm : ${jobsList}">
                <input type="checkbox" th:field="*{jobsList}" th:value="${jobForm.jobName}"/>
                <label th:text="${jobForm.jobName}"></label>
            </tr>
            </thead>
        </table>
    </div>
</div>

任何帮助将不胜感激

0 个答案:

没有答案