0

I am sending a <form> with an input type="file" and a submit button via http and now it seems to work fine. My Controller gets the file and I can check the file e.g. for correctness.

After I checked if the file is correct, I want to interact with my Front-End, I want to display a String in a label and maybe interact with drop-down-menus etc.

In the future I want to be able to display the data-results my program is creating.

How do I communicate from my controller (I guess via HTTP) with my HTML code?

Rob Quincey
  • 2,834
  • 2
  • 38
  • 54

2 Answers2

0

One way is make an ajax request.

You make a request in background to your server, when he responds you can make everything with JavaScript.

Jquery implement methods to use ajax request.

0

You can do it dynamically with Ajax, but its quite complicated:

Sending file together with form data via ajax post

$.ajax({
    type: 'POST',
    url: 'your_controller_address',

Ajax target would be your controller, and you'd need to make your controller return for example json data and process it in:

success: function(response)
        {
            if(response.success == true) {
                //yourcode to show notification
            }
        }

Controller:

@RequestMapping(value="/your_controller_address", method=RequestMethod.POST)
  @ResponseBody
  public String some_method(@ModelAttribute("form_model") Form_model_type form_model) {
    //do your stuff with model here
    return "{\"success\":\"true\"}";
  }

The other way, less efficient is to send form to another controller (<form action="/your_controller_address") which process your data and shows you notification, but it requires to reload whole page.

RaV
  • 1,029
  • 1
  • 9
  • 25
  • Thanks for the answer. Where do I put the ajax code, what is my controller adress and how can I show a notification within if(response.data == true) { //yourcode to show notification } – Felix Ludwig Jul 12 '17 at 10:39
  • If you using Spring framework: I added controller for you, but if you want to ask for more details (for example how to create form model in Spring), you should ask another question or google it I guess. – RaV Jul 12 '17 at 12:06
  • Ajax code you put between next to your html. Notification you can just create by simply using javascript `document.getElementById("notification_space").innerHTML = "success";`, dont forget to create also html element with id="notification_space". Its simple way, but there is a lot of fancy notification libs, just look around. – RaV Jul 12 '17 at 12:12
  • So is 'your_controller_adress' for example "/uploadXML" in my case? – Felix Ludwig Jul 12 '17 at 13:43
  • I guess yes. How did you send your data otherwise without defining controller and his address? – RaV Jul 13 '17 at 09:52
  • Yeah my controller got the adress localhost:8181/uploadXML. But is url: localhost:8181/uploadXml right then? – Felix Ludwig Jul 13 '17 at 16:37