Search code examples
springspring-mvchttp-status-code-404spring-form

Why when I submit a form into a Spring MVC application I obtain this HTTP Status 404 error page?


I am pretty new in Spring MVC and I have the following problem trying to submit a form defined into a view.

So I have a view named consultazione.jsp that contains this form:

<form:form action="/consultazioneRicercaForm" method="post" modelAttribute="consultazioneRicercaForm">
    <div class="row">
        <div class="col-md-4">

            <label>Regioni:</label>
            <form:select path="regioneSelezionata" id="selReg">
                <form:options items="${listaRegioni}" itemLabel="desReg" itemValue="codReg" />
            </form:select>
        </div>


        <div class="col-md-4">
            <label>Province:</label>
            <form:select path="provinciaSelezionata" id="selProv">

            </form:select>
        </div>


        <div class="col-md-4">
        </div>

    </div>

    <div class="row">
        <div class="col-md-4">
            <label>Codice Meccanografico:</label>
            <form:input path="codMec" />
        </div>
    </div>

    <input type="submit" value="Save Changes" />
</form:form>

As you can see in the previous code when the form is submitted it is generated a POST request toward the /consultazioneRicercaForm with a modelAttribute="consultazioneRicercaForm" object that represent the form object in which are stored the values inserted in the form.

So this is my controller class:

@PropertySource("classpath:wifi.properties")
@Controller
public class MinisterialeController {

    @Autowired
    GeograficaService geograficaService;

    @RequestMapping(value = "consultazioneMinisteriale", method = RequestMethod.GET)
    public String consultazione(@ModelAttribute ConsultazioneRicercaForm consultazioneRicercaForm, Locale locale, Model model) {

        model.addAttribute("consultazioneRicercaForm", consultazioneRicercaForm);
        List<Twb1012Regione> listaRegioni = geograficaService.getListaRegioni();

        System.out.println("Numero regioni: " + listaRegioni.size());

        model.addAttribute("listaRegioni", listaRegioni);
        model.addAttribute("regioneSelezionata", new Twb1012Regione());

        return "utenteMinisteriale/consultazione";
    }


    @RequestMapping(value = "/provinceDiUnaRegione.json")
    @ResponseBody
    public String getProvince(String codiceRegione) {

        System.out.println("INTO getProvince()");

        List<Twb1013Provincia> provinceDiUnaRegioneList =  geograficaService.getListaProvinceDiUnaRegione(codiceRegione);

        try {
            String listaProvince = new ObjectMapper().writeValueAsString(provinceDiUnaRegioneList);
            return listaProvince;
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        return "";


    }


    @RequestMapping(value = "consultazioneRicercaForm", method=RequestMethod.POST)
    public String consultazioneRicercaForm(@ModelAttribute ConsultazioneRicercaForm consultazioneRicercaForm) {
        System.out.println(consultazioneRicercaForm.getCodMec());
        return "redirect:/consultazioneMinisteriale";
    }
}

As you can see this is the method that handle the previous post request representing the post submit operation:

    @RequestMapping(value = "consultazioneRicercaForm", method=RequestMethod.POST)
    public String consultazioneRicercaForm(@ModelAttribute ConsultazioneRicercaForm consultazioneRicercaForm) {
        System.out.println(consultazioneRicercaForm.getCodMec());
        return "redirect:/consultazioneMinisteriale";
    }

The problem is that when I submit the form clicking on the submit button I obtain this error page (but I can't see no error in the stacktrace):

JBWEB000065: HTTP Status 404 - /consultazioneRicercaForm

JBWEB000309: type JBWEB000067: Status report

JBWEB000068: message /consultazioneRicercaForm

JBWEB000069: description JBWEB000124: The requested resource is not available.
JBoss Web/7.2.2.Final-redhat-1

Why? What could be the problem? How can I try to fix it?


Solution

  • Your form's action URL is absolute (it starts with /):

    <form:form action="/consultazioneRicercaForm" method="post" ...
    

    A URL starting with / will look for the page at root of your domain (say http://example.com/consultazioneRicercaForm).

    In other words, if the HTML page that contains that form is at http://example.com/theNameOfMyApp/consultazione.jsp, then that action will post the form to http://example.com/consultazioneRicercaForm not http://example.com/theNameOfMyApp/consultazioneRicercaForm as you may be expecting.

    How to be sure?

    What is the URL of your application?

    http://example.com/consultazione.jsp or http://example.com/theNameOfMyApp/consultazione.jsp?

    If is the latter, then your problem is likely to be the / in the beginning of the URL as mentioned above.

    To be more specific, in the first URL, you app is deployed to the ROOT context. In the second URL, your app is deployed to the theNameOfMyApp context.

    404 could be also just a nonexistent URL

    It is very common for the problem to be just a typo in the action mapped URL. In your case, this is less likely, since your controller MinisterialeController has a method mapping that path:

    @RequestMapping(value = "consultazioneRicercaForm", method=RequestMethod.POST)
        public String consultazioneRicercaForm(
    

    Just don't forget, that mapping alone enables the consultazioneRicercaForm path. But it still has the context of the application.

    Solution:

    Assuming your application is not deployed to the ROOT context (check the How to be sure? above), remove the / from the action attribute of your form:

    <form:form action="consultazioneRicercaForm" method="post" ...
    

    This way, if the HTML page that contains that form is at http://example.com/theNameOfMyApp/consultazione.jsp, then that action will post the form to http://example.com/theNameOfMyApp/consultazioneRicercaForm.