使用thymeleaf方法的Spring Boot不会调用

时间:2018-01-18 03:26:19

标签: java spring spring-mvc spring-boot thymeleaf

我尝试使用表单操作在按钮单击上调用带有post贴图的java方法,我尝试了很多,不幸的是,该方法没有调用,令我震惊的是,几乎相同的代码绝对正常工作在其他一些项目中很好。

这是控制器类:

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



}

这是包含Thymeleaf的HTML文件:

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

这里我试图在保存按钮上点击/问题/保存/发布映射,不幸的是,onSaveClick方法没有调用。请帮帮我。感谢

项目结构: Project Structure

单击“保存”按钮后的

网络选项卡: enter image description here

更多信息:

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已按日志显示进行映射,我也可以使用PostMan直接调用该方法,但是,我无法通过保存按钮上的Thymeleaf html页面执行此操作。

来自浏览页面来源的源代码:

enter image description here

2 个答案:

答案 0 :(得分:1)

最后,布局文件有问题,导致点击按钮由于某种原因自动执行GET而不是POST。 enter image description here 删除布局文件后,每件事都有效。

答案 1 :(得分:0)

您的控制器已映射“/ question / save”

<强> BUT

你的行动有一个“/ question / save /”

的网址

注意尾随斜线。