接上面代码,在com.javaee.ex12.po包中创建实体类Book,包含String类型的bname,pub分别对应教材名称,出版社。float类型的price对应价格。然后在com.javaee.ex12.po包创建实体类CourseEx,其中包含int类型的cno,period分别对应课程编号和学时数,String类型的cname,bname,pub分别对应课程名称,教材名称,出版社。下一步,在控制器类CourseController中,添加处理方法toCourseAddPojoNest和CourseAddPojoNest。在CourseAddPojoNest方法中,以POJO方式绑定数据。下一步,在“WEB-INF/pages/course”目录中,创建add_course_pojo_nest.jsp文件,将add_course_pojo.jsp文件中的内容复制过来,注意,<form/>元素的action属性值要与处理器方法courseAddPojoNest的映射路径一致,给出对应代码。下一步,在“WEB-INF/pages/course”目录中,创建course_info_pojo_nest.jsp文件,以EL方式显示请求域中的对象,给出对应代码。下一步,在index.jsp文件中,添加跳转至处理器方法toCourseAddPojoNest的链接,给出对应代码。 创建实体类com.javaee.ex12.po.Student,其中包含String类型的sno,sname对应学号,姓名。int类型的age对应年龄,以及List<String> courseList对应课程表。
时间: 2023-07-15 22:11:52 浏览: 178
Book.java代码:
```java
package com.javaee.ex12.po;
public class Book {
private String bname;
private String pub;
private float price;
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getPub() {
return pub;
}
public void setPub(String pub) {
this.pub = pub;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
```
CourseEx.java代码:
```java
package com.javaee.ex12.po;
public class CourseEx {
private int cno;
private String cname;
private int period;
private Book book;
public int getCno() {
return cno;
}
public void setCno(int cno) {
this.cno = cno;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
}
```
CourseController.java代码:
```java
package com.javaee.ex12.controller;
import com.javaee.ex12.po.CourseEx;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/course")
public class CourseController {
@RequestMapping(value = "/toCourseAddPojoNest", method = RequestMethod.GET)
public String toCourseAddPojoNest(Model model) {
model.addAttribute("courseEx", new CourseEx());
return "course/add_course_pojo_nest";
}
@RequestMapping(value = "/courseAddPojoNest", method = RequestMethod.POST)
public String courseAddPojoNest(@ModelAttribute("courseEx") CourseEx courseEx) {
return "redirect:/course/courseInfoPojoNest?cname=" + courseEx.getCname() + "&period=" + courseEx.getPeriod() + "&bname=" + courseEx.getBook().getBname() + "&pub=" + courseEx.getBook().getPub() + "&price=" + courseEx.getBook().getPrice();
}
@RequestMapping(value = "/courseInfoPojoNest", method = RequestMethod.GET)
public String courseInfoPojoNest() {
return "course/course_info_pojo_nest";
}
}
```
add_course_pojo_nest.jsp代码:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加课程</title>
</head>
<body>
<h3>添加课程</h3>
<form action="/course/courseAddPojoNest" method="post">
<table>
<tr>
<td>课程名称:</td>
<td><input type="text" name="cname"/></td>
</tr>
<tr>
<td>学时数:</td>
<td><input type="text" name="period"/></td>
</tr>
<tr>
<td>教材名称:</td>
<td><input type="text" name="book.bname"/></td>
</tr>
<tr>
<td>出版社:</td>
<td><input type="text" name="book.pub"/></td>
</tr>
<tr>
<td>价格:</td>
<td><input type="text" name="book.price"/></td>
</tr>
</table>
<input type="submit" value="添加"/>
</form>
</body>
</html>
```
course_info_pojo_nest.jsp代码:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>课程信息</title>
</head>
<body>
<h3>课程信息</h3>
<table>
<tr>
<td>课程名称:</td>
<td>${param.cname}</td>
</tr>
<tr>
<td>学时数:</td>
<td>${param.period}</td>
</tr>
<tr>
<td>教材名称:</td>
<td>${param.bname}</td>
</tr>
<tr>
<td>出版社:</td>
<td>${param.pub}</td>
</tr>
<tr>
<td>价格:</td>
<td>${param.price}</td>
</tr>
</table>
</body>
</html>
```
Student.java代码:
```java
package com.javaee.ex12.po;
import java.util.List;
public class Student {
private String sno;
private String sname;
private int age;
private List<String> courseList;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getCourseList() {
return courseList;
}
public void setCourseList(List<String> courseList) {
this.courseList = courseList;
}
}
```
阅读全文