2

I have a simple J2ee application. I want to create a simple form of registration, then send the data at controller to storage the data in mysql database. So I have this jsp page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
        <meta charset="utf-8">
        <title>Registrazione</title>
    </head>
<body>

    <h2>${msg}</h2>
    <form action="/sendDati" method="POST">
        <label>Nome <span class="color-red">*</span></label>
        <input type="text" class="form-control margin-bottom-20" name="nome" required></input>      

        <label>Email <span class="color-red">*</span></label>
        <input type="email" class="form-control margin-bottom-20" name="email" required></input>

        <label>Password <span class="color-red">*</span></label>
        <input type="password" class="form-control margin-bottom-20" name="password" required></input>                          

        <label>Conferma Password <span class="color-red">*</span></label>
        <input type="password" class="form-control margin-bottom-20" name="password1" required></input>
        <button type="submit" class="btn btn-primary">REGISTRATI</button>
    </form>
</body>
</html>

and this is the controller:

package com.springmvcapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RegistrazioneController {

    @RequestMapping("/registrazione")
    public ModelAndView helloWorld(){

        ModelAndView model = new ModelAndView("registrazione");
        model.addObject("msg", "hello world");
        return model;
    }

    @RequestMapping(value = "/sendDati")
    public String getDataFromForum(@RequestParam String nome){
        System.out.println("pippo");
        return nome;
    }
}

But when I try to click on Registrazione button I received this error

HTTP Status 404 - /sendDati

How can I fixed this error?

EDIT:

springmvcapp-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.springmvcapp.controller" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
  <display-name>SpringMVCApp</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>springmvcapp</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvcapp</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

EDIT 2 I have change this. When I try to open my registrazione.jsp page I use this method:

<form action="RegistrazioneController/registrazione.html">
                            <button class="btn btn-primary btn-lg" data-toggle="modal" onclick="submit">
                               Registrazione
                            </button>
                        </form>

My Jsp form is this:

<form action="RegistrazioneController/sendDati" method="POST"/>

My RegistrazioneController class is this:

@Controller
@RequestMapping("/RegistrazioneController")
public class RegistrazioneController {

    @RequestMapping("/registrazione")
    public ModelAndView helloWorld(){

        ModelAndView model = new ModelAndView("registrazione");
        model.addObject("msg", "hello world");
        return model;
    }

    @RequestMapping(value = "/sendDati")
    public String getDataFromForum(@RequestParam String nome){
        System.out.println("pippo");
        return nome;
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
bircastri
  • 2,169
  • 13
  • 50
  • 119

3 Answers3

2

Subject:
POST Html-Form data towards Spring-Boot @Controller as one DTO Object example

The following demonstrates:
- SPRING BOOT Application
- Html-Form with 2 fields. The Form POSTs data towards the controller single method.
- TestController with test() method that Receives an object TestObj that includes the 2 fields.

Hope that it helps..
Y.Lev

HTML

<form action="/test" method="post">
     f name:<input name="fname" value="yosi"/><br>
     L name:<input name="lname" value="lev"/><br>
     <input type="submit"/>
 </form>

APPLICATION MAIN

@SpringBootApplication
public class ApplicationMain {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationMain.class, args);
    }
}

CONTROLLER

@Controller
public class TestController {
    @RequestMapping(value="/test", method=RequestMethod.POST)
    public @ResponseBody String test( TestObj testObj){
        return "HELLO TEST : " + testObj;
    }
}

DTO Object

public class TestObj {
    public String fname;
    public String lname;

    public String getFname() { return fname;}
    public void setFname(String fname) { this.fname = fname; }
    public String getLname() { return lname; }
    public void setLname(String lname) { this.lname = lname; }

    @Override
    public String toString() {
        return "TestObj [fname=" + fname + ", lname=" + lname + "]";
    }
}

Enjoy..

ylev
  • 2,313
  • 1
  • 23
  • 16
1

On your form action add ${pageContext.request.contextPath}/NameOfTheProject/sendDati

then execute your code inside

@RequestMapping(value = "/sendDati")
    public String getDataFromForum(@RequestParam String nome){
        System.out.println("pippo"); //execute code here
        return nome;
    }
Nb12se
  • 513
  • 6
  • 21
-1

It is a 404 error which means the web app server didn't find the resource. The reason is, you have not given your controller a name.

You have to define the name of the controller like

 @Controller
 @RequestMapping("/controllerName")

and you need to modify the action value in the form as well.

action="appName/controllerName/methodName"
JeanValjean
  • 17,172
  • 23
  • 113
  • 157
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78