Null value in the controller after @PostMapping | Java

Viewed 230

I am trying to create a web application in Java using Spring.

The idea would be to call in @Get the model that displays the data entry form and then in @Post I would like to insert them in my persistence layer (SQL Server).

The problem is that I cannot make the object I pass in @Get be filled by the form, and therefore subsequently insert the data into the database. The attributes of the object I receive in @Post are all null and I can't understand why.

There is my controller:

package com.test.webapp.controller;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.test.weapp.domain.Categorie;
import com.test.weapp.domain.File;
import com.test.weapp.domain.Regioni;
import com.test.webapp.repository.CategorieRepository;
import com.test.webapp.repository.FileRepository;
import com.test.webapp.repository.RegioniRepository;

@Controller
@RequestMapping("/")
public class IndexController 
{
    @Autowired
    private CategorieRepository categorieRepository;
    
    @Autowired
    private RegioniRepository regioniRepository;
    
    @Autowired
    private FileRepository fileRepository;
    
    @GetMapping(value="/index")
    public String getWelcome(Model model)
    {
        File file = new File();
        
        model.addAttribute("regioni", getRegioni());
        model.addAttribute("categorie", getCategorie());
        model.addAttribute("Fileee", file);
        return "home";
    }
    
    @PostMapping(value="/index")
    public String GestInsFile (@ModelAttribute("Fileee") File file)
    {
            fileRepository.InsFile(file);
            return "home";
    }
    
    
    @ModelAttribute("regioni")
    public List<Regioni> getRegioni()
    {
        List<Regioni> regioni = regioniRepository.SelRegioni();

        return regioni;
    }
    
    @ModelAttribute("categorie")
    public List<Categorie> getCategorie()
    {
        List<Categorie> categorie = categorieRepository.SelCategorie();

        return categorie;
    }
    
    
}

Class File:

package com.test.weapp.domain;

import java.io.Serializable;
import java.util.Date;

import org.springframework.web.multipart.MultipartFile;

public class File implements Serializable {

    private static final long serialVersionUID = -316224596822077411L;

    private int id;
    private String titolo;
    private String regione;
    private String categoria;
    private String descrizione;
    private String note;
    private MultipartFile file;

    public File() {

    }

    public MultipartFile getFile() {
        return file;
    }

    public void setFile(MultipartFile file) {
        this.file = file;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitolo() {
        return titolo;
    }

    public void setTitolo(String titolo) {
        this.titolo = titolo;
    }

    public String getRegione() {
        return regione;
    }

    public void setRegione(String regione) {
        this.regione = regione;
    }

    public String getCategoria() {
        return categoria;
    }

    public void setCategoria(String categoria) {
        this.categoria = categoria;
    }

    public String getDescrizione() {
        return descrizione;
    }

    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

}

Class Categorie

package com.test.weapp.domain;

import java.io.Serializable;

public class Categorie implements Serializable 
{

    private static final long serialVersionUID = 6904000811482985125L;
    
    private int id;
    private String categoria;
    
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCategoria() {
        return categoria;
    }

    public void setCategoria(String categoria) {
        this.categoria = categoria;
    }

    public Categorie() {
        
    }
    
}

Class Regioni

package com.test.weapp.domain;

import java.io.Serializable;

public class Regioni implements Serializable 
{
    
    private static final long serialVersionUID = 7083492150275199488L;
    
    private int id;
    private String regione;
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getRegione() {
        return regione;
    }
    public void setRegione(String regione) {
        this.regione = regione;
    }
    public Regioni() {
        
    }
    
}

Class FileRepositoryImpl that inserts into the table

package com.test.webapp.repository;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.test.weapp.domain.File;

@Repository
public class FileRepositoryImpl implements FileRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<File> SelFileByFilter(String id) {
        return null;
    }

    @Override
    public void InsFile(File file) {
        String Sql = "insert into [dbo].[FILE] VALUES ('" + file.getTitolo() + "','" + file.getRegione() + "','"
                + file.getCategoria() + "','"  + file.getDescrizione() + "','" + file.getNote()
                + "');";

        jdbcTemplate.update(Sql);
    }

    @Override
    public void DelFile(String id) {

    }

}

And here is my JSP showing the Home.jsp compile form

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!doctype html>
<html lang="it">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport"
    content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Altri CSS -->
<link href="<c:url value="/static/css/style.css" />" rel="stylesheet">

<title>Area Riservata</title>
</head>
<body>

    <h1>Area Riservata</h1>
    <p>Bim Italia Srl</p>
    
    <main>
        <form:form action ="index" method="POST" modelAttribute="Fileee"
            enctype="multipart/form-data">
            <table>
            
            
                <CAPTION>Informazioni File</CAPTION>
                <thead>
                </thead>
                <tfoot>
                </tfoot>
                <tbody>
                    <tr>
                        <td data-title='Provider Name'>File:</td>
                        <td><form:input path="file" type="file" class="form:input-large" /></td>
                    </tr>
                    <tr>
                        <td data-title='Provider Name'>Titolo:</td>
                        <td><form:input path="titolo" type="text" class="form-control" /> </td>
                    </tr>
                    <tr>
                        <td data-title='Provider Name'>Regione:</td>
                        <td><form:select path="regione" class="form-control">
                                <form:options items="${regioni}" itemValue="Id" itemLabel="Regione" />
                            </form:select></td>
                    </tr>
                    <tr>
                        <td data-title='Provider Name'>Categoria:</td>
                        <td><form:select path="categoria" class="form-control">
                                <form:options items="${categorie}" itemValue="Id" itemLabel="Categoria" />
                            </form:select></td>
                    </tr>
                    <tr>
                        <td data-title='Provider Name'>Descrizione:</td>
                        <td><form:textarea path="descrizione" rows="2" cols="33" autoResize="true" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;" />
                        </td>
                    </tr>
                    <tr>
                        <td data-title='Provider Name'>Note:</td>
                        <td><form:textarea path="note" rows="2" cols="33" autoResize="true" style="font-family: Helvetica Neue,Helvetica,Arial,sans-serif;" />
                        </td>
                    </tr>
                    
                </tbody>
                
            </table>
            
        <input type="submit" id="btnAdd" />
                </form:form>
        
    </main>

        
    <br />

    <script src="/static/js/index.js"></script>

</body>
</html>

Here the data entered are all null!

Please help me understand why my form does not return the fields I filled in!

Thanks for the attention.

1 Answers

add File in your getWelcome controller @RequestBody annotation

   @GetMapping(value="/index")
    public String getWelcome(@RequestBody File file, Model model)
    {
        
        model.addAttribute("regioni", file.getRegioni());
        model.addAttribute("categorie", file.getCategorie());
        model.addAttribute("Fileee", file);
        return "home";
    }
Related