web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
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-app_2_4.xsd">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/bean_config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
bean.config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--使用jdbcTemplate查询数据-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/spring1</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>001052</value>
</property>
</bean>
<bean id="daosupport" class="com.StudentDao">
<property name="jtl">
<ref bean="jdbcTemplate"/>
</property>
</bean>
<!-- 视图控制器-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- 多动作控制器-->
<bean id="paraMethodResolver"
class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/queryuser.do">QueryUser</prop>
<prop key="/deleteuser.do">DeleteUser</prop>
</props>
</property>
</bean>
<bean name="/*user.do" class="com.StudentMutilAction">
<property name="methodNameResolver">
<ref bean="paraMethodResolver"/>
</property>
<property name="dao">
<ref local="daosupport"/>
</property>
</bean>
</beans>
StudentBean.java:
package com;
import java.sql.Date;
import java.util.HashSet;
import java.util.Set;
/**
* DocuStuInfo generated by MyEclipse - Hibernate Tools
*/
public class StudentBean implements java.io.Serializable {
// Fields
private String stuId;
private String name;
private String sex;
private Integer age;
private String sfzhm;
private Date csrq;
private String zzmm;
private String jtdh;
private String jtdz;
private String jkzk;
private Set courseStu = new HashSet();
// Constructors
/** default constructor */
// public DocuStuInfo() {
// }
//
// /** minimal constructor */
// public DocuStuInfo(String stuId) {
// this.stuId = stuId;
// }
//
// /** full constructor */
// public DocuStuInfo(String stuId, String name, String sex, Integer age, String sfzhm, Date csrq, String zzmm, String jtdh, String jtdz, String jkzk) {
// this.stuId = stuId;
// this.name = name;
// this.sex = sex;
// this.age = age;
// this.sfzhm = sfzhm;
// this.csrq = csrq;
// this.zzmm = zzmm;
// this.jtdh = jtdh;
// this.jtdz = jtdz;
// this.jkzk = jkzk;
// }
// Property accessors
public String getStuId() {
return this.stuId;
}
public void setStuId(String stuId) {
this.stuId = stuId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSfzhm() {
return this.sfzhm;
}
public void setSfzhm(String sfzhm) {
this.sfzhm = sfzhm;
}
public Date getCsrq() {
return this.csrq;
}
public void setCsrq(Date csrq) {
this.csrq = csrq;
}
public String getZzmm() {
return this.zzmm;
}
public void setZzmm(String zzmm) {
this.zzmm = zzmm;
}
public String getJtdh() {
return this.jtdh;
}
public void setJtdh(String jtdh) {
this.jtdh = jtdh;
}
public String getJtdz() {
return this.jtdz;
}
public void setJtdz(String jtdz) {
this.jtdz = jtdz;
}
public String getJkzk() {
return this.jkzk;
}
public void setJkzk(String jkzk) {
this.jkzk = jkzk;
}
public Set getCourseStu() {
return courseStu;
}
public void setCourseStu(Set courseStu) {
this.courseStu = courseStu;
}
}
StudentDao.java:
package com;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
public class StudentDao {
private JdbcTemplate jtl = null;
public JdbcTemplate getJtl() {
return jtl;
}
public void setJtl(JdbcTemplate jtl) {
this.jtl = jtl;
}
public void executeSql(String deleteSql){
jtl.execute(deleteSql);
}
public List querySql(String selectsql){
return jtl.queryForList(selectsql);
}
}
StudentMutilAction.java:
package com;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class StudentMutilAction extends MultiActionController{
private StudentDao dao;
public StudentDao getDao() {
return dao;
}
public void setDao(StudentDao dao) {
this.dao = dao;
}
public ModelAndView QueryUser(HttpServletRequest request, HttpServletResponse res) {
String propName = request.getParameter("select1");
String conName = request.getParameter("select2");
if (conName.equals("DY")) conName = ">";
if (conName.equals("XY")) conName = "<";
if (conName.equals("DEY")) conName = "=";
String strValue = request.getParameter("strvalue");
String sqlSelect = null,fromStr = null,whereStr = null;
sqlSelect = "select * from tb_stuinfo where " + propName + " " + conName + "'" + strValue + "'";;
System.out.println("sssss" + sqlSelect);
List liststu = dao.querySql(sqlSelect);
System.out.println("liststu:" + liststu);
Map map = new HashMap();
map.put("stulist", liststu);
return new ModelAndView("index",map);
}
public ModelAndView DeleteUser(HttpServletRequest request, HttpServletResponse response) {
String stuid = request.getParameter("id");
System.out.println("stuid:" + stuid);
String sqlstate = null;
sqlstate = "delete from tb_stuinfo where stu_id ='" + stuid + "'";
dao.executeSql(sqlstate);
sqlstate = "select * from tb_stuinfo";
List liststu = dao.querySql(sqlstate);
Map map = new HashMap();
map.put("stulist", liststu);
return new ModelAndView("index",map);
}
}
index.jsp:
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<style type="text/css">
<!--
body {
background-color: #F5B4E0;
}
.style4 {color: #000033}
-->
</style>
<title>利用Spring中的多方法控制器实现数据查询和删除操作</title><body>
<form action="queryuser.do" method="post" name="form1" class="style4">
<div align="center">
<tr>
<td width="154">查询字段:
<select name="select1">
<option value="csrq">出生日期</option>
<option value="sfzhm">身份证号</option>
</select>
</td>
<td width="154">运算符:
<select name="select2">
<option value="DY">大于</option>
<option value="XY">小于</option>
<option value="DEY">等于</option>
</select>
</td>
<td width="190"><input type="text" width="185" name="strvalue" value="" /></td>
<td width="113"><input type="submit" name="query" value="查询" ></td>
</tr>
</div>
</form>
<table width="800" border="1" align="center" cellpadding="0" cellspacing="0" >
<tr>
<td class="style4" height="28"><div align="center">学生姓名</div></td>
<td class="style4"><div align="center">学生性别</div></td>
<td class="style4"><div align="center">身份证号</div></td>
<td class="style4"><div align="center">出生日期 </div></td>
<td class="style4"><div align="center">家庭电话</div></td>
<td class="style4"><div align="center">家庭地址</div></td>
<td class="style4"><div align="center">用户操作</div></td>
</tr>
<c:forEach var="stuinf" items="${stulist}">
<tr>
<td height="28" align="center" class="style4">
<div align="center">
<c:out value="${stuinf.name}" />
</div>
</td>
<td height="28" align="center" class="style4">
<div align="center">
<c:out value="${stuinf.sex}"/>
</div>
</td>
<td height="28" align="center" class="style4">
<div align="center">
<c:out value="${stuinf.sfzhm}"/>
</div>
</td>
<td height="28" align="center" class="style4">
<div align="center">
<fmt:formatDate value="${stuinf.csrq}" dateStyle="long"/>
</div>
</td>
<td height="28" align="center" class="style4">
<div align="center">
<c:out value="${stuinf.jtdh}" />
</div>
</td>
<td height="28" align="center" class="style4">
<div align="center">
<c:out value="${stuinf.jtdz}" />
</div>
</td>
<td height="28" align="center" class="style4">
<div align="center">
<input type="button" value="删除" οnclick="window.location.href('deleteuser.do?id=${stuinf.stu_id}')" />
</div>
</td>
</tr>
</c:forEach>
</table>
</center>
</body>
</html>