@Restcontroller returns to same page but with different URL

Asked

Viewed 65 times

0

I’m using @Restcontroller to manage different Forms/actions on the same page (with Thymeleaf), and it’s working properly, but when calling method (triggered via button on page) when returning to the page the URL in the browser is not 'reset' even if I recreate my Modelview Every time:

Example: Initial URL: http://localhost:8090/project/v1/controller/

URL after some action on Restcontorller: http://localhost:8090/project/v1/controller/actionA

Desired URL when returning to page after button-fired method: http://localhost:8090/project/v1/controller/

Code: //method I use to open the page @Requestmapping(value = "/", method = Requestmethod.GET) public Modelandview index(){ Modelandview Mav = new Modelandview(); Mav.addObject("myObj", this.someMethods()); Mav.setViewName("index"); //my page Return Mav; }

//método disparado pelo botão na página, contendo a ação
@RequestMapping(value = "/actionA", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = "action=actionA")
    public ModelAndView actionA(...) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("myObj", this.someMethods());
        mav.setViewName("index"); //a mesma página
        return mav;
}

I cannot use the same value parameter because I use it in my Forms/actions to define which method to fire in my restcontroller, example:

<form id="action" method="POST" th:object="${Object}"
              th:action="@{/v1/emulador-central/action}">
...
   <button type="submit" name="action" value="actionA" class="btn btn-primary">Aprovar</button>
   <button type="submit" name="action" value="actionB" class="btn btn-primary" style="float:right;">Reprovar</button>
...
</form>

I tried to use redirect and it didn’t work, I also tried to create a POST method to create Modelandview and then return it to my index page, another flaw. Can anyone guide me how to make it work properly? Thank you.

  • If someone is facing the same problem: so far I have not found the expected solution, when I find update.

1 answer

0

Try this:

<form id="action" th:action="@{/v1/emulador-central/}">
   <button type="submit" th:name="action" th:value="actionA" class="btn btn- 
     primary">Aprovar</button>
   <button type="submit" th:name="action" th:value="actionB" class="btn btn-primary" 
     style="float:right;">Reprovar</button>
</form>

    @RequestMapping("/")
    public ModelAndView index(@RequestParam(value = "action", required = false) String action){
        //system.out.println(action);
        ModelAndView mav = new ModelAndView();
        mav.addObject("myObj", this.someMethods());
        mav.setViewName("index"); //a mesma página
            return mav;
    }
  • Opa @Kaique, from what I understood your suggestion would be in the method index, right? By the way I was not clear (I will edit the question) but this method I use to open the screen initially, generating me the url I want, straight. My problem is that when shooting one of the buttons of some form, I go to the second method, in case the actionA, which also does what I want (with service and client themselves), but concatenates the action in the url, this is the point I’m trying to adjust. Even so, I tried to perform the adjustment (no actionA) as indicated and I was unsuccessful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.