JAVAWEB-MVC案例-新增-修改-配置底层存储源(4)

一、新增操作

(1)表单参数回显

 

(2)使用重定向,可以避免表单的重复提交问题

response.sendRedirect("success.jsp");

(3)设置信息到另一个界面

request.setAttribute("message","用户名已经存在,请重新填写");

Object msg =request.getAttribute("message");

(4)新增操作

Customer customer = new Customer(name,address,phone);

customerDAO.save(customer);

 

二、修改操作

修改流程:

(1)<a href="edit.do?id=<%=customer.getId() %>">UPDATE</a>

获取修改数据的id,并跳转到CustomerServlet中的edit方法

主要代码:

<td><%= customer.getId() %></td>
<td><%= customer.getName() %></td>
<td><%= customer.getAddress() %></td>
<td><%= customer.getPhone() %></td>
<td>
	<!-- 进行该条数据的更新 -->
	<a href="edit.do?id=<%=customer.getId() %>">UPDATE</a>

(2)在edit方法中跳转到updatecustomer.jsp,并传入Customer对象。

 

主要代码:

//获取请求参数
		String idStr = request.getParameter("id");
		
		//调用CustomerDAO的customerDAO.get(id)获取和id对应的Customer对象的customer
		try {
			//获得id对应的Customer对象
			Customer customer = customerDAO.get(Integer.parseInt(idStr));
			if(customer != null)
			{
				forwardPath = "/updatecustomer.jsp";
				//将customer放入request中
				request.setAttribute("customer", customer);
			}
		} catch (NumberFormatException e) {
		}
		//响应updatecustomer.jsp页面:转发
		request.getRequestDispatcher(forwardPath).forward(request,response);

(3)在updatecustomer.jsp页面进行数据的更改,并进行进入CustomerServlet中进行update方法。

主要代码:

 

<form action="update.do" method="post">
		<input type="hidden" name="id" value="<%= id %>">
		<input type="hidden" name="oldName" value="<%= oldName %>">
		
		<table> 
			<tr>
				<td>CustomerName:</td>
				<td><input type="text" name="name" value="<%= name%>"/></td>
			</tr>

 

(4)在CustomerServlet中执行update方法,进行数据更新。

//如果老名字和新名字不同
		if(!oldName.equalsIgnoreCase(name))
		{
			//搜索数据库中是否已经包含了新名字的数据
			long count = customerDAO.getCountWithName(name);
			//包含,则返回updatecustomer.jsp,并显示信息
			if(count > 0)
			{
				request.setAttribute("message", "用户名已经被占用,请重新填写");
				request.getRequestDispatcher("/updatecustomer.jsp").forward(request, response);
				return;
			}
		}
		//老名字和新名字相同,都会进行更新
		Customer customer = new Customer(name,address,phone);
		customer.setId(Integer.parseInt(id));

		customerDAO.update(customer);
		
		response.sendRedirect("query.do");

三、MVC案例之通过配置切换底层存储源

 

主体代码:

(1)InitServlet.java

public class InitServlet extends HttpServlet {
	@Override
	public void init() throws ServletException {
		//初始化接口类别为jdbc
		CustomerDAOFactory.getInstance().setType("jdbc");
		
		//读取switch.properties
		InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/switch.properties");
		Properties properties = new Properties();
		try {
			properties.load(in);
			//获取文件中的接口类型
			String type = properties.getProperty("type");
			CustomerDAOFactory.getInstance().setType(type);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

(2)CustomerDAOFactory.java

private java.util.Map<String,CustomerDAO> daos = new HashMap<String,CustomerDAO>();
	
	//工厂的初始化
	private CustomerDAOFactory(){
		daos.put("jdbc", new CustomerDAOJdbcImpl());
		daos.put("xml", new CustomerDAOXMLImpl());
	}
	
	//单例模式
	private static CustomerDAOFactory instance = new CustomerDAOFactory();
	
	public static CustomerDAOFactory getInstance()
	{
		return instance;
	}
	
	private static String type = null;
	
	//设置接口类别
	public void setType(String type)
	{
		this.type = type;
	}
	
	//获取接口类别
	public CustomerDAO getCustomerDAO()
	{
		return daos.get(type);
	}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值