Spring Boot with thymeleaf method not calling

Siddharth Sachdeva

I'm trying to call a java method with post mapping on a button click using form actions, I tried a lot, unfortunately, the method is not calling, shocking to me is that the nearly same code is working absolutely fine in some other project.

Here's the controller class:

@Controller
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    GenericClient client;

    @GetMapping("/")
    public ModelAndView createDashboardView(){
        ModelAndView modelAndView = new ModelAndView("views/question");
        List<QuestionDTO> questions = client.genericClient(null, "question/fetchAllQuestions");
        QuestionsList questionsList = new QuestionsList();
        questionsList.setId1(questions.get(0).getId());
        questionsList.setQuestion1(questions.get(0).getDescription());
        questionsList.setId2(questions.get(1).getId());
        questionsList.setQuestion2(questions.get(1).getDescription());
        questionsList.setId3(questions.get(2).getId());
        questionsList.setQuestion3(questions.get(2).getDescription());
        modelAndView.addObject("questionsList", questionsList);

        return modelAndView;
    }

    @PostMapping("/save")
    public ModelAndView onSaveClick(QuestionsList questionsList, BindingResult result){
        ModelAndView modelAndView = new ModelAndView("views/question");
        System.out.println("Inside method");

        System.out.println(questionsList.getQuestion2());

        return modelAndView;
    }



}

Here's the HTML file with Thymeleaf:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://thymeleaf.org"
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorator="views/master">

<body>
    <div layout:fragment="page-content">
        <div class="container-fluid">
            <!-- BEGIN PAGE CONTENT-->

            <div class="row-fluid errmsg"  id="dvError"
                visible="false">
                <div style="width: 100%; text-align: center;">
                    <div class="message success">
                        <p></p>
                    </div>
                </div>
            </div>


            <div class="row-fluid">
                <div class="span12">
                    <!-- BEGIN SAMPLE FORM PORTLET-->
                    <div class="portlet box blue tabbable">
                        <div class="portlet-title">
                            <h4>
                                <i class="icon-reorder"></i><span class="hidden-480">Add/Edit
                                    Questions</span>&nbsp;
                            </h4>
                        </div>
                        <div class="portlet-body form">
                            <div class="tabbable portlet-tabs">
                                <ul class="nav nav-tabs">&nbsp;
                                </ul>
                                <div class="tab-content">

                                    <div class="tab-pane active" id="portlet_tab1">
                                        <!-- BEGIN FORM-->
                                        <div class="control-group"></div>

                                    </div>
                                    <div class="mytable" style="overflow: auto;">
                                        <div class="control-group">
                                            <div class="controls questionMar01 questionColor ">
                                                <!--  Note:- Please enter @@ in your question where you want company name -->
                                            </div>
                                        </div>

                        <form  class="form-horizontal"  action="#" th:action="@{/question/save}" th:object="${questionsList}" method="POST">

                                    <div>
                                        <input th:field="*{question1}" style="height:30px"  id="txtQuestion1"
                                            placeholder="Enter Question 1 Here" class="m-wrap large"
                                            type="text" /> 
                                            <br/><br/>
                                            <input th:field="*{question2}" style="height:30px"  id="txtQuestion2"
                                            placeholder="Enter Question 2 Here" class="m-wrap large"
                                            type="text"/>
                                            <br/><br/>
                                            <input  th:field="*{question3}" style="height:30px"  id="txtQuestion3"
                                            placeholder="Enter Question 3 Here" class="m-wrap large"
                                            type="text"  /> 
                                    </div>

                                        <div>&nbsp;</div>


                                        <div class="form-actions">
                                            <button type="submit" class="btn blue okMark">Save</button>
                                            <!-- <button id="btncancel" class="btn cancel" 
                                                OnClick="btncancel_Click">Cancel</button> -->
                                        </div>
                                    </form>
                                    <!-- END FORM-->
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- END SAMPLE FORM PORTLET-->
                </div>
            </div>
            <!-- END PAGE CONTENT-->
        </div>
    </div>
</div>
</body>
</html>

Here I'm trying to call /question/save/ post mapping on Save button click, unfortunately, onSaveClick method is not calling. Please help me out. Thanks

Project Structure: Project Structure

Network tab after Save button click: enter image description here

More information:

2018-01-18 17:40:15.741  INFO 9264 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/question/],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.beezyadmin.controller.QuestionController.createDashboardView()
2018-01-18 17:40:15.742  INFO 9264 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/question/save],methods=[POST]}" onto public org.springframework.web.servlet.ModelAndView com.beezyadmin.controller.QuestionController.onSaveClick(com.beezyadmin.dto.QuestionsList,org.springframework.validation.BindingResult)

/question/save is getting mapped as shown in the logs and also I'm able to call the method directly using PostMan, however, I'm unable to do so from Thymeleaf html page on Save button click.

Source code from view page source:

enter image description here

xingbin

Finally, there is something wrong the layout file, which causes clicking button perform an GET instead POST automatically for some reason. enter image description here After removing layout file every thing works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive