然后在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对应课程表。下一步,创建控制器类com.javaee.ex12.controller.StudentController, 并在其中添加两个方法,分别用于页面跳转和添加学生信息。
时间: 2023-07-15 08:10:46 浏览: 143
以下是代码示例:
CourseEx.java
```
package com.javaee.ex12.po;
public class CourseEx {
private int cno;
private String cname;
private String bname;
private String pub;
private int period;
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 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 int getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
}
```
CourseController.java
```
package com.javaee.ex12.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.javaee.ex12.po.CourseEx;
@Controller
@RequestMapping("/course")
public class CourseController {
@RequestMapping("/toCourseAddPojoNest")
public String toCourseAddPojoNest() {
return "course/add_course_pojo_nest";
}
@RequestMapping("/courseAddPojoNest")
public String courseAddPojoNest(HttpServletRequest request, Model model, CourseEx course) {
// 处理添加课程信息的逻辑
model.addAttribute("course", course);
return "course/course_info_pojo_nest";
}
}
```
add_course_pojo_nest.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加课程信息</title>
</head>
<body>
<h2>添加课程信息</h2>
<form action="${pageContext.request.contextPath}/course/courseAddPojoNest" method="post">
<p>编号:<input type="text" name="cno" /></p>
<p>名称:<input type="text" name="cname" /></p>
<p>教材名称:<input type="text" name="bname" /></p>
<p>出版社:<input type="text" name="pub" /></p>
<p>学时数:<input type="text" name="period" /></p>
<p><input type="submit" value="添加" /></p>
</form>
</body>
</html>
```
course_info_pojo_nest.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>课程信息</title>
</head>
<body>
<h2>课程信息</h2>
<p>编号:${course.cno}</p>
<p>名称:${course.cname}</p>
<p>教材名称:${course.bname}</p>
<p>出版社:${course.pub}</p>
<p>学时数:${course.period}</p>
</body>
</html>
```
Student.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;
}
}
```
StudentController.java
```
package com.javaee.ex12.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.javaee.ex12.po.Student;
@Controller
@RequestMapping("/student")
public class StudentController {
@RequestMapping("/toAddStudent")
public String toAddStudent() {
return "student/add_student";
}
@RequestMapping("/addStudent")
public String addStudent(HttpServletRequest request, Model model, Student student) {
// 处理添加学生信息的逻辑
model.addAttribute("student", student);
return "student/student_info";
}
}
```
以上代码仅作为参考,具体实现根据实际需求进行调整。
阅读全文