我尝试使用表单操作在按钮单击上调用带有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>
</h4>
</div>
<div class="portlet-body form">
<div class="tabbable portlet-tabs">
<ul class="nav nav-tabs">
</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> </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方法没有调用。请帮帮我。感谢
单击“保存”按钮后的更多信息:
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页面执行此操作。
来自浏览页面来源的源代码: