2

我在使用构造从我的 JSP 通过 POST 向后端发送数据时遇到问题:

<form:radiobuttons path="pvClCategory" items="${categoryList}" itemLabel="clcaCategory"/>

提交后,我的 Jboss(版本 5)显示:

HTTP Status 400 - description: The request sent by the client was syntactically incorrect ().

我不知道如何查看已发送的请求。当我在下面使用构造时,一切正常,但我不想只绑定对象中的一个值,而是绑定整个对象:

<form:radiobuttons path="pvClCategory.clcaCategory" items="${categoryList}" itemValue="clcaCategory" itemLabel="clcaCategory"/>

所以,第一个表单构造(绑定整个对象)给我带来了一个问题。表单已正确初始化,这意味着单选选项已正确显示,并且在 jsp 站点上选择了正确的值。但是当我想提交表单时出现问题,我收到错误(客户端发送的 HTTP 状态 400 请求在语法上不正确)。知道为什么吗?我做错了什么?


我的代码:

我有一个实体类 Violation 字段名为 pvClCategory 类型为 ClCategory

@Entity
@Table(name="PV_VIOLATIONS")
public class Violation implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="VIOL_ID")
    private Long id;

    @ManyToOne
    @JoinColumn(name="VIOL_CLCA_ID")
    private ClCategory pvClCategory;

    /* others fields and getters/setters */
}

我的 ClCategory 实体类需要将其绑定到 Violation.pvClCategory 字段:

@Entity
@Table(name="PV_CL_CATEGORIES")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class ClCategory implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="CLCA_ID")
    private long clcaId;

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

    @OneToMany(mappedBy="pvClCategory")
    private List<Violation> pvViolations;

    public ClCategory() {
    }

    public long getClcaId() {
        return this.clcaId;
    }

    public void setClcaId(long clcaId) {
        this.clcaId = clcaId;
    }

    public String getClcaCategory() {
        return this.clcaCategory;
    }

    public void setClcaCategory(String clcaCategory) {
        this.clcaCategory = clcaCategory;
    }

    public List<Violation> getPvViolations() {
        return this.pvViolations;
    }

    public void setPvViolations(List<Violation> pvViolations) {
        this.pvViolations = pvViolations;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (clcaId ^ (clcaId >>> 32));
        return result;
    }

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

这个 Violation 类在我的表单中的 commandName 属性中使用如下(文件名:v.jsp):

<form:form action="../update" method="post" commandName="violation">
    <form:radiobuttons path="pvClCategory" items="${categoryList}" itemLabel="clcaCategory"/>
    <!-- other fields -->
    <button type="submit" value="Apply Changes" class="button-default" >Apply</button>
</form:form>

我的控制器:

@Controller
@RequestMapping("/viol")
public class ViolationController {

    @RequestMapping("edit/{violationId}")
    public String edit(@PathVariable("violationId") long id, Map<String, Object> map) {
        Violation violation = violationService.getViolation(id);

        map.put("violation",violation);
        map.put("categoryList", adminService.listCategories());

        return "v";
    } 
}

正如我在构造表单中所理解的那样:radiobuttons path="" items=""路径是数据绑定的属性,因此项目中交付列表中的整个对象应该绑定到它。我输入了我的类别的项目列表,这些类别是 ClCategory 类型的对象。提交后出现错误。

当我使用form:radiobuttons path="pvClCategory.clcaCategory" items="${categoryList}" itemValue="clcaCategory" itemLabel="clcaCategory"仅绑定来自项目中对象的字符串值(在这两种情况下都使用相同的对象列表ClCategory 类型),则表单已正确提交,但我不想只绑定 objecte 的一个值,而是绑定整个对象。你能帮我做错什么吗?

4

0 回答 0