Luca De Angelis
Luca De Angelis

Reputation: 59

deploy project from spring boot to tomcat, error in post form

I have installed spring boot so far to perform manual execution. Passing on Tomcat I have problems running in the form specifically in the post, which I did not have before. I noticed that the url is not shown correctly the application name is lost.

@RequestMapping(value = "/registrazioneNuovoRuolo", method = RequestMethod.GET)
    public String showFormRegistrazioneRuolo(WebRequest request, Model model) {
        RuoloDto RuoloDto = new RuoloDto();
        model.addAttribute("ruolo", RuoloDto);
        model.addAttribute("visite", visitaRepository.findAll());
        return "registrazioneRuolo";
    }

    //Derivante dall'invio della form , effettuo la creazione del ruolo legando i dati acquisiti
    @RequestMapping(value = "/registrazioneNuovoRuolo", method = RequestMethod.POST)
    public ModelAndView registerNuovoRuolo(@Valid RuoloDto ruoloDto, 
            BindingResult result, WebRequest request, Errors errors , Model model) { 

        Ruolo ruolo = new Ruolo();
        if (!result.hasErrors()) {
            ruolo.setNome(ruoloDto.getNome().toUpperCase());
            if(ruoloDto.getIdVisite()!=null)
                ruolo.setVisite(visitaRepository.findAllById(ruoloDto.getIdVisite()));
            ruoloRepository.save(ruolo);
            return new ModelAndView("redirect:/homepageVIME","successInserimentoRuolo",true);
        }
        return new ModelAndView("redirect:/registrazioneNuovoRuolo","success",false);
    }

I correctly display the form, as soon as I confirm to go to the post method of the form the url is incorrect. I show you an example: http://localhost:8081/nameapp/registrazioneNuovoRuolo and it's right when i confirm http://localhost:8081/registrationNuovoRuolo that returning 404 because missing nameapp

Upvotes: 0

Views: 138

Answers (3)

Romil Patel
Romil Patel

Reputation: 13727

Here you have the same endpoints (@RequestMapping(value = "/registrazioneNuovoRuolo" ) with two different Methods which might cause the problem. When you send Request through web-browser it is GET request. To make POST request you should use Postman and similar tools if you have not created forms with the POST method. There is no problem with Tomcat as well

You will have your output with the following if you don't have any class level Request Mapping.

http://localhost:8081/registrationNuovoRuolo

Upvotes: 1

Luca De Angelis
Luca De Angelis

Reputation: 59

i have solved in different way: adding WebRequest on controller and the attribute model.addAttribute("contextPath", request.getContextPath()); So i have changed the action with this one. Thank so much ;)

Upvotes: 0

Alien
Alien

Reputation: 15878

Make sure you are giving slash before form action like below so that spring will prepend the context path (nameapp) automatically..

action="/registrazioneNuovoRuolo"

If above not works then you can prepend context path before like below.

"${#request.contextPath}/registrazioneNuovoRuolo" (thymeleaf)


action="${pageContext.request.contextPath}/registrazioneNuovoRuolo" (JSP)

Upvotes: 1

Related Questions