第一次实现最基本的MVC架构--登录注册查询删除基本操作

第一部分(model层):底层实现--Dao(基于DbUtils实现) CustomerDao接口 JdbcUtils(C3P0实现) 和实体类Customer(setter和getter方法),以及继承Dao类和CustomerDao接口的实现类

Dao

package com.itcast.mvc.dao;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
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 com.itcast.mvc.jdbc.utils.JdbcUtils;

//该泛型是当前DAO绑定的实体类的类型,封装了基本的CRUD方法以供子类继承使用,整个DAO采取DBUtils解决方案。
public class DAO<T> {
	//获取T所对应的class对象,这里需要用到反射的机制
	private Class<T> clazz;
	
	//创建一个QueryRunner对象,实现dbutils的工具类使用
	private QueryRunner qr=new QueryRunner(); 
	
	//在构造器中实现泛型反射
	public DAO(){
		//获取父类带泛型的类型
		Type type=getClass().getGenericSuperclass();
		
		//若带参则强转成ParameterizedType类型
		if(type instanceof ParameterizedType){
			ParameterizedType pt=(ParameterizedType) type;
			
			//获取真正泛型参数,调用getActualTypeArguments()方法获取真正的参数
			Type[] typeArgs=pt.getActualTypeArguments();
			
			//判断是否含有参数,若含有则继续判断
			if(typeArgs!=null&&typeArgs.length>0){
				
				//若typeArgs[0]是Class对象,则将其赋给clazz(强转成Class)
				if(typeArgs[0] instanceof Class){
					clazz=(Class)typeArgs[0];
				}
			}
		}
	}
	
	
	//返回某一字段的值
	public <E> E getForValue(String sql,Object...agrs){
		Connection con=null;
		try {
			//通过JdbcUtils工具类直接调用getConnection()方法获取一个连接对象
			con=JdbcUtils.getConnection();
			
			//使用DButils中的querryRunner
			//使用ScalarHandler返回某个特定的值
			return (E) qr.query(con, sql, new ScalarHandler(), agrs);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//最后关闭连接,同样通过JdbcUtils工具类直接调用releaseConnection方法释放连接
			JdbcUtils.releaseConnection(con);
		}
		return null;
	} 
	
	//返回多个查询对象,第一个参数为传进来的sql语句,第二个参数为占位符
	public List<T> getForlist(String sql,Object...agrs){
		Connection con=null;
		try {
			//通过JdbcUtils工具类直接调用getConnection()方法获取一个连接对象
			con=JdbcUtils.getConnection();
			
			//使用DButils中的querryRunner中的query方法
			//new BeanListHandler<>(泛型对象)返回多个对象,agrs是占位符
			return qr.query(con, sql, new BeanListHandler<>(clazz),agrs);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//最后关闭连接,同样通过JdbcUtils工具类直接调用releaseConnection方法释放连接
			JdbcUtils.releaseConnection(con);
		}
		return null;
	}
	
	
	//该方法实现查一个对象,返回对应T的实体类的对象
	public T get(String sql,Object...agrs){
		Connection con=null;
		try {
			//通过JdbcUtils工具类直接调用getConnection()方法获取一个连接对象
			con=JdbcUtils.getConnection();
			
			//使用DButils中的querryRunner中的query方法中的new BeanHandler来放回一个对象
			//参数con,sql,相应的new Handler(),站位符agrs
			return qr.query(con, sql, new BeanHandler<>(clazz), agrs);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//最后关闭连接,同样通过JdbcUtils工具类直接调用releaseConnection方法释放连接
			JdbcUtils.releaseConnection(con);
		}
		return null;
	}
	
	
	//该方法封装了insert,delete,update等基本操作,sql代表sql语句,agrs代表sql语句中的占位符,不需要返回值,只需完成操作
	public void update(String sql,Object...agrs){
		Connection con=null;
		try {
			//通过JdbcUtils工具类直接调用getConnection()方法获取一个连接对象
			con=JdbcUtils.getConnection();
			
			//使用DButils中的querryRunner中的update方法方便insert,delete和update操作
			qr.update(con, sql, agrs);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//最后关闭连接,同样通过JdbcUtils工具类直接调用releaseConnection方法释放连接
			JdbcUtils.releaseConnection(con);
		}
	}
}
Customer接口和Customer实体类

package com.itcast.mvc.dao;

import java.util.List;

import com.itcast.mvc.domian.Customer;

//该接口的方法取决与业务
public interface CustomerDao {
	public List<Customer> getFuzzySearch(FuzzySearch fs);
	public List<Customer> getAll();
	public void save(Customer customer);
	public Customer getID(Integer ID);
	public void delete(Integer ID);
	public long getCount(String name);
	public void update(Customer customer);
}

package com.itcast.mvc.domian;
//实体类
public class Customer {
	private Integer ID;
	private String name;
	private String address;
	public Integer getID() {
		return ID;
	}
	public void setID(Integer iD) {
		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 Customer() {
	}
	public Customer(String name, String address) {
		super();
		this.name = name;
		this.address = address;
	}
	
}

继承Dao和Customer接口的实现类

package com.itcast.mvc.dao.implement;

import java.util.List;

import com.itcast.mvc.dao.CustomerDao;
import com.itcast.mvc.dao.DAO;
import com.itcast.mvc.dao.FuzzySearch;
import com.itcast.mvc.domian.Customer;
//定义一个类,用来实现CoustomerDao接口和DAO类,即可以了解一个类专门实现Customer接口中的方法,接口中相应的方法能又实现DAO类中的相应的方法
public class CustomerDaoImp extends DAO<Customer> implements CustomerDao{
	
	//在CustomerDaoImp实现类先实现接口中的方法
	//模糊查询方法,两点创建新类获取相应的属性,sql模糊查询实现,这里参数一定是模糊查询的类的对象
	public List<Customer> getFuzzySearch(FuzzySearch fs){
		//1.先写入sql语句
		String sql="select * from studentlist where name like ? and address like ? ";
		//2.调用Dao中的方法,将sql语句和参数传进去
		return getForlist(sql,fs.getName(),fs.getAddress());
	}
	
	@Override
	//在CustomerDaoImp实现类先实现接口中的方法
	public List<Customer> getAll() {
		//1.先写入sql语句
		String sql="select * from studentlist";
		//2.调用Dao中的方法,将sql语句和参数传进去
		//此处传进去一个sql语句就好了,不需要站位符
		return getForlist(sql);
		
	}

	@Override
	//在CustomerDaoImp实现类先实现接口中的方法
	public void save(Customer customer) {
		//1.先写入sql语句
		String sql="insert into studentlist (name,address) values (?,?)";
		//2.调用Dao中的方法,将sql语句和参数传进去
		update(sql,customer.getName(),customer.getAddress());
	}

	@Override
	//在CustomerDaoImp实现类先实现接口中的方法
	public Customer getID(Integer ID) {
		//1.先写入sql语句
		String sql="select * from studentlist where id=?";	
		//2.调用Dao中的方法,将sql语句和参数传进去
		return get(sql,ID);
	}

	@Override
	//在CustomerDaoImp实现类先实现接口中的方法
	public void delete(Integer ID) {
		//1.先写入sql语句
		String sql="delete from studentlist where id=? ";
		//2.调用Dao中的方法,将sql语句和参数传进去
		update(sql,ID);
		
	}

	@Override
	//在CustomerDaoImp实现类先实现接口中的方法
	public long getCount(String name) {
		//1.先写入sql语句
		String sql="select count(id) from studentlist where name=?";
		//2.调用Dao中的方法,将sql语句和参数传进去
		return getForValue(sql,name);
	}

	@Override
	public void update(Customer customer) {
		//修改sql即update 表名 set 字段名=? where 条件=?,这里删除和编辑都是通过id的条件来实现的
		String sql="update studentlist set name=?, address=? where id=?";
		//只要是update的语句(创建,添加,编辑,删除即都不需要返回值的操作)都调用DAO中的update方法
		update(sql,customer.getID(),customer.getName(),customer.getAddress());
	}

	

}
//底层实现
//底层与数据库打交道的是Dao,该类中方法即CRUD最基本的数据库方法(创建数据库连接,执行sql语句,关闭连接),实现是DBUtils中QerryRunnner和JdbcUtils(c3p0和释放方法)
//然后创建接口实体类的Dao,接口中的方法即为具体的CRUD
//创建实体类,即属性和get,set方法
//创建实现Dao类和CustomerDao接口的实现类,实现接口中的方法,返回调用Dao中的方法
JdbcUtils实现和C3P0环境配置

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
  <!-- This app is massive! -->
  <named-config name="mvc"> 
    <property name="user">root</property>
  	<property name="password">123</property>
  	<property name="driverClass">com.mysql.jdbc.Driver</property>
  	<property name="jdbcUrl">jdbc:mysql:///list</property>

  	

