使用spring MVC在JSP表单中呈现一对多关系时遇到问题。贝娄我给3个档案。这里是学生与商标之间的一对多关系。所以在添加和更新学生详细信息时如何在JSP中表示Mark set。实际上我在jsp页面写的是没有用的。另一件事我希望控制器中的单个对象在表单提交。
Student.hbm.xml
<hibernate-mapping>
<class name="net.startMVC.pojo.Student" table="student" catalog="spring_mvc_start" >
<id name="studid" type="int" >
<column name="studid" />
<generator class="increment" />
</id>
<many-to-one name="address" class="net.startMVC.pojo.Address" lazy="false" fetch="select">
<column name="addressid" />
</many-to-one>
<property name="name" type="string">
<column name="name" length="100" />
</property>
<property name="roleNo" type="java.lang.Integer">
<column name="role_no" />
</property>
<property name="stream" type="string">
<column name="stream" length="50" />
</property>
<list name="marks" table="mark" inverse="true" lazy="false" fetch="select">
<key>
<column name="studid" />
</key>
<list-index base="1" column="markid"/>
<one-to-many class="net.startMVC.pojo.Mark" />
</list>
</class>
Mark.hbm.xml
<hibernate-mapping>
<class name="net.startMVC.pojo.Mark" table="mark" catalog="spring_mvc_start">
<id name="markid" type="int">
<column name="markid" />
<generator class="increment" />
</id>
<many-to-one name="student" class="net.startMVC.pojo.Student" fetch="select">
<column name="studid" />
</many-to-one>
<property name="totalMark" type="java.lang.Integer">
<column name="total_mark" />
</property>
</class>
添加控制器方法
@RequestMapping("/addStudent")
public ModelAndView addStudent(Model model)
{
System.out.println("comming controller");
return new ModelAndView("addStudent", "command", new Student());
}
编辑控制器方法
@RequestMapping(value="/editStudent", method=RequestMethod.GET)
public ModelAndView editStudent(@RequestParam String studID,Model model)
{
System.out.println("studID->"+studID);
setStudent(helloMVCService.getOneStudentDetails(studID));
return new ModelAndView("addStudent", "command", getStudent());
}
保存并更新控制器方法
@RequestMapping(value = "/addNewStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute Student student,Model model) {
helloMVCService.saveStudentData(student);
return "redirect:/hello";
}
addStudend.jsp
<form:form method="POST" action="addNewStudent">
<form:hidden path="studid"/>
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="roleNo">Roll No</form:label></td>
<td><form:input path="roleNo" /></td>
</tr>
<tr>
<td><form:label path="stream">Stream</form:label></td>
<td><form:input path="stream" /></td>
</tr>
<tr>
<td><form:label path="address.loc">Location</form:label></td>
<td><form:input path="address.loc" /></td>
</tr>
<tr>
<td><form:label path="address.state">State</form:label></td>
<td><form:input path="address.state" /></td>
</tr>
<tr>
<td><form:label path="address.country">Country</form:label></td>
<td><form:input path="address.country" /></td>
</tr>
<c:forEach var="marks" varStatus="vs" items="${marks}">
<tr>
<td><form:label path="marks[vs.index].totalMark">Mark</form:label></td>
<td><form:input path="marks[vs.index].totalMark" /></td>
</tr>
</c:forEach>
<tr>
<td colspan="2">
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>