Spring的“<form:select ..”不=“”work,=“”http =“”status =“”400 =“”

时间:2016-12-03 15:44:55

标签: java spring jsp spring-mvc jstl

=“”

“项目”

@Entity
@Table(name = "projects")
public class Project extends NamedEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;

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

@Column(name = "start_date")
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date startDate;

@Column(name = "end_date")
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Date endDate;

@Column(name = "description")
private String description;

@Column(name = "notes")
private String notes;

@ManyToOne
@JoinColumn(name = "project_status_id")
private ProjectStatus projectStatus;

@ManyToOne
@JoinColumn(name = "project_objective_id")
private ProjectObjective projectObjective;

// Getters & Setters...
}

“ProjectStatus”

@Entity
@Table(name = "project_statuses")
public class ProjectStatus {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;

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

// Getters & Setters...
}

“ProjectObjective”

@Entity
@Table(name = "project_objectives")
public class ProjectObjective {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;

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

// Getters & Setters...
}

“控制器”

@Controller
public class ProjectController {

@Autowired
private FirmManagerService firmManagerService;

@ModelAttribute("statuses")
public Collection<ProjectStatus> populateProjectStatuses() {
    return this.firmManagerService.findProjectStatuses();
}

@ModelAttribute("objectives")
public Collection<ProjectObjective> populateProjectObjectives() {
    return this.firmManagerService.findProjectObjectives();
}

@RequestMapping(value = "/projects/{projectId}/edit", method = RequestMethod.GET)
public String initUpdateProjectForm(@PathVariable("projectId") long projectId, Model model) {
    Project project = this.firmManagerService.findProjectById(projectId);
    model.addAttribute("project", project);
    return "projects/createOrUpdateProject";
}

@RequestMapping(value = "/projects/{projectId}/edit", method = RequestMethod.POST)
public String processUpdateProjectForm(@ModelAttribute("project") Project project, ModelMap model) {
    model.put("project", project);
        return "projects/testEditProjectPage";
}
//...
}

JSP“createOrUpdateProject”

 <form:form modelAttribute="project" method="post" action="/projects/${project.id}/edit"
               class="form-horizontal">
        <input type="hidden" name="id" value="${project.id}"/>
           <label>Name</label>
              <form:input class="form-control" path="name"/>
           <label>Start Date</label>
              <form:input class="form-control" path="startDate"/>
           <label>End Date</label>
              <form:input class="form-control" path="endDate"/>
           <label>Description</label>
              <form:input class="form-control" path="description"/>
           <label>Notes</label>
              <form:input class="form-control" path="notes"/>


            <label>Project's Status</label>
            <form:select class="form-control" path="projectStatus" items="${statuses}" size="4"/>
           <label>Project's Objective</label>
            <form:select class="form-control" path="projectObjective" items="${objectives}" size="7"/>

        <div class="form-group">
            <button class="btn btn-default" type="submit">Update Project</button>
        </div>
    </form:form>

JSP“testEditProjectPage”

<table class="table table-striped">
<tr>
    <th>ID</th>
    <td><b><c:out value="${project.id}"/></b></td>
</tr>
<tr>
    <th>Name</th>
    <td><b><c:out value="${project.name}"/></b></td>
</tr>
<tr>
    <th>Status ID</th>
    <td><c:out value="${project.projectStatus.id}"/></td>
</tr>
<tr>
    <th>Status</th>
    <td><c:out value="${project.projectStatus.name}"/></td>
</tr>
<tr>
    <th>Objective ID</th>
    <td><c:out value="${project.projectObjective.id}"/></td>
</tr>
<tr>
    <th>Objective</th>
    <td><c:out value="${project.projectObjective.name}"/></td>
</tr>
<tr>
    <th>Description</th>
    <td><c:out value="${project.description}"/></td>
</tr>
<tr>
    <th>Notes</th>
    <td><c:out value="${project.notes}"/></td>
</tr>
<tr>
    <th>Start date</th>
    <td><fmt:formatDate value="${project.startDate}" pattern="yyyy-MM-dd"/></td>
</tr>
<tr>
    <th>End date</th>
    <td><fmt:formatDate value="${project.endDate}" pattern="yyyy-MM-dd"/></td>
</tr>

项目有projectStatus和projectObjective。问题是,如果我使用spring的“form:select”用“projectStatus”和“projectObjective”字段发布“project”,我会收到HTTP 400错误(客户端发送的请求在语法上是不正确的。)。

SCREEN

如果我从JSP表单中排除这两个选项,它会正确显示最近的POSTed项目,但当然还有NULL状态和目标字段。

SCREEN

请!!澄清我,如何正确使用这个“形式:选择”来发布数据。由于这个问题我不能“编辑”项目(通过使用entityManager.merge(project)),因为projectStatus和projectObjective的“id”和“name”字段是NULL:(

我使用Spring的PetClinic代码编写了这个项目。在PetClinic它工作得很好。我有相同的字段“projectStatus”和“projectObjective”就像PetClinic的“Pet”实体中的“type”一样 像那样:

<form:form modelAttribute="pet"
        <input type="hidden" name="id" value="${pet.id}"/>
            <form:input class="form-control" path="name"/>
            <form:input class="form-control" path="birthDate"/>
            <form:select class="form-control" path="type" items="${types}" size="5"/>
            <button class="btn btn-default" type="submit">Update Pet</button>
    </form:form>

0 个答案:

没有答案