序言:
通过自定义MVC(二)我们了解了如何运用自定义mvc完成增删改查,但这只是简洁版的,不适合我们的生活实际运用,下面让我们通过一个案例来把我们之前所学的知识进行一个总结。
案例:
我们以一张图来理解我们要写的案例
如图所示:
我们在数据库里创建我们需要的三张表,然后我们来看看具体是怎样实现的
我们是在自定义MVC(二)中所写的增删改查的方法基础上去实现的
StudentDao.jsva
代码如下:
package com.dengrenli.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.dengrenli.entity.Banji;
import com.dengrenli.entity.Hobby;
import com.dengrenli.entity.Student;
import com.dengrenli.entity.Teacher;
import com.dengrenli.util.BaseDao;
import com.dengrenli.util.DBAccess;
import com.dengrenli.util.PageBean;
import com.dengrenli.util.StringUtils;
public class StudentDao extends BaseDao<Student> {
/**
* 用户查询方法(带分页的)
* @param book
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Student> list(Student stu, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql = "select * from student where true ";
String name=stu.getName();
String tname=stu.getTeacher();
String banji=stu.getBanji();
String hobby=stu.getHobby();
int id = stu.getId();
if(StringUtils.isNotBlank(name)) {
sql += " and name like '%"+name+"%'";
}
// if(StringUtils.isNotBlank(tname)) {
// sql += " and teacher like '%"+tname+"%'";
// }
// if(StringUtils.isNotBlank(banji)) {
// sql += " and banji like '%"+banji+"%'";
// }
// if(StringUtils.isNotBlank(hobby)) {
// sql += " and hobby like '%"+hobby+"%'";
// }
if(id != 0) {
sql += " and id ="+id;
}
return super.executeQuery(sql, Student.class, pageBean);
}
/**
* 拿到教师表里面的所有数据
* @return
*/
public List<Teacher> listtea() {
Connection con=null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Teacher> list=new ArrayList<>();
try {
con=DBAccess.getConnection();
ps=con.prepareStatement("select * from teacher");
rs=ps.executeQuery();
while(rs.next()) {
Teacher tea=new Teacher();
tea.setTid(rs.getInt("tid"));
tea.setName(rs.getString("name"));
list.add(tea);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 拿到爱好表里面的所有数据
* @return
*/
public List<Hobby> listhobby() {
Connection con=null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Hobby> list=new ArrayList<>();
try {
con=DBAccess.getConnection();
ps=con.prepareStatement("select * from hobby");
rs=ps.executeQuery();
while(rs.next()) {
Hobby tea=new Hobby();
tea.setId(rs.getInt("id"));
tea.setName(rs.getString("name"));
list.add(tea);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 拿到所有班级表的数据
* @return
*/
public List<Banji> listbanji() {
Connection con=null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Banji> list=new ArrayList<>();
try {
con=DBAccess.getConnection();
ps=con.prepareStatement("select * from banji");
rs=ps.executeQuery();
while(rs.next()) {
Banji bj=new Banji();
bj.setCid(rs.getInt("cid"));
bj.setName(rs.getString("name"));
list.add(bj);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 修改方法
* @param book
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int edit (Student stu) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql = "update student set name=?,teacher=?,banji=?,hobby=? where id=?";
return super.executeUpdate(sql, new String[] {"name","teacher","banji","hobby","id"}, stu);
}
/**
* 新增
* @param book
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int add (Student stu) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql = "insert into student values(?,?,?,?,?)";
return super.executeUpdate(sql, new String[] {"id","name","teacher","banji","hobby"}, stu);
}
/**
* 删除
* @param book
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int del (Student stu) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql = "delete from student where id=?";
return super.executeUpdate(sql, new String[] {"id"}, stu);
}
}
DispatcherServlet.java
代码如下:
package com.dengrenli.framework;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
/**
* 中央控制器
* 作用:接收请求,通过请求寻找处理请求对应的自控器
* @author Administrator
*
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 5483874334171819754L;
// private Map<String, Action> actionMap = new HashMap<>();
//
private ConfigModel configModel;
public void init() {
// actionMap.put("/addCal", new AddCalAction());
// actionMap.put("/delCal", new DelCalAction());
try {
String xmlPath = this.getInitParameter("xmlPath");
if(xmlPath == null || "".equals(xmlPath)) {
configModel = ConfigModelFactory.newInstance();
}else {
configModel = ConfigModelFactory.newInstance(xmlPath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
init();
String url = req.getRequestURI();
// /web_mvc_addCal.action
// /addCal
url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
ActionModel actionModel = configModel.get(url);
if(actionModel == null) {
throw new RuntimeException("你没有配置action标签,找不到对应的自控制器来处理浏览器发送出来的请求 !!!!");
}
try {
// Action action = (Action) Class.forName("com.caoguangli.web.AddCalAction").newInstance();
// Action action = (Action) AddCalAction();
Action action = (Action) Class.forName(actionModel.getType()).newInstance();
// action就是com.dengrenli.web.CalAction
if(action instanceof ModelDrivern) {
ModelDrivern modelDrivern = (ModelDrivern) action;
// 此时的model所有属性值是null
Object model = modelDrivern.getModel();
BeanUtils.populate(model, req.getParameterMap());
// 可以将req.getParameterMap()的值通过反射的方式将其塞进model实例
// BeanUtils.populate方法的原理
// Map<String, String[]> paMap = req.getParameterMap();
// Set<Entry<String, String[]>> entrySet = paMap.entrySet();
// Class<? extends Object> cls = model.getClass();
// for (Entry<String, String[]> entry : entrySet) {
// Field field = cls.getDeclaredField(entry.getKey());
// field.setAccessible(true);
// field.set(model, entry.getValue());
// }
}
String code = action.execute(req, resp);
ForwardModel forwardModel = actionModel.get(code);
if(forwardModel != null) {
String jspPath = forwardModel.getPath();
if("false".equals(forwardModel.getRedirect())) {
// 做转发的处理
req.getRequestDispatcher(jspPath).forward(req, resp);
}else {
resp.sendRedirect(req.getContextPath()+jspPath);
}
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// Action action = actionMap.get(url);
// try {
action.execute(req, resp);
// } catch (Exception e) {
// e.printStackTrace();
// }
}
}
复选框自定义标签的助手类
checkboxTag.java
代码如下:
package com.dengrenli.tag;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.BeanUtils;
public class checkboxTag extends BodyTagSupport {
/**
*
*/
private static final long serialVersionUID = 4441713257512925582L;
private List<Object> items=new ArrayList<>();
private String name;
private String textKey;
private String textVal;
private String CheckboxVal;
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public String getCheckboxVal() {
return CheckboxVal;
}
public void setCheckboxVal(String checkboxVal) {
CheckboxVal = checkboxVal;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int doStartTag() {
JspWriter out = pageContext.getOut();
try {
out.write(toHtml());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SKIP_BODY;
}
private String toHtml() throws IllegalArgumentException, IllegalAccessException, InstantiationException, NoSuchFieldException, SecurityException, InvocationTargetException, NoSuchMethodException {
StringBuffer sb = new StringBuffer();
String val;
String html;
for (Object obj : items) {
val = BeanUtils.getProperty(obj, textKey);
html = BeanUtils.getProperty(obj, textVal);
if(CheckboxVal !=null) {
String[] split = CheckboxVal.split(",");
for (String s : split) {
if(s.equals(val)) {
sb.append("<input checked=\"checked\" type='checkbox' value ='"+val+"' name='"+name+"' >"+html+" ");
}else {
sb.append("<input type='checkbox' value ='"+val+"' name='"+name+"' >"+html+" ");
}
}
}else {
sb.append("<input type='checkbox' value ='"+val+"' name='"+name+"' >"+html+" ");
}
}
return sb.toString();
}
}
下拉框自定义标签的助手类
SelectTag.java
代码如下:
package com.dengrenli.tag;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.BeanUtils;
/**
* * <select id='' name=''>
<option value=''>===请选择===</option>
</select>
查询维度:下拉列表
修改页面:下拉列表 数据回显
最终效果:<z:select>...</z:select>
1、id、name
2、数据源items、存入数据库中的值textKey(value)、展示列textVal(option中的内容)
3、加入属性(默认头部属性值headTextKey,默认的展示列值headTextVal)
4、加入属性,能够实现数据回显的功能selectedVal
* @author machenike
*
*/
public class SelectTag extends BodyTagSupport {
private static final long serialVersionUID = 7670015428082811040L;
private String id;
private String name;
private List<Object> items = new ArrayList<>();
private String textKey;
private String textVal;
private String headTextKey;
private String headTextVal;
private String selectedVal;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public String getHeadTextKey() {
return headTextKey;
}
public void setHeadTextKey(String headTextKey) {
this.headTextKey = headTextKey;
}
public String getHeadTextVal() {
return headTextVal;
}
public void setHeadTextVal(String headTextVal) {
this.headTextVal = headTextVal;
}
public String getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.write(toHTML());
} catch (IOException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
return super.doStartTag();
}
/**
* 拼接出下拉列表所对应的select的html代码
* <select id='' name=''>
<option value=''>===请选择===</option>
</select>
* @return
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws NoSuchMethodException
* @throws InvocationTargetException
*/
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuilder sb = new StringBuilder();
sb.append("<select id='"+id+"' name='"+name+"'>");
if(!(headTextKey == null || "".equals(headTextKey) || headTextVal == null || "".equals(headTextVal) )) {
sb.append("<option value='"+headTextKey+"' selected>"+headTextVal+"</option>");
}
String val;
String html;
for (Object obj : items) {
// 要让学生id存入数据,让学生的名字展示再jsp页面
// <option value='s001'>zs</option>
// 有两种方式(通过反射拿值)
// 第一种
/*Field textKeyField = obj.getClass().getDeclaredField(textKey);
textKeyField.setAccessible(true);
val = (String) textKeyField.get(obj);
Field textValField = obj.getClass().getDeclaredField(textVal);
textValField.setAccessible(true);
html = (String) textValField.get(obj);*/
// 第二种(需要导入两个架包才能实现)
val = BeanUtils.getProperty(obj, textKey);
html = BeanUtils.getProperty(obj, textVal);
if(val.equals(selectedVal)) {
sb.append("<option value='"+val+"' selected>"+html+"</option>");
}else {
sb.append("<option value='"+val+"'>"+html+"</option>");
}
}
sb.append("</select>");
return sb.toString();
}
}
通用分页自定义标签的助手类
PageTag.java
代码如下:
package com.dengrenli.tag;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.dengrenli.util.PageBean;
public class PageTag extends BodyTagSupport {
private static final long serialVersionUID = -3609493782238830873L;
private PageBean pageBean;
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() {
StringBuilder sb = new StringBuilder();
// 拼接下一次发送请求所要提交的隐藏的form表单
sb.append("<form id='pageBeanForm' action='"+pageBean.getUrl()+"' method='post'>");
sb.append("<input type='hidden' name='page'>");
Map<String, String[]> paMap = pageBean.getPaMap();
if(paMap != null && paMap.size()>0) {
Set<Entry<String, String[]>> entrySet = paMap.entrySet();
for (Entry<String, String[]> entry : entrySet) {
// 上一次请求可能携带页码name=page的参数,但是改参数在前面已经单独赋值
// 为什么要单独赋值呢?因为上一次请求是第一页的数据,下一次可能是第二页,
// 以为这前后请求page对应的值是不一样的,需要单独赋值
if(!"page".equals(entry.getKey())) {
// <input type='hidden' name='hobby' value='1'>
// <input type='hidden' name='hobby' value='2'>
// [1,2]
for(String val:entry.getValue()) {
sb.append("<input type='hidden' name='"+entry.getKey()+"' value='"+val+"' > ");
}
}
}
}
sb.append("</form>");
// 拼接分页条
sb.append("<div style='text-align: right; font-size: 12px;'>");
sb.append("每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页 <a ");
sb.append(" href='javascript:gotoPage(1)'>首页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getPreviousPage()+")'>上一页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getNextPage()+")'>下一页</a> <a");
sb.append(" href='javascript:gotoPage("+pageBean.getMaxPage()+")'>尾页</a> <input type='text'");
sb.append(" id='skipPage'");
sb.append(" style='text-align: center; font-size: 12px; width: 50px;'> <a");
sb.append(" href='javascript:skipPage()'>Go</a>");
sb.append("</div>");
// 拼接分页所需要的js代码
sb.append("<script type='text/javascript'>");
sb.append(" function gotoPage(page) {");
sb.append(" document.getElementById('pageBeanForm').page.value = page;");
sb.append(" document.getElementById('pageBeanForm').submit();");
sb.append(" }");
sb.append(" function skipPage() {");
sb.append(" var page = document.getElementById('skipPage').value;");
sb.append(" if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>"+pageBean.getMaxPage()+"){");
sb.append(" alert('请输入1~N的数字');");
sb.append(" return;");
sb.append(" }");
sb.append(" gotoPage(page);");
sb.append(" }");
sb.append("</script>");
return sb.toString();
}
}
标签的配置.tld文件
z.tld
代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>deng 1.1 core library</description>
<display-name>deng core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/deng</uri>
<tag>
<!-- 标签库中的标签(类似c:set c:out的定义) -->
<name>page</name>
<!-- 是标签运行具体代码,也就是助手类,下面填写的是助手类的全路径名 -->
<tag-class>com.dengrenli.tag.PageTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<!-- 该标签的属性 -->
<name>pageBean</name>
<!-- 该属性是否必填 -->
<required>true</required>
<!-- 是否支持表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>select</name>
<tag-class>com.dengrenli.tag.SelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headTextKey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headTextVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>selectedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>check</name>
<tag-class>com.dengrenli.tag.checkboxTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>CheckboxVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
mvc.xml
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
config标签:可以包含0~N个action标签
-->
<config>
<action path="/student" type="com.dengrenli.web.StudentAction">
<forward name="list" path="/studentList.jsp" redirect="false" />
<forward name="edit" path="/studentEdit.jsp" redirect="false" />
<!-- 增删改一定要用重定向 -->
<forward name="toList" path="/student.action?methodName=list"></forward>
</action>
</config>
接下来我们来看看我们的前端界面
studentList.jsp
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/deng" prefix="z" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>小说目录</h2>
<br>
<c:if test="${empty listtea}">
<jsp:forward page="${pageContext.request.contextPath}/student.action?methodName=list"></jsp:forward>
</c:if>
<c:if test="${empty listbanji}">
<jsp:forward page="${pageContext.request.contextPath}/student.action?methodName=list"></jsp:forward>
</c:if>
<c:if test="${empty listhobby}">
<jsp:forward page="${pageContext.request.contextPath}/student.action?methodName=list"></jsp:forward>
</c:if>
<form action="${pageContext.request.contextPath}/student.action?methodName=list" method="post">
教员<z:select name="teacher" textVal="name" items="${listtea}" textKey="tid" headTextKey="0" headTextVal="全部老师" ></z:select>
班级<z:select name="banji" textVal="name" items="${listbanji}" textKey="cid" headTextKey="0" headTextVal="全部班级"></z:select>
爱好:<z:check items="${listhobby}" name="hobby" textKey="id" textVal="name" ></z:check>
<br/>
<input type="text" name="name">
<input type="submit" value="查询" >
</form>
<a href="${pageContext.request.contextPath}/student.action?methodName=preSave">增加</a>
<table border="1" width="100%">
<tr>
<td>学生id</td>
<td>学生姓名</td>
<td>学生教员</td>
<td>学生所在班级</td>
<td>学生爱好</td>
<td>操作</td>
</tr>
<c:if test="${empty stuList}">
<jsp:forward page="${pageContext.request.contextPath}/student.action?methodName=list"></jsp:forward>
</c:if>
<c:forEach items="${stuList }" var="s">
<tr>
<td>${s.id }</td>
<td>${s.name }</td>
<td>${s.teacher }</td>
<td>${s.banji }</td>
<td>${s.hobby }</td>
<td>
<a href="${pageContext.request.contextPath}/student.action?methodName=preSave&id=${s.id}">修改</a>
<a href="${pageContext.request.contextPath}/student.action?methodName=del&id=${s.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<z:page pageBean="${pageBean}"></z:page>
</body>
</html>
studentEdit.jsp
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/deng" prefix="z" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/student.action" method="post">
<input type="hidden" name="methodName" value="${stu.name == null ? 'add' : 'edit'}">
学生ID:<input type="text" name="id" value="${stu.id }"><br>
学生姓名:<input type="text" name="name" value="${stu.name }"><br>
学生的老师:<z:select name="teacher" textVal="name" items="${listtea}" selectedVal="${stu.teacher}" textKey="tid" headTextKey="0" headTextVal="全部老师" ></z:select>
学生所在班级:<z:select name="banji" textVal="name" items="${listbanji}" selectedVal="${stu.banji}" textKey="cid" headTextKey="0" headTextVal="全部老师" ></z:select>
学生爱好:<z:check items="${listhobby}" name="hobby" textKey="id" textVal="name" CheckboxVal="${stu.hobby}"></z:check>
<input type="submit" value="提交">
</form>
</body>
</html>
效果如图所示:
谢谢大家,多多指教!