}
导出的JSP界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script> <script type="text/javascript"> $(function(){ $("#queryByName").click(function(){ var name=$(this).prev().val(); location.href="${pageContext.request.contextPath}/employee/queryByName?name="+name; }); $("#import").click(function(){ var array=$("input[type=checkbox]:checked"); if(array.length==0){ alert("请选择您要导出的数据..."); }else{ var ids=new Array(); array.each(function(){ ids.push($(this).parent().next().text()); }); $.post("${pageContext.request.contextPath}/employee/queryByIds","ids="+ids,function(d){ if(d=="ok"){ alert("数据导入成功!"); }else{ alert("数据导入失败!"); } location.reload(); },"json"); } }); }); </script> </head> <body> <div> 姓名:<input type="text" name="name"/><input type="button" value="查询" id="queryByName"/> <input type="button" value="导出" id="import"/> </div> <table border="1" width="80%"> <tr> <th>选择</th> <th>序号</th> <th>姓名</th> <th>职位</th> <th>薪资</th> <th>部门</th> </tr> <c:forEach items="${requestScope.list }" var="e"> <tr> <td><input type="checkbox"></td> <td>${e.id }</td> <td>${e.name }</td> <td>${e.position }</td> <td>${e.salary }</td> <td>${e.department.name }</td> </tr> </c:forEach> </table> </body> </html>
查询的SQL语句
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.baway.dao.EmployeeMapper"> <select id="selectByName" parameterType="string" resultMap="rm"> select e.*,d.id d_id,d.name d_name from employee e,department d where e.did=d.id and e.name like '%${value}%' </select> <resultMap type="employee" id="rm"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="salary" property="salary"/> <result column="position" property="position"/> <association property="department" javaType="department"> <id column="d_id" property="id"/> <result column="d_name" property="name"/> </association> </resultMap> <select id="selectAll" resultMap="rm"> select e.*,d.id d_id,d.name d_name from employee e,department d where e.did=d.id </select> <select id="selectByIds" parameterType="int" resultMap="rm"> select e.*,d.id d_id,d.name d_name from employee e,department d where e.did=d.id <if test="ids!=null and ids.length!=0"> <foreach collection="ids" item="id" close=")" open="and e.id in(" separator=","> #{id} </foreach> </if> </select> </mapper>
修改的JSP
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <script type="text/javascript" src="My97DatePickerBeta/My97DatePicker/WdatePicker.js"></script> </head> <body > <form action="CupAction!update" method="post"> <table border="1" bgcolor="palegreen"> <tr> <td>编号:</td> <td><input type="text" readonly="readonly" value="${cup.id }" name="cup.id" /></td> </tr> <tr> <td>名字:</td> <td><input type="text" value="${cup.name }" name="cup.name" /></td> </tr> <tr> <td>颜色:</td> <td><input type="text" value="${cup.color }" name="cup.color" /></td> </tr> <tr> <td>大小:</td> <td> <input type="radio" name="cup.size" <c:if test="${cup.size=='0' }"> checked="checked" </c:if> value="0" > <input type="radio" name="cup.size" <c:if test="${cup.size==1 }"> checked="checked" </c:if> value="1" > <input type="radio" name="cup.size" <c:if test="${cup.size=='2' }"> checked="checked" </c:if> value="2"> </td> </tr> <tr> <td>日期:</td> <td> <input class="Wdate" name="cup.createtime" value="${cup.createtime }" type="text" id="temp" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm'})"/> </td> </tr> <tr> <td>请选择:</td> <td><select name="cup.Cuptype.cid" id="selectid"> <option value="">---请选择身份---</option> <c:forEach items="${list }" var="cuptype"> <option value="${cuptype.cid }"<c:if test="${ cuptype.cid==cup.cuptype.cid}" >selected="selected"</c:if>>${cuptype.cname }</option> </c:forEach> </select></td> </tr> <tr><td><input type="submit" value="修改" ></td></tr> </table> </form> </body> </html>
dao层方法和实现类
public interface UserDao { public List<User> getUserList(); public List<Dept> getDeptList(); public User getById(User user); public void delete(User user); public void register(User user); public void login(User user); public void update(User user); }
import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.bawei.wang.bean.Dept; import com.bawei.wang.bean.User; public class UserDaoImpl extends HibernateDaoSupport implements UserDao{ @Override public List<User> getUserList() { // TODO Auto-generated method stub return this.getHibernateTemplate().find("from User"); } @Override public List<Dept> getDeptList() { // TODO Auto-generated method stub return this.getHibernateTemplate().find("from Dept"); } @Override public void delete(User user) { this.getHibernateTemplate().delete(user); } @Override public void register(User user) { this.getHibernateTemplate().save(user); } @Override public void login(User user) { // TODO Auto-generated method stub } @Override public void update(User user) { this.getHibernateTemplate().update(user); } @Override public User getById(User user) { // TODO Auto-generated method stub return this.getHibernateTemplate().get(User.class,user.getId()); } }
Action控制类
import java.util.List; import com.bawei.wang.bean.User; import com.bawei.wang.service.UserService; public class UserAction { private List list; private UserService service; private User user; public List getList() { return list; } public void setList(List list) { this.list = list; } public UserService getService() { return service; } public void setService(UserService service) { this.service = service; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getUserList(){ System.out.println("进入查询的方法"); list=service.getUserList(); return "select"; } public String getDeptList(){ System.out.println("进入部门的方法"); list=service.getDeptList(); return "insert"; } public String update(){ service.update(user); user.setUsername(""); return getDeptList(); } public String getById(){ System.out.println("进入查询ID的方法"); user=service.getById(user); list=service.getDeptList(); return "update"; } public String register(){ System.out.println("进入添加方法"); service.register(user); return getUserList(); } public String delete(){ System.out.println("进入删除方法"); service.delete(user); System.out.println(user); return getUserList(); } }
查询界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'select.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <table border="1" bgcolor="palegreen"> <tr> <td>id</td> <td>用户姓名</td> <td>密码</td> <td>性别</td> <td>年龄</td> <td>地址</td> <td>部门名称</td> <td>添加</td> </tr> <c:forEach items="${list }" var="user"> <tr> <td>${user.id }</td> <td>${user.username }</td> <td>${user.password }</td> <td> ${user.sex }</td> <td>${user.age }</td> <td>${user.address }</td> <td>${user.dept.name }</td> <td><a href="UserAction!getDeptList.action">添加</a> /<a href="UserAction!delete.action?user.id=${user.id }">删除</a></td> </tr> </c:forEach> </table> <br> </body> </html>
