0

First the modal of team Update where Team is an entity in my spring mvc application Then The controller method and the updateTeam method implementation:

Here is the code:

<div class="modal fade" id="update-team-modal_${t.id}" tabindex="-1"
    role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5>Modification Equipe</h5>
            </div>
            <div class="modal-body">
                <form enctype="multipart/form-data" method="POST"
                    action="updateTeam" onsubmit="return validateTeamUpdate();">
                    <div class="form-group">
                        <input id="id" type="hidden" name="id" class="form-control"
                            value="${t.id}">
                    </div>

                    <div class="form-group">
                        <label for="name">Nom </label><span class="req">*</span>
                        <div class="input-group">
                            <input type="text" name="name" id="name"
                                class="form-control-large" value="${t.name }" autocomplete="off"
                                required="required" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="color">Couleur</label><span class="req">*</span> <input
                            type="text" id="color" name="color" class="form-control demo"
                            style="width: 200px;"
                            data-swatches="#fff|#000|#f00|#0f0|#00f|#ff0|#0ff"
                            required="required" value="${t.color }">
                    </div>

                    <div class="form-group">
                        <label for="validateur">Validateur</label> <span class="req">*
                        </span> <select class="form-control-small" id="validateur"
                            name="validateur" required="required">
                            <option value="${t.validateur.matricule}">${t.validateur.firstName
                                } ${t.validateur.lastName }</option>
                            <c:forEach var="u" items="${users}">
                                <option value="${u.matricule}">${u.firstName}
                                    ${u.lastName}</option>
                            </c:forEach>
                        </select>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-ok" type="submit"
                            style="background-color: #C6172E; color: white;">
                            <i class="glyphicon glyphicon-ok"></i>Editer
                        </button>
                        <button class="btn"
                            style="background-color: #5A6B80; color: white;"
                            data-dismiss="modal" onclick="this.form.reset();">
                            <i class="glyphicon glyphicon-remove"></i>Annuler
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@RequestMapping(value = "/updateTeam", method = RequestMethod.POST)
    public ModelAndView updateTeam(@RequestParam("id") long id,
            @RequestParam("name") String name,
            @RequestParam("validateur") String validateur,@RequestParam("color")String color) {

        Team team = teamService.getById(id);
        User validant = null;
        if (!validateur.equals("null"))
            validant = userService.getByMatricule(validateur);
        team.setName(name);
        team.setColor(color);
        team.setValidateur(validant);
        teamService.updateTeam(team);
        return new ModelAndView(new RedirectView("gestionEquipes"));
    }

@Override
    public void updateTeam(Team t) {
         teamRepository.saveAndFlush(t);    
    }

Bad Request 400 Etat HTTP 400 - Required String parameter 'color' is not present in spring mvc controller? La requête envoyée par le client était syntaxiquement incorrecte.

Can someone give me an idea how to solve this problem?

Scath
  • 3,777
  • 10
  • 29
  • 40
Espoirs
  • 1
  • 2

2 Answers2

0
@RequestMapping(value = "/addTeam", method = RequestMethod.POST) 
public ModelAndView addTeam(@RequestParam("name") String name,
    @RequestParam("color") String color, 
    @RequestParam("validateur") String validateur,
     RedirectAttributes redirectAttributes) {

            User validant = null;
            if (!validateur.equals("null"))
                validant = userService.getByMatricule(validateur);

            List<Team>teams=teamService.getAll();
            boolean uniqueName=true;
            boolean uniqueColor=true;
            for (Team team : teams){
                if (name.toLowerCase().equals(team.getName().toLowerCase()))
                    uniqueName = false;
                if(color.equals(team.getColor()))
                    uniqueColor=false;              
            }
            if (uniqueName == false) 
                redirectAttributes.addFlashAttribute("flashMessageErrorName", "error");
            else if(uniqueColor==false)
                redirectAttributes.addFlashAttribute("flashMessageErrorColor", "error");
            else if((uniqueName==false)&&(uniqueColor==false))
                    redirectAttributes.addFlashAttribute("flashMessageError", "error");             
            else{
            Team team = new Team(name, color);
            team.setValidateur(validant);
            boolean test = teamService.addTeam(team);
            if (test == true)
            redirectAttributes.addFlashAttribute("flashMessageAdd","success");

            }
            return new ModelAndView(new RedirectView("gestionEquipes"));
}
flyingfox
  • 13,414
  • 3
  • 24
  • 39
Espoirs
  • 1
  • 2
  • no i didn't unfortunately it works as i mentioned with addTeam method but not with updateTeam. I don't understand why? – Espoirs Apr 04 '18 at 16:48
  • i posted my Team entity; i did addTeam with this code and it works. I don't know why it doesn't work for updateTeam – Espoirs Apr 09 '18 at 15:16
  • please do not add your code as an answer,check your the submit code before update – flyingfox Apr 10 '18 at 01:58
0
package tn.softMaint.MiniPortail.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import com.google.gson.annotations.Expose;

@Entity
@Table(name = "teams")
/**
 * 
 * @author ajlassi
 * Classe d'entité des équipes
 * Les attributs avec l'annotation @Expose seront exposés à GSON
 */
public class Team implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name = "team_id")
    @Expose
    private long id;

    @Expose
    @Column(unique = true)
    @NotEmpty
    private String name,color;

    @Expose
    private boolean isActif;


    @ManyToOne(optional = false,fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @NotNull
    private User validateur ;

    @ManyToMany(fetch=FetchType.EAGER,mappedBy = "teams")
    private List<User> members = new ArrayList<User>();

    public Team() {
        super();

    }

    public Team(String name, String color) {
        super();
        this.name = name;
        this.color = color;
        setIsActif(true);
    }

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public boolean getIsActif() {
        return isActif;
    }

    public void setIsActif(boolean isActif) {
        this.isActif = isActif;
    }

    public User getValidateur() {
        return validateur;
    }

    public void setValidateur(User validateur) {
        this.validateur = validateur;
    }

    public List<User> getMembers() {
        return members;
    }

    public void setMembers(List<User> members) {
        this.members = members;
    }

    @Override
    public String toString() {
        return "Team [id=" + id + ", name=" + name + ", color=" + color
                + ", isActif=" + isActif + ", validateur=" + validateur
                + ", members=" + members + "]";
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Team other = (Team) obj;
        if (id != other.id)
            return false;
        return true;
    }

}
Espoirs
  • 1
  • 2