vendredi 2 décembre 2016

Why is my Form not processed in Spring MVC?

I encounter a problem with a form treatment with Spring :

Etat HTTP 400 - Client request is syntaxically incorrect (translated from french).

The POST method should send to controller a text value and checkboxes values that are part of a form.

Here is the JSP :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://ift.tt/QfKAz6" prefix="c"%>
<%@taglib uri="http://ift.tt/IED0jK" prefix="mvc"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ift.tt/kTyqzh">
<html>
<head>
<style>
.error {
    color: #ff0000;
    font-weight: bold;
}

#listOfSkills tr:first-child {
    font-weight: bold;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Liste des Jobs</title>
</head>
<body>
    <mvc:form modelAttribute="NewJob" action="job" method="POST">
        <table id="listOfJobs" border="1">
            <thead>
                <tr>
                    <td>${JobSize}Jobs</td>
                    <td>Compétences</td>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${requestScope['Jobs']}" var="job">
                    <tr>
                        <td>${job.jobName}</td>
                        <td><c:forEach items="${job.jobSkills}" var="skill">
                            ${skill.pk.skill.skillName}<br>
                            </c:forEach></td>
                    </tr>
                </c:forEach>
                <tr>
                    <td><mvc:label path="jobName">Nouveau job</mvc:label> <mvc:input
                            path="jobName" /></td>
                    <td><c:forEach items="${requestScope['Skills']}" var="skill">
                            <mvc:checkbox path="jobSkills" label="${skill.skillName}"
                                value="${skill.skillId}" />
                            <br>
                        </c:forEach></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Submit" /></td>
                </tr>
            </tbody>
        </table>
    </mvc:form>
</body>
</html>

Things look like this :

enter image description here

The first part of the table show all the existing jobs and associated skills. At the end of the table, there's a form to create a new job (text) and checkboxes to choose associated skills.

The problem occurs when I use the checkboxes.

A single value for the Job name is persisted in the database, but when I use the checkboxes, the form is not processed.

"Job" and "Skill" are 2 different entities. A relation with attributes exists between them : "JobSkills". "JobSkills" entity uses a "JobSkillsPK" entity to generate a unique PK for each [job,skill] couple in the JobSkills entity.

Job :

@Entity(name = "job")
public class Job implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "JOB_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long jobId;

    @Column(name = "JOB_NAME")
    private String jobName;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.job", cascade = CascadeType.ALL)
    private Set<JobSkills> jobSkills = new HashSet<JobSkills>();

Skill :

@Entity(name = "skill")
public class Skill implements Serializable {

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

    @Id
    @Column(name = "SKILL_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long skillId;

    @Column(name = "SKILL_NAME")
    private String skillName;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.job", cascade = CascadeType.ALL)
    private Set<JobSkills> skillJobs = new HashSet<JobSkills>();

JobSkill :

@Entity(name = "jobskills")
public class JobSkills implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private JobSkillPK pk;

    @Column(name = "XP_YEARS")
    private int xpInYears;

JobSkillPK :

@Embeddable
public class JobSkillPK implements Serializable {

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

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "ID_JOB")
    private Job job;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "ID_SKILL")
    private Skill skill;

Does someone know what is malformed in my POST request that could explain why a single text is processed but not checkboxes ?

PS : all the skills persisted have been added manually in the database.

Thanx by advance.




Aucun commentaire:

Enregistrer un commentaire