效果图:
list.jsp:
<form action="<%=basePath %>List.action" id="mainForm" method="post">
<td>
<input name="command" type="text" class="allInput" value="${command}"/>
</td>
<td width="90" align="right">描述</td>
<td>
<input name="description" type="text" class="allInput" value="${description}"/>
</td>
<tbody>
<tr>
<th><input type="checkbox" id="all" οnclick="checkAll(this)"/></th>
<th>序号</th>
<th>指令名称</th>
<th>描述</th>
<th>操作</th>
</tr>
<c:forEach items="${messageList}" var="message" varStatus="status">
<tr <c:if test="${status.index%2 != 0}">style='background-color:#ECF6EE;'</c:if>>
<td><input type="checkbox" /></td>
<td>${status.index + 1}</td>
<td>${message.command}</td>
<td>${message.description}</td>
<td>
<a href="#">修改</a>
<a href="#">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
ListServlet.java:
req.setCharacterEncoding("UTF-8");
String command = req.getParameter("command");
String description = req.getParameter("description");
req.setAttribute("command", command);
req.setAttribute("description", description);
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/message","root","123456");
StringBuilder sql = new StringBuilder("select ID,COMMAND,DESCRIPTION,CONTENT from MESSAGE where 1=1");
List<String>paramList = new ArrayList<String>();
if(command != null && !"".equals(command.trim())){
sql.append(" and COMMAND=?");
paramList.add(command);
}
if(description != null && !"".equals(description.trim())){
sql.append(" and DESCRIPTION like '%' ? '%'");
paramList.add(description);
}
PreparedStatement statement = conn.prepareStatement(sql.toString());
for (int i=0; i<paramList.size(); i++) {
statement.setString(i+1, paramList.get(i));
}
ResultSet rs = statement.executeQuery();
List<Message>messageList = new ArrayList<Message>();
while(rs.next()){
Message message = new Message();
messageList.add(message);
message.setId(rs.getString("ID"));
message.setCommand(rs.getString("COMMAND"));
message.setDescription(rs.getString("DESCRIPTION"));
message.setContent(rs.getString("CONTENT"));
}
req.setAttribute("messageList", messageList);