Search code examples
javaspringjspspring-mvcrequest-mapping

wrong URI passed through spring form action


my project is called CV; in a jsp I have the following spring:form action in the "anagrafica.jsp":

<form:form modelAttribute="anagrafica" action="${pageContext.servletContext.contextPath}/formazione/save" method="post">

in the controller I have:

package it.curriculum.controller;

@Transactional
@Controller
@RequestMapping("/anagrafica")
public class AnagraficaController {
    @Autowired
    private ApplicationContext context;
    @Autowired
    private AnagraficaDao anagraficaDao;

    @RequestMapping(method=RequestMethod.GET)
    public String showHome(@ModelAttribute("tecnologia")Tecnologia tecnologia, ModelMap map){
        map.addAttribute(context.getBean("anagrafica"));
        return "anagrafica";
    }

    @Transactional
    @RequestMapping(value="/save", method=RequestMethod.POST)
    public String validateAnag(@ModelAttribute("anagrafica") Anagrafica anagrafica, @ModelAttribute("formazione") Formazione formazione){
        anagraficaDao.save(anagrafica);
        return "formazione"
    }
}

the problem is when I click on the submit button of the anagrafica form: it executes the code inside the previous controller and then it redirects to:

localhost:8080/CV/formazione/save Why so? I just wanted to be redirected to

localhost:8080/CV/formazione

not

localhost:8080/CV/formazione/save. Thank you very much for the help.


Solution

  • You are missing redirect keyword / merchanism.

    You need to redirect as follows,

    @RequestMapping("/save"){
        ...
    return "redirect:/formazione";