MVC 增删改查

package web;

import java.io.IOException;
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 dao.AccountDao;
import dao.common.DaoFactory;
import entity.Account;

@WebServlet(urlPatterns="/account.do")
public class AccountServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2547680690878139285L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method=req.getParameter("method");
		if(method!=null&&!method.equals("")){
			if(method.equals("add")){
				addAccount(req,resp);
			}else if(method.equals("list")){
				listAccount(req,resp);
			}else if(method.equals("edit")){
				editAccount(req,resp);
			}else if(method.equals("update")){
				updateAccount(req,resp);
			}else if(method.equals("delete")){
				deleteAccount(req,resp);
			}
		}
	}
	public void addAccount(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		String name=req.getParameter("name");
		String age=req.getParameter("age");
		String address=req.getParameter("address");
		
		Account account=new Account();
		
		account.setName(name);
		account.setAge(Integer.parseInt(age));
		account.setAddress(address);
		try {
			AccountDao accountDao=DaoFactory.getInstance("accountDao", AccountDao.class);
			accountDao.save(account);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		listAccount(req,resp);
		}
	public void listAccount(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
	
		try {
			AccountDao accountDao = DaoFactory.getInstance("accountDao", AccountDao.class);
			List<Account> list=accountDao.findAll();
			req.setAttribute("accountList", list);
			req.getRequestDispatcher("list.jsp").forward(req, resp);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	//editAccount用于在数据库中找到实体account,然后把实体呈现给update.jsp
	public void editAccount(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
	
		try {
			AccountDao accountDao = DaoFactory.getInstance("accountDao", AccountDao.class);
			Account account=accountDao.get(Integer.parseInt(req.getParameter("id")));
			req.setAttribute("account", account);
			req.getRequestDispatcher("update.jsp").forward(req, resp);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//在update页面修改完后,由updateAccount()添加到数据库,不能有add,因为会再添加一个用户,而不是更新
	public void updateAccount(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
		String id=req.getParameter("id");
		String name=req.getParameter("name");
		String age=req.getParameter("age");
		String address=req.getParameter("address");
		try {
			AccountDao accountDao = DaoFactory.getInstance("accountDao", AccountDao.class);
			Account account=accountDao.get(Integer.parseInt(req.getParameter("id")));
			account.setName(name);
			account.setAge(Integer.parseInt(age));
			account.setAddress(address);
			accountDao.edit(account);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		listAccount(req,resp);
	}
	public void deleteAccount(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
		try {
			AccountDao accountDao = DaoFactory.getInstance("accountDao", AccountDao.class);
			accountDao.delete(Integer.parseInt(req.getParameter("id")));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		listAccount(req,resp);
	}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    	<a href="add.jsp">添加用户</a><br>
    	<a href="account.do?method=list">查看用户列表</a><br>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <form align="center" action="account.do?method=add" method="post">
    	<table>
    	
    		<tr>
    			<td>用户名:</td>
    			<td><input type="text" name="name"></td>
    		</tr>
    		
    		<tr>
    			<td>年龄:</td>
    			<td><input type="text" name="age"></td>
    		</tr>
    		
    		<tr>
    			<td>地址:</td>
    			<td><input type="text" name="address"></td>
    		</tr>
    		
    		<tr>
    			<td colspan="2">
    				<input type="submit" value="添加">&nbsp;&nbsp;
    				<input type="reset" value="重置">
    			</td>
    		</tr>
    	</table>
    </form>
  </body>
</html>
<%@ page language="java" import="java.util.*,entity.*" pageEncoding="UTF-8"%>
<%
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 'list.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h3>用户信息列表</h3>
    <table>
    	<tr>
    		<th>ID</th>
    		<th>name</th>
    		<th>age</th>
    		<th>address</th>
    	</tr>
    	<%
    		List<Account> list=(List<Account>)request.getAttribute("accountList");
    		int size=list.size()!=0?list.size():0;
    		for(int i=0;i<size;i++){
    			Account account=list.get(i);
    	 %>
    	 <tr>
    	 	<td><%=account.getId() %></td>
    	 	<td><%=account.getName() %></td>
    	 	<td><%=account.getAge() %></td>
    	 	<td><%=account.getAddress() %></td>
    	 	<td><a href="account.do?method=edit&id=<%=account.getId() %>">修改用户</a></td>
    	 	<td><a href="account.do?method=delete&id=<%=account.getId() %>">删除用户</a></td>
    	 </tr>
    	 <%} %>
    </table>
    
     
  </body>
</html>
<%@ page language="java" import="java.util.*,entity.*" pageEncoding="UTF-8"%>
<%
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 'update.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <form action="account.do?method=update" method="post">
    <h3>修改用户信息页面</h3>
    <%
    	Account account=(Account)request.getAttribute("account");
     %>
    <input type="hidden" name="id" value="<%=account.getId() %>">
    <table>		
    	<tr>
    		<td>NAME:</td>
    		<td><input type="text" name="name" value="<%=account.getName() %>"></td>
    	</tr>
    	<tr>
    		<td>Age:</td>
    		<td><input type="text" name="age" value="<%=account.getAge() %>"></td>
    	</tr>
    	<tr>
    		<td>Address:</td>
    		<td><input type="text" name="address" value="<%=account.getAddress() %>"></td>
    	</tr>
    	<tr>
    		<td colspan="2">
    			<input type="submit" value="修改完成">&nbsp;&nbsp;
    			<input type="reset" value="重置">
    		</td>
    	</tr>
    </table>
   </form> 
  </body>
</html>


你可能感兴趣的:(MVC 增删改查)