How to send form data to controller

21,919

Solution 1

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..

Solution 2

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;
    }
Share:
21,919
bircastri
Author by

bircastri

Updated on December 26, 2020

Comments

  • bircastri
    bircastri over 3 years

    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;
        }
    }
    
    • Sridhar
      Sridhar over 9 years
      Can you put up more details? XML configuration files, controller scanned or not?
  • bircastri
    bircastri over 9 years
    I try to apply your change, I have insert this: @RequestMapping("/RegistrazioneController") but now when I try to open this page I received Error 404.
  • Danyal Sandeelo
    Danyal Sandeelo over 9 years
    did you change the value in action in form?
  • bircastri
    bircastri over 9 years
    I have edit my questions and I have update the method
  • MiraTech
    MiraTech about 4 years
    is DTO object a mandatory I mean should I create it? can't I send only one input as String?