您的位置:首页 > 编程语言 > Java开发

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

2018-01-13 10:44 162 查看
一、新增操作

(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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