首页 文章

没有调用[复制]的百里香叶法 spring 启动

提问于
浏览
1

这个问题在这里已有答案:

我试图用按钮单击使用表单操作调用一个带有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" /> 
                                            

<input th:field="*{question2}" style="height:30px" id="txtQuestion2" placeholder="Enter Question 2 Here" class="m-wrap large" type="text"/>

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

在这里,我试图在Save按钮上调用/ question / save / post mapping,不幸的是,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

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

  • 1

    你的控制器有映射“/ question / save”

    BUT

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

    注意尾随斜线 .

相关问题