简单MVC实现增删改查

反射工具类RelfectionUtils

 package Utils;

 import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; /**
* 反射的 Utils 函数集合
* 提供访问私有变量, 获取泛型类型 Class, 提取集合中元素属性等 Utils 函数
* @author Administrator
*
*/
public class ReflectionUtils { /**
* 通过反射, 获得定义 Class 时声明的父类的泛型参数的类型
* 如: public EmployeeDao extends BaseDao<Employee, String>
* @param clazz
* @param index
* @return
*/
@SuppressWarnings("unchecked")
public static Class getSuperClassGenricType(Class clazz, int index){
Type genType = clazz.getGenericSuperclass(); if(!(genType instanceof ParameterizedType)){
return Object.class;
} Type [] params = ((ParameterizedType)genType).getActualTypeArguments(); if(index >= params.length || index < 0){
return Object.class;
} if(!(params[index] instanceof Class)){
return Object.class;
} return (Class) params[index];
} /**
* 通过反射, 获得 Class 定义中声明的父类的泛型参数类型
* 如: public EmployeeDao extends BaseDao<Employee, String>
* @param <T>
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static<T> Class<T> getSuperGenericType(Class clazz){
return getSuperClassGenricType(clazz, 0);
} /**
* 循环向上转型, 获取对象的 DeclaredMethod
* @param object
* @param methodName
* @param parameterTypes
* @return
*/
public static Method getDeclaredMethod(Object object, String methodName, Class<?>[] parameterTypes){ for(Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()){
try {
//superClass.getMethod(methodName, parameterTypes);
return superClass.getDeclaredMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
//Method 不在当前类定义, 继续向上转型
}
//..
} return null;
} /**
* 使 filed 变为可访问
* @param field
*/
public static void makeAccessible(Field field){
if(!Modifier.isPublic(field.getModifiers())){
field.setAccessible(true);
}
} /**
* 循环向上转型, 获取对象的 DeclaredField
* @param object
* @param filedName
* @return
*/
public static Field getDeclaredField(Object object, String filedName){ for(Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()){
try {
return superClass.getDeclaredField(filedName);
} catch (NoSuchFieldException e) {
//Field 不在当前类定义, 继续向上转型
}
}
return null;
} /**
* 直接调用对象方法, 而忽略修饰符(private, protected)
* @param object
* @param methodName
* @param parameterTypes
* @param parameters
* @return
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public static Object invokeMethod(Object object, String methodName, Class<?> [] parameterTypes,
Object [] parameters) throws InvocationTargetException{ Method method = getDeclaredMethod(object, methodName, parameterTypes); if(method == null){
throw new IllegalArgumentException("Could not find method [" + methodName + "] on target [" + object + "]");
} method.setAccessible(true); try {
return method.invoke(object, parameters);
} catch(IllegalAccessException e) {
System.out.println("不可能抛出的异常");
} return null;
} /**
* 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
* @param object
* @param fieldName
* @param value
*/
public static void setFieldValue(Object object, String fieldName, Object value){
Field field = getDeclaredField(object, fieldName); if (field == null)
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]"); makeAccessible(field); try {
field.set(object, value);
} catch (IllegalAccessException e) {
System.out.println("不可能抛出的异常");
}
} /**
* 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
* @param object
* @param fieldName
* @return
*/
public static Object getFieldValue(Object object, String fieldName){
Field field = getDeclaredField(object, fieldName); if (field == null)
throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + object + "]"); makeAccessible(field); Object result = null; try {
result = field.get(object);
} catch (IllegalAccessException e) {
System.out.println("不可能抛出的异常");
} return result;
}
}

jdbcutils

 package Utils;

 import java.sql.Connection;
import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtil { public static void reaseConnection(Connection connection) {
try {
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static DataSource dataSource = null;
static{
dataSource = new ComboPooledDataSource("mvc");
} public static DataSource getDataSource(){
return dataSource;
} public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("连接失败");
}
return null; }
}

