Spring和Hibernate表单绑定

时间:2014-03-26 20:18:15

标签: spring hibernate jsp

好的,我遇到了问题,已经被困了几个小时。我正在使用Hibernate运行当前版本的spring并且需要从表单中获取数据并将其保存到数据库中,这就是它给我带来了很多麻烦并且不知道如何去做。下面是控制器,JSP,Model和Dao。

控制器

private final String addNewView = "addAward";

@RequestMapping(value = "/addAwardType", method = RequestMethod.GET) 
public String addAwardType() {                                
 LOG.debug("Display form to add a new contact.");        
 return addNewView;                                         
}                      

@RequestMapping(value = "/addAwardType", method = RequestMethod.POST)          
public ModelAndView addAwardType(                                       
  @ModelAttribute("AwardTypeModel") AwardType awardType,                
  BindingResult result) {                                           
 return new ModelAndView(addNewView, "AwardType", new AwardTypeModel(awardType));    
} 



}

JSP

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Add AwardType</h2>
<form:form method="POST" commandName="addAwardType">

    <table>
    <tr>
        <td><form:label path="AwardType.name">Award Name</form:label></td>
        <td><form:input path="AwardType.name" /></td> 
    </tr>
    <tr>
        <td><form:label path="AwardType.description">Last Name</form:label></td>
        <td><form:input path="AwardType.description" /></td>
    </tr>
    <tr>
        <td> <form:checkbox path="AwardType.isActive" value="Active"/></td>
    </tr>
    <tr>
        <td><form:label path="AwardType.created"></form:label></td>
        <td><form:input path="AwardType.created" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Add Award"/>
        </td>
    </tr>
</table>  

</form:form>
</body>
</html>

模型

public class AwardTypeModel extends BaseModel {

private int id;
private String name;
private String description;
private boolean active;
private Date created;
private Date modified;

/**
 * Construct from persistence object
 * A similar constructor will be needed in each model
 * @param dbo - the Persistence Object (data base object)
 */
public AwardTypeModel(AwardType dbo){
    this.id = dbo.getAwardTypeId();
    this.name = dbo.getAwardName();
    this.description = dbo.getDescription();    
    this.active = dbo.isActive();
    this.created = dbo.getCreated();
    this.modified = dbo.getModified();

}



/* (non-Javadoc)
 * @see com.eaglecrk.recognition.model.BaseModel#convertToDb()
 */
@Override
public BasePersistence convertToDb() {
    AwardType dbo = new AwardType();
    dbo.setAwardTypeId(this.id);
    dbo.setAwardName(this.name);
    dbo.setDescription(this.description);
    dbo.setActive(this.active);
    dbo.setCreated(this.created);
    dbo.setModified(this.modified);     
    return dbo;
}

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public boolean isActive() {
    return active;
}
public void setActive(boolean active) {
    this.active = active;
}
public Date getCreated() {
    return created;
}
public void setCreated(Date created) {
    this.created = created;
}
public Date getModified() {
    return modified;
}
public void setModified(Date modified) {
    this.modified = modified;
}
}

0 个答案:

没有答案