【问题标题】:JSP dropdown does not submit objectJSP下拉不提交对象
【发布时间】:2012-08-01 09:09:15
【问题描述】:

我有一个用数据库中的数据填充的表单。 在开始描述我的问题之前,一些sn-ps:

一类:

// @Entity for areas
public class Area {
@Id
@Column(name = "area")
private String area;

@Column(name = "deleted")
private boolean deleted;

getter/setter
}

二等

// @Entity for employees
public class Employee {
@Id
@GeneratedValue
@Column(name = "ID")
private long id;

@ManyToOne
@JoinColumn(name = "area")
private Area area;

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

getter/setter

调用EmployeeController中的方法将数据返回给jsp

protected String createDialog( @PathVariable("id") Long id, Model model ){
    Employee employee = id == 0 ? new Employee() : employeeService.findById(id);
    //return employee
    model.addAttribute("employeeModel", employee );
 //add data needed to create dropdown holding areas
    //areaService.findAll returns a List<Area>
    model.addAttribute("areas", areaService.findAll( 
                new Sort( 
                    Sort.Direction.ASC,
                    "area"
                    )
                ));
    return "employees/dialogUpdateEdit";
}

jsp,显示区域的下拉列表,如果没有返回新员工,则显示已知数据

<form:form action="employees/ajax" commandName="employeeModel" method="POST" id="createForm">
    <table class="fullWidth">
        <tr>
            <td>Area</td>
            <td>
                <form:select path="area" items="${areas}" class="fullWidth">
                </form:select>
            </td>
        </tr>
        <tr>
            <td>Employee Name</td>
            <td><form:input path="name" class="fullWidth"/></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Save Changes" id="btnSaveEmployee" class="fullWidth" />
            </td>
        </tr>
    </table>

    <!-- adding hidden field to hold id on update -->
<form:hidden path="id" />   

</form:form>

控制器方法进行验证并返回一些错误或不返回

@RequestMapping(value = "/ajax", method = RequestMethod.POST)
protected @ResponseBody ValidationResponse createOrUpdate(
        @Validated @ModelAttribute("employeeModel") Employee employee,
        BindingResult bindingResult) {

    if (!bindingResult.hasErrors()) {
        employeeService.createOrUpdate(employee);
    }

    return validate(employee, null, bindingResult);
}

对于问题: 这一切都很好,下拉列表被填充,数据被填充到输入中。 但是当我点击提交时,我收到以下错误:

java.lang.IllegalStateException:无法转换类型的值 [java.lang.String] 到所需类型 [com.whatever.Area] 的属性 “区域”:未找到匹配的编辑器或转换策略

据我了解,表单只是为“区域”提交纯字符串,而不是绑定列表中的对象。

如何让表单提交对象而不是字符串?我的绑定有问题吗?

感谢您的帮助!

【问题讨论】:

  • 你能发布你的控制器方法映射 url "employees/ajax" 吗?

标签: java spring jsp


【解决方案1】:

回答我自己的问题,并希望这个人在不知不觉中帮助我的人获得很多学分;)

我必须添加一个自定义活页夹......以前从未听说过,所以我先问了很长一段路。

这个链接有我找到的关于该主题的最好和最短的教程,因为我知道我在寻找什么: http://empire5.com/development/binding-a-custom-object-in-spring-3/

【讨论】:

    【解决方案2】:

    我认为你可以试试这个类型代码

    <select name="area" id="area" >      
      <option value="${areas}">${areas}</option>
    </select>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-30
      • 1970-01-01
      相关资源
      最近更新 更多