How to map Bootstrap Modal to Spring MVC controller

I have a form in Bootstrap Modal and I want my Spring MVC controller to listen to this. My problem is that modal does not generate href because it is inside the current page, so I cannot only match modal in my Spring MVC controller.

I need this because I want to show errors in the bindingresult object. How can i do this?

This is my modal: http://www.bootply.com/zerZIYpNAF Let's say it is located in index.jsp, so the imaginary path would be / index # myModal.jsp or something like that.

@RequestMapping(value="/send", method = RequestMethod.GET) public String get(Dummybean bean){ return "??"; //index#myModal } @RequestMapping(value="/send", method = RequestMethod.POST) public String post(@Valid @ModelAttribute("dummy") DummyBean bean, BindingResult bindingResult){ if(bindingResult.hasErrors()){ return "??"; //index#myModal } //do something } public class DummyBean{ @NotNull private String name; public String getName() { return username; } public void setName(String name) { this.name = name; } 
+5
source share
2 answers

You cannot directly call bootstrap modal to pop up using the controller. There you cannot bind the form using Spring. But you can achieve this with Ajax. You should use the form as a regular HTML form without using spring tags.

 function searchAjax() { var data = {} data["query"] = $("#query").val(); $.ajax({ type : "POST", contentType : "application/json", url : "${home}search/api/getSearchResult", data : JSON.stringify(data), dataType : 'json', timeout : 100000, success : function(data) { console.log("SUCCESS: ", data); display(data); }, error : function(e) { console.log("ERROR: ", e); display(e); }, done : function(e) { console.log("DONE"); } }); } 

This is an ajax example for you to get an idea. You must HttpServletRequest retrieve data from the controller. The above example is taken from http://www.mkyong.com/spring-mvc/spring-4-mvc-ajax-hello-world-example/

0
source

1) create a new function only for verification

2) create a js function, preferring to use jquery and sending an ajax request to work in the first step.

3) depends on the status of the verification, will process errors or send the form in its entirety.

read this article, it fully answered your javacodegeeks.com question

-1
source

Source: https://habr.com/ru/post/1205967/


All Articles