Etat HTTP 400 - Spring mvc控制器中不存在必需的字符串参数'color'?

时间:2018-04-04 08:34:21

标签: spring-mvc http-request-parameters

首先是团队更新的模式,其中Team是我的spring mvc应用程序中的实体然后是控制器方法和updateTeam方法实现:

以下是代码:

<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);    
    }

错误请求400 Etat HTTP 400 - 必需字符串参数'color'在spring mvc控制器中不存在? Larequêteenvoyéeparleclientétaitlanguiqueiquementincorrecte。

有人可以告诉我如何解决这个问题吗?

2 个答案:

答案 0 :(得分: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"));
}

答案 1 :(得分: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;
    }

}