DAO操作类 依靠c3p0,dbutil

 package com.mvc.dao;

 import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.el.util.ReflectionUtil; import com.sun.org.apache.bcel.internal.generic.NEW; import Utils.JdbcUtil;
import Utils.ReflectionUtils; public class Dao<T> {
private Class<T> clazz;
private QueryRunner queryRunner= new QueryRunner(JdbcUtil.getDataSource());
public Dao() {
clazz = ReflectionUtils.getSuperGenericType(this.getClass());
}
public <E> E getForValue(String sql,Object ...arg){
try {
return (E) queryRunner.query(sql,new ScalarHandler(),arg);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("getForValue error");
}
return null; }
public List<T> getForList(String sql,Object ...args){
try {
return queryRunner.query(sql, new BeanListHandler<>(clazz),args);
} catch (SQLException e) {
System.out.println("getForList error");
}
return null; }
public T get(String sql,Object ...args){
try {
return queryRunner.query(sql,new BeanHandler<>(clazz),args);
} catch (SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("get error");
}
return null;
} public void updata(String sql,Object ...args){
try {
queryRunner.update(sql,args);
} catch (SQLException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("updata error");
}
}
}

Customer类

package com.mvc.domain;

public class Customer {
private String id;
private String name;
private String address;
private String phone;
private int count; public Customer() {
super();
}
public Customer(String id, String name, String address, String phone) {
super();
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
this.count = 0;
} public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", phone=" + phone + "]";
} }

CustomerDao接口

 package com.mvc.dao;

 import java.util.List;

 import com.mvc.domain.CriteriaCustomer;
import com.mvc.domain.Customer; public interface CustomerDao {
public List<Customer> getAll();
public void save(Customer customer);
public void upCount(Customer customer);
public void upAll(Customer customer);
public Customer getForIdName(String id,String name);
public Customer getForId(String id);
public void delete(String id);
public long getCountWithName(String name);
public long getCountWithId(String id);
public List<Customer> getCriteriaCustomers(CriteriaCustomer cc);
}

操作类

 package com.mvc.jdbc;

 import java.sql.SQLException;
import java.util.List; import com.mvc.dao.CustomerDao;
import com.mvc.dao.Dao;
import com.mvc.domain.CriteriaCustomer;
import com.mvc.domain.Customer; public class CustomerDaoJdbc extends Dao<Customer> implements CustomerDao{ @Override
public List<Customer> getAll(){
// TODO Auto-generated method stub
String sql = "SELECT * FROM customer";
return getForList(sql);
} @Override
public void save(Customer customer) {
// TODO Auto-generated method stub
String sql = "insert into customer(id,name,address,phone,count) value(?,?,?,?,?)";
updata(sql,customer.getId(),customer.getName(),customer.getAddress(),customer.getPhone(),customer.getCount()); } @Override
public void upAll(Customer customer) {
// TODO Auto-generated method stub
String sql = "UPDATE customer SET NAME=?,ADDRESS=?,PHONE=? WHERE ID=?";
updata(sql,customer.getName(),customer.getAddress(),customer.getPhone(),customer.getId()); } @Override
public void upCount(Customer customer) {
// TODO Auto-generated method stub
String sql = "UPDATE customer SET COUNT=? WHERE ID=?";
updata(sql,customer.getCount(),customer.getId());
} @Override
public Customer getForIdName(String id,String name) {
// TODO Auto-generated method stub
String sql = "SELECT id,name,address,phone,count FROM customer where id = ? and name = ?";
return get(sql,id,name);
}
@Override
public Customer getForId(String id) {
// TODO Auto-generated method stub
String sql = "SELECT id,name,address,phone,count FROM customer where id = ?";
return get(sql,id);
}
@Override
public void delete(String id) {
// TODO Auto-generated method stub
String sql = "delete from customer where ID = ? ";
updata(sql,id); } @Override
public long getCountWithName(String name) {
// TODO Auto-generated method stub
String sql = "select count(id) from customer where name = ?";
return (long)getForValue(sql, name);
} @Override
public List<Customer> getCriteriaCustomers(CriteriaCustomer cc) {
// TODO Auto-generated method stub
String sql = "SELECT * FROM customer WHERE NAME LIKE ? AND address LIKE ? AND phone LIKE ?";
System.out.println(cc.getName()+cc.getAddress()+cc.getPhone());
return getForList(sql,cc.getName(),cc.getAddress(),cc.getPhone());
} @Override
public long getCountWithId(String id) {
// TODO Auto-generated method stub
String sql = "select count(id) from customer where id= ?";
return (long)getForValue(sql, id);
} }

servlet

 package com.mvc.servlet;

 import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.mvc.dao.CustomerDao;
import com.mvc.domain.CriteriaCustomer;
import com.mvc.domain.Customer;
import com.mvc.jdbc.CustomerDaoJdbc;
import com.sun.corba.se.impl.protocol.giopmsgheaders.Message; import jdk.nashorn.internal.ir.RuntimeNode.Request; /**
* Servlet implementation class SqlServlet
*/
@WebServlet("/SqlServlet")
public class SqlServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
CustomerDao customerDao = null; public SqlServlet() {
super();
// TODO Auto-generated constructor stub
customerDao = new CustomerDaoJdbc();
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String servletPath = request.getServletPath();
// System.out.println(servletPath);
servletPath = servletPath.substring(1, servletPath.length() - 3);
try {
Method method = getClass().getDeclaredMethod(servletPath,
HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this, request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } @SuppressWarnings("unused")
private void edit(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("edit");
String id = request.getParameter("id");
System.out.println(id);
Customer customer = customerDao.getForId(id);
System.out.println(customer);
request.setAttribute("customer", customer);
request.getRequestDispatcher("/updata.jsp").forward(request, response);
} @SuppressWarnings("unused")
private void updata(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("updata");
String id = request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
Customer customer = new Customer(id, name, address, phone);
String massage;
massage = "修改成功";
customerDao.upAll(customer);
request.setAttribute("massage", massage);
request.setAttribute("customer", customer);
request.getRequestDispatcher("/updata.jsp").forward(request, response); } @SuppressWarnings("unused")
private void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("add");
String id = request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
Customer customer = new Customer(id, name, address, phone);
System.out.println(customer);
long count = customerDao.getCountWithName(name);
long count1 = customerDao.getCountWithId(id);
System.out.println("count:" + count);
request.setAttribute("id", id);
request.setAttribute("name", name);
request.setAttribute("address", address);
request.setAttribute("phone", phone);
if (count1 > 0) {
request.setAttribute("massage", "学号已存在,报名失败");
request.getRequestDispatcher("/newcustomer.jsp").forward(request,
response);
} else {
request.setAttribute("massage", "报名成功");
customerDao.save(customer);
request.getRequestDispatcher("/newcustomer.jsp").forward(request,
response);
}
} @SuppressWarnings("unused")
private void delete(HttpServletRequest request, HttpServletResponse response)
throws IOException {
System.out.println("delete");
String id = request.getParameter("id");
System.out.println(id);
//customerDao.delete(id); //response.sendRedirect("query.do");
} @SuppressWarnings("unused")
private void query(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
System.out.println(name + address + phone);
System.out.println("query");
CriteriaCustomer cc = new CriteriaCustomer(name, address, phone);
List<Customer> customers = customerDao.getCriteriaCustomers(cc);
request.setAttribute("customers", customers);
try {
request.getRequestDispatcher("/1.jsp").forward(request,
response);
} catch (ServletException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @SuppressWarnings("unused")
private void come(HttpServletRequest request, HttpServletResponse response) {
String name = request.getParameter("name");
String id = request.getParameter("id");
Customer customer = customerDao.getForIdName(id, name);
System.out.println(customer);
if (customer != null) {
request.setAttribute("massage", "");
customer.setCount(customer.getCount() + 1);
customerDao.upCount(customer);
//response.sendRedirect("NewFile_1.jsp");
try {
request.getRequestDispatcher("/NewFile_1.jsp").forward(request,
response);
} catch (ServletException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else {
try {
String massage = "名字或学号错误";
request.setAttribute("massage", massage);
request.setAttribute("id", id);
request.setAttribute("name", name);
request.getRequestDispatcher("/come.jsp").forward(request,
response);
} catch (ServletException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

以下jsp代码 css样式等为网上找的素材

1.增加用户的jsp

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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">
<!--[if IE]><meta http-equiv="x-ua-compatible" content="IE=9" /><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Your Description Here">
<meta name="keywords"
content="bootstrap themes, portfolio, responsive theme">
<meta name="author" content="ThemeForces.Com"> <!-- Favicons
================================================== -->
<!-- <link rel="shortcut-icon" href="img/favicon.ico" type="image/x-icon"> -->
<!-- <link rel="apple-touch-icon" href="img/apple-touch-icon.png"> -->
<link rel="apple-touch-icon" sizes="72x72"
href="img/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114"
href="img/apple-touch-icon-114x114.png"> <!-- Bootstrap -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<!-- <link rel="stylesheet" type="text/css"
href="fonts/font-awesome/css/font-awesome.css"> --> <!-- Stylesheet
================================================== -->
<link rel="stylesheet" type="text/css" href="css/style.css">
<!-- <link rel="stylesheet" type="text/css" href="css/responsive.css"> --> <!-- <script type="text/javascript" src="js/modernizr.custom.js"></script> --> <title></title>
</head>
<body>
<h3 style="text-align: center;"><font color="red"><c:out value="${massage}"></c:out></font></h3>
<div id="tf-contact">
<div class="container">
<div class="section-title">
<hr>
</div> <div class="space"></div> <div class="row">
<div class="col-md-6 col-md-offset-3">
<form id="contact" action="add.do" method="post">
<div class="form-group">
<input class="form-control" id="exampleInputEmail1" type="text"
name="id" value="${id}" placeholder="长学号" />
</div> <div class="form-group">
<input class="form-control" type="text" name="name"
value="${name}" placeholder="姓名" />
</div> <div class="form-group">
<input class="form-control" type="text" name="address"
value="${address}" placeholder="班级" />
</div> <td><div class="form-group">
<input class="form-control" type="text" name="phone"
value="${phone}" placeholder="手机" />
</div>
<input class="btn btn-primary my-btn dark" type="submit"
value="--报名--" />
</form>
</div>
</div>
</div>
</div>
<!-- <script type="text/javascript" src="js/jquery.1.11.1.js"></script>-->
<!-- Include all compiled plugins (below), or include individual files as needed -->
<!-- <script type="text/javascript" src="js/bootstrap.js"></script> --> <!-- Javascripts
================================================== -->
<!-- <script type="text/javascript" src="js/main.js"></script>-->
</body>
</html>

2.查询

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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>
<script type="text/javascript" src="scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
</script>
</head>
<body> <form action="query.do" method="post">
<table>
<tr>
<td>CustomerName:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone"/></td>
</tr>
<tr>
<td><input type="submit" value="Query"/></td>
<td><a href="newcustomer.jsp">Add New Customer</a></td>
</tr>
</table>
</form> <br><br> <c:if test="${!empty requestScope.customers }"> <hr>
<br><br> <table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>^ID</th>
<th>CustomerName</th>
<th>Address</th>
<th>Phone</th>
<th>UPDATE\DELETE</th>
</tr> <c:forEach items="${requestScope.customers }" var="cust"> <tr>
<td>${cust.id }</td>
<td>${cust.name }</td>
<td>${cust.address }</td>
<td>${cust.phone }</td>
<td>
<c:url value="/edit.do" var="editurl">
<c:param name="id" value="${cust.id }"></c:param>
</c:url>
<a href="${editurl }">UPDATE</a>
<c:url value="/delete.do" var="deleteurl">
<c:param name="id" value="${cust.id }"></c:param>
</c:url>
<a href="${deleteurl }" class="delete">DELETE</a>
</td>
</tr> </c:forEach> </table>
</c:if> </body>
</html>

3.删除

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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>
<script type="text/javascript" src="scripts/jquery-1.7.2.js"></script> </head>
<body> <form action="query.do" method="post">
<table>
<tr>
<td>CustomerName:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone"/></td>
</tr>
<tr>
<td><input type="submit" value="Query"/></td>
<td><a href="newcustomer.jsp">Add New Customer</a></td>
</tr>
</table>
</form> <br><br> <c:if test="${!empty requestScope.customers }"> <hr>
<br><br> <table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>^ID</th>
<th>CustomerName</th>
<th>Address</th>
<th>Phone</th>
<th>UPDATE\DELETE</th>
</tr> <c:forEach items="${requestScope.customers }" var="cust"> <tr>
<td>${cust.id }</td>
<td>${cust.name }</td>
<td>${cust.address }</td>
<td>${cust.phone }</td>
<td>
<c:url value="/edit.do" var="editurl">
<c:param name="id" value="${cust.id }"></c:param>
</c:url>
<a href="${editurl }">UPDATE</a>
<c:url value="/delete.do" var="deleteurl">
<c:param name="id" value="${cust.id }"></c:param>
</c:url>
<a href="${deleteurl }" class="delete">DELETE</a>
</td>
</tr> </c:forEach> </table>
</c:if> </body>
</html>

4.修改

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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">
<!--[if IE]><meta http-equiv="x-ua-compatible" content="IE=9" /><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Your Description Here">
<meta name="keywords"
content="bootstrap themes, portfolio, responsive theme">
<meta name="author" content="ThemeForces.Com"> <!-- Favicons
================================================== -->
<link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon">
<link rel="apple-touch-icon" href="img/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72"
href="img/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114"
href="img/apple-touch-icon-114x114.png"> <!-- Bootstrap -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css"
href="fonts/font-awesome/css/font-awesome.css"> <!-- Stylesheet
================================================== -->
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/responsive.css"> <script type="text/javascript" src="js/modernizr.custom.js"></script> <title></title>
</head>
<body> <c:if test="${requestScope.massage != null }">
<br>
<h3 style="text-align: center;"><font color="red"><c:out value="${massage}"></c:out></font></h3>
<br>
<br>
</c:if> <c:set var="id" value="${customer != null ? customer.id : param.id }"></c:set> <c:set var="name"
value="${customer != null ? customer.name : param.name }"></c:set>
<c:set var="address"
value="${customer != null ? customer.address : param.address }"></c:set>
<c:set var="phone"
value="${customer != null ? customer.phone : param.phone }"></c:set>
<div id="tf-contact">
<div class="container">
<div class="section-title"> <hr>
</div> <div class="space"></div> <div class="row">
<div class="col-md-6 col-md-offset-3">
<form id="contact" action="updata.do" method="post">
<input type="hidden" name="id" value="${id }"></input>
<div class="form-group">
<h4>姓名:</h4>
<input class="form-control" type="text" name="name"
value="${name}" placeholder="姓名" />
</div> <div class="form-group">
<h4>班级:</h4>
<input class="form-control" type="text" name="address"
value="${address}" placeholder="班级" />
</div> <div class="form-group">
<h4>手机:</h4>
<input class="form-control" type="text" name="phone"
value="${phone}" placeholder="手机" />
</div> <input class="btn btn-primary my-btn dark" type="submit"
value="修改" />
</form>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="js/jquery.1.11.1.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script type="text/javascript" src="js/bootstrap.js"></script> <!-- Javascripts
================================================== -->
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>MVC</display-name> <servlet>
<description></description>
<display-name>SqlServlet</display-name>
<servlet-name>SqlServlet</servlet-name>
<servlet-class>com.mvc.servlet.SqlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SqlServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> </web-app>

运行效果 增加信息

简单MVC实现增删改查

数据库内数据

简单MVC实现增删改查

上一篇:java图片缩放


下一篇:配置hibernate,Struts。文件