  </named-config>
</c3p0-config>

package com.itcast.mvc.jdbc.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
//Jdbc工具类,使用C3P0实现连接池建立
public class JdbcUtils {
	private static DataSource datasource=null;
	
	//加载驱动放到静态代码处,目的是执行一次节约资源
	static{
		//new一个ComboPooledDataSource类的对象("c3p0配置文件的文件名")转给DataSource的引用
		datasource=new ComboPooledDataSource("mvc");
	}
	//释放Connection,将该方法定义为static可以直接使用类名访问
	public static void releaseConnection(Connection con){
		if(con!=null){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	//获取一个Connection对象,将该方法定义为static可以直接使用类名访问
	static public Connection getConnection() throws SQLException{
		return datasource.getConnection();
	}
}

第二部分(Controler)是Servlet实现类和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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MVC1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Servlet</display-name>
    <servlet-name>Servlet</servlet-name>
    <servlet-class>com.itacast.servlet.Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

package com.itacast.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itcast.mvc.dao.CustomerDao;
import com.itcast.mvc.dao.FuzzySearch;
import com.itcast.mvc.dao.implement.CustomerDaoImp;
import com.itcast.mvc.domian.Customer;


/**
 * Servlet implementation class Servlet
 */
public class Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet() {
        super();
    }
    //用具体实现类对象创建CustomerDao对象,实现多态
    private CustomerDao cd=new CustomerDaoImp();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//此处调用doPost方法,且传进去的参数与doGet方法,达到一样
		doPost(request,response);
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	//超链接一般都写在doPost中
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取servletpath即超链接的地址
		String servletpath=request.getServletPath();
		//将获取的连接路径仅留下方法名
		String methodname=servletpath.substring(1,servletpath.length()-3);
		//System.out.println(methodname);

			try {
				//利用反射原理获取相应的方法getClass方法获取该类,此处省略了对象名就是this
				//getDeclaredMethod(方法名,参数列表的类方法  即类型.class)方法获取相应的声明方法
				Method method=getClass().getDeclaredMethod(methodname,HttpServletRequest.class,HttpServletResponse.class);
				//调用invoke方法执行方法,invoke方法中第一个参数为类的实例对象,后面参数为相应函数中的参数
				method.invoke(this, request, response);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		catch (NoSuchMethodException e) {
			//当没有该方法时,给予响应,即给出响应jsp
			response.sendRedirect("error.jsp");
		} catch (SecurityException e) {
			e.printStackTrace();
		}
	}
	//实现servlet和jsp之间的调用三步:1.获取方法的返回值,然后调用setAttribute方法传进去,最后调用getRuequestDispatcher方法实现转发或者重定向,
	//该处为转发。
	private void query(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		//1.先调用getParameter方法收集请求参数
		String name=request.getParameter("Name");
		String address=request.getParameter("Address");
		
		//创建FuzzySearch类的对象,并将获取的请求参数对FuzzySearch类对象进行初始化
		FuzzySearch fs=new FuzzySearch(name,address);
		
		//再调用getFuzzySearch方法执行模糊查询并将返回结果返回到customers对象的list中
		List<Customer> customers=cd.getFuzzySearch(fs);
		//1.获取Customer集合
		//List<Customer> customers=cd.getAll();
		
		//2.将customer集合放入request中,调用setAttribute("属性名",属性),这里即为属性名(页面上getAttribute获取的属性名),customer为传进去的属性
		request.setAttribute("customers",customers );
		
		//3.转发页面到index.jsp,先调用getRequestDispathcer("转发的页面")方法,然后调用forward(request,response)方法
		//不能使用重定向
		request.getRequestDispatcher("/index.jsp").forward(request, response);	
	}
	private void edit(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		//先将forward即响应页面为error.jsp
		String forward="/error.jsp";
		//1.编辑先获取ID
		String idstr =request.getParameter("ID");
		try {
			//2.通过id获取相应的customer对象赋给customer对象
			Customer customer=cd.getID(Integer.parseInt(idstr));
			if(customer!=null){
			//3.将获取的customer放到request中,然后响应update.jsp
			request.setAttribute("customer",customer);
			//若不为空,即获取到customer,然后响应到update.jsp页面进行编译
			forward="/update.jsp";
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		//4.响应update.jsp
		request.getRequestDispatcher(forward).forward(request, response);
	}
	private void update(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		String name=request.getParameter("Name");
		String address=request.getParameter("Address");
		String id=request.getParameter("ID");
		String oldname=request.getParameter("oldname");
		
		//若oldname与name不相同即需要判断更改的名字是否已经占用,若相同则不需要判断
		if(!oldname.equalsIgnoreCase(name)){
			if(cd.getCount(name)>0){
				request.setAttribute("message", "该用户名"+name+"已经被占用,请重新填写!");
				request.getRequestDispatcher("/upate.jsp").forward(request, response);
				return;
			}
		}
		Customer customer=new Customer(name,address);
		//这里需要把id设置进去
		customer.setID(Integer.parseInt(id));
		//如果没有被占用就调用相应的update方法进行update
		cd.update(customer);
		response.sendRedirect("query.do");
	}
	
	//Servlet调用的方法都是CustomerDao中实现接口Customer中的方法,Customer接口方法通过Dao来实现
	private void add(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		String name=request.getParameter("Name");
		String address=request.getParameter("Address");
		//因为如果返回大于0,表示该名字已经存在不能添加了
		if(cd.getCount(name)>0){
			request.setAttribute("message", "用户名"+name+"已被占用,请重新填写!");
			//若被占用就请求转发到add.jsp重新添加
			request.getRequestDispatcher("/add.jsp").forward(request, response);
			return;
		}
		//若没有存在则正常调用save方法进行添加保存到数据库中
		cd.save(new Customer(name,address));
		//然后在重定向到添加成功的jsp
		response.sendRedirect("success.jsp");
	}
	private void delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		//获取请求参数getPartementer("")
		String idstr=request.getParameter("ID");
		int id=0;
		
		try{
			id=Integer.parseInt(idstr);
			//Servlet调用的CRUD都是调用Customer接口中的方法
			//cd是Customer接口对象指向CustomerDaoImp实现类对象
			//将获取的请求参数传到delete方法中
			cd.delete(id);
			//此处使用重定向为了保持在index.jsp当前页面,这里参数是query.do,即Servlet的URL映射*.do,此处为query.do
			response.sendRedirect("query.do");
		}catch(Exception e){
			//当没有该方法时,response对象给予响应,即给出响应jsp,sendRedirect重定向
			e.printStackTrace();
			//此处使用重定向为了保持在index.jsp当前页面
			response.sendRedirect("query.do");
		}
	}
}

最后就是View层的jsp实现
index.jsp,add.jsp,update.jsp,error.jsp和success.jsp实现

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%> 
<%@ page import="com.itcast.mvc.domian.Customer;" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
<!-- javascript代码一般写在tile后 ,第一行导包-->
<script type="text/javascript" src="scripts/jquery-1.7.2.js"></script>
<!-- 下面写js代码 -->
<script type="text/javascript">
	$(function(){
		$(".delete").click(function(){
			//content获取标签里面的名字
			//this是a,parent是td,parent是tr,id是第一列,名字是第二个即1(从0开始)
			var content=$(this).parent.parent.find("td:eq(1)").text;
			//要删除的名字,即content
			var flag=confirm("确定要删除"+content+"用户吗?");
			//返回ture正常执行,返回false取消执行
			return flag;
		})
	})
</script>
</head>
<body>
<!-- 表结构标签from,action=“映射的servlet”,method=“请求方式” -->
	<form action="query.do" method="post">
	<!-- 使用table产生一张表 -->
		<table>
		<!-- tr行 -->
		<tr>
		<!-- td列 -->
			<td>Name :</td>
			<!-- input添加文本框或者按钮,type=“属性类型” name=“属性名字” -->
			<td><input type="text" name="Name"/></td>
		</tr>
		<tr>
			<td>ID :</td>
			<td><input type="text" name="ID"/></td>
		</tr>
		<tr>
			<td>Address :</td>
			<td><input type="text" name="Address"/></td>
		</tr>
		<tr>
		<!-- 提交按钮和超链接 -->
			<td><input type="submit" value="Query"/></td>
			<!-- 这里超链接href="超链接的jsp(即给出一个超链接的响应页面jsp),<a>中间为显示超链接的名字</a>" -->
			<td><a href="add.jsp">Add new Students</a></td>
		</tr>
	</table>
	</form>
	<br><br>
	
	<!-- 定义一个list对象接收getAttribute("setAttribute方法中的属性名")返回对象 -->
	<%
		List<Customer> customers=(List<Customer>)request.getAttribute("customers");
		if(customers!=null && customers.size()>0){
	%>
		<!-- 加条水平线 -->
		<hr>
		<br>
		<!-- border边框宽度,cellpadding用于隔开单元格与单元格间距,cellspacing单元格补白之间的距离-->
			<table border="1" cellpadding="10" cellspacing="0">
			<!-- 该处为第一行,即头 -->
					<tr>
					<!-- <td>表示内容单元格 <th>则表示标题-->
						<th>ID</td>
						<th>Name</td>
						<th>Address</td>
						<th>Update</th>
						<th>Delete</th>
					</tr>			
				
				<!-- 将表格多少行放如到for循环中,因为取决于有多少个用户数 ,每一循环创建一行,和相应的列数构成表-->
				<%
					for(Customer customer:customers){
				
				%>
				<!-- 一行tr ,里面包含着列td,th一般用于标题-->
					<tr>
					<!-- 调用相应实体类即Customer类中的具体方法,获取相应的值,显示在jsp中 -->
						<td><%=customer.getID()%></td>
						<td><%=customer.getName()%></td>
						<td><%=customer.getAddress()%></td>
						<!-- 该处放两个超链接,即Update和Delete ,href=""空时就没有超链接页面-->
						<td><a href="edit.do?ID=<%=customer.getID()%>" class="update">Update</a></td>
						<!-- 此处超链接后面给请求参数即?参数 -->
						<!-- 凡是像删除或者修改都是具体对象时都采用?传参数的形式来确定具体对象 -->
						<td><a href="delete.do?ID=<%=customer.getID()%>" class="delete">Delete</a></td>
					</tr>
				<%		
					}
				%>
			</table>
	
	
	<%		
		}
	%>	


</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.itcast.mvc.domian.Customer;" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<!-- setAttribute的属性通过getAttribute方法来获取 -->
	<%
		Object msg=request.getAttribute("message");
		if(msg!=null){
	%>
		<!-- 如果是java代码(即取变量中的值)一定要用百分号表达式,否则就显示文本信息! -->
		<font color="red"><%=msg%></font>
	<%		
		}
	%>
	
	<!-- 编写add的响应jsp,核心是action=""(将web.xml的url地址设为.do)然后通过反射从/add.do获取add即方法名,
	再找到Servlet中相应的方法,执行Customer接口中的方法,实现与数据库间的操作 -->
	<form action="add.do" method="post">
		<table>
			<tr>
				<td>Name :</td>
				<td><input type="text" name="Name" 
				value="<%=request.getParameter("Name")==null?"":request.getParameter("Name")%>"/></td>
			</tr>
			<tr>
				<td>Address :</td>
				<td><input type="text" name="Address"
				value="<%=request.getParameter("Address")==null?"":request.getParameter("Address")%>"/></td>
			</tr>
			<tr>
				<td><input type="submit" value="Submit"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.itcast.mvc.domian.Customer"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<%
		Object msg=request.getAttribute("message");
		if(msg!=null){
	%>
	
		<font color="red"><%=msg%></font>
	<%		
		}
		
		String name=null;
		String id=null;
		String address=null;
		String oldname=null;
		Customer customer=(Customer)request.getAttribute("customer");
		if(customer!=null){
			name=customer.getName();
			//int+""即转变成String类型
			id=customer.getID()+"";
			address=customer.getAddress();
			oldname=customer.getName();
		}else{
			id=request.getParameter("ID");
			oldname=request.getParameter("Name");
			name=request.getParameter("Name");
			address=request.getParameter("Address");
		}
	%>
	<form action="update.do" method="post">
		<input type="hidden" name="ID" value="<%=id%>"/>
		<input type="hidden" name="oldname" value="<%=oldname%>"/>
		<table>
			<tr>
				<td>Name :</td>
				<td><input type="text" name="Name" value="<%=name%>"/></td>
			</tr>
			<tr>
				<td>Address :</td>
				<td><input type="text" name="Address" value="<%=address%>"/></td>
			</tr>
			<tr>
				<td><input type="submit" value="Submit"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h4>对不起,请求有误!</h4>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<h4>用户添加成功!</h4>
	<!-- href="超链接的jsp" 然后就是超链接的名字-->
	<h4><a href="index.jsp">返回</a><h4>
</body>
</html>

代码中都有详细的个人注释,对MVC架构有了一定的了解,然后下面就是运行图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值