jeudi 25 octobre 2018

Invoke service method if checkbox is pressed

I have few services and I want to call their methods in my controller if proper checkbox is pressed. E.g. if 3 checkboxes are pressed then it should call a specific method from 3 services in one request.

How should I reach that?

There is an example below:

@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";
}

And below my form class with names for checkboxes

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;
}

}

And my html pages

<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>

and jobs.html file

<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>

Any help would be appreciated




Aucun commentaire:

Enregistrer un commentaire