编写系统主页面(index.jsp),以及其对应的LookTeacherAction,该业务控制器主要用于查询所有教师信息,然后跳转到lookTeacher.jsp页面显示查询结果。
时间: 2023-06-25 20:03:17 浏览: 136
以下是主页面index.jsp的代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>教师信息管理系统</title>
</head>
<body>
<h1>教师信息管理系统</h1>
<form action="lookTeacher.action" method="post">
<input type="submit" value="查看所有教师信息">
</form>
</body>
</html>
```
上述代码中,我们定义了一个表单,当用户点击“查看所有教师信息”按钮时,表单会向lookTeacher.action发送post请求。
接下来是LookTeacherAction的代码:
```java
package com.example.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.dao.TeacherDao;
import com.example.model.Teacher;
public class LookTeacherAction {
private TeacherDao teacherDao;
public void setTeacherDao(TeacherDao teacherDao) {
this.teacherDao = teacherDao;
}
public String execute(HttpServletRequest request, HttpServletResponse response) {
List<Teacher> teacherList = teacherDao.getAllTeachers();
request.setAttribute("teacherList", teacherList);
return "success";
}
}
```
在上述代码中,我们注入了一个TeacherDao实例,并且在execute方法中调用了它的getAllTeachers方法获取所有教师信息。然后,我们将查询结果存储在request的attribute中,并返回字符串“success”。
最后,我们需要在struts.xml中配置LookTeacherAction的映射:
```xml
<action name="lookTeacher" class="com.example.action.LookTeacherAction">
<result name="success">/lookTeacher.jsp</result>
</action>
```
上述配置表示,当请求进入lookTeacher.action时,会调用LookTeacherAction的execute方法,然后根据返回结果跳转到/lookTeacher.jsp页面。在lookTeacher.jsp页面中,我们可以通过EL表达式获取request中存储的查询结果,并将其展示出来。
阅读全文