Struts开发第四步
调用AddressDAO.java操作数据库 修改AddressAction.java中的请求处理函数 来调用Hibernate层的AddressDAO.java 实现与数据库的交互
实现功能:
显示列表页面数据
实现增加联系人功能
实现修改联系人功能
实现删除联系人功能
1 显示列表页面数据
列表页面的请求会执行AddressAction.java中的list()函数 因此我们在该函数中来实现列表数据的提取
首先从当前session中取得已经登录的用户名 然后调用addressDAO对象的findAllByUsername()函数查询改用户的联系人列表 返回对象为list 最后调用setAttribute()将list对象保存起来 保存的变量名为addressList 供JSP页面显示
- public ActionForward list(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- String username = (String)request.getSession().getAttribute(Constants.USERNAME_KEY);
- List list = addressDAO.findAllByUsername(username);
- request.setAttribute("addresslist",list);
- ActionForward forward = mapping.findForward(Constants.LIST_KEY);
- return (forward);
- }
上面的函数中将提取的数据列表保存在request对象中 在返回到address.jsp页面后 只需要输出该数据列表即可
首先使用判断request中是否存在addressList对象 如果存在则使用来输出该对象 他包含三个属性
name属性表示要输出addressList对象
id属性表示列表中的每一个联系人对象都会赋给address
scope属性表示查找add热水sList对象的范围为request
在与之间添加一个代码 使用输出当前循环中的对象address的各个属性
代码为
- <logic:present name="addressList">
- <logic:iterate id="address" name="addressList" scope="request">
- <TR>
- <TD><bean:write name="address" property="name" scope="page"/></TD>
- <TD><bean:write name="address" property="***" scope="page"/></TD>
- <TD><bean:write name="address" property="mobile" scope="page"/></TD>
- <TD><bean:write name="address" property="email" scope="page"/></TD>
- <TD><bean:write name="address" property="qq" scope="page"/></TD>
- <TD><bean:write name="address" property="company" scope="page"/></TD>
- <TD><bean:write name="address" property="address" scope="page"/></TD>
- <TD><bean:write name="address" property="postcode" scope="page"/></TD>
- <TD><a href='address_edit.do?method=edit&id='><bean:message key="button.edit" /></a>
- <a href="address.do?method=delete&id=address" property="id" scope="page"/>"><bean:message key="button.delete" /></a></TD>
- </TR>
- </logic:iterate>
- </logic:present>
2 实现增加联系人功能
首先从当前Session中取出已经登录的用户名 然后调用addressDAO对象的i***ist()函数查询当前输入的联系人姓名是都存在 如果不存在 则创建一个Address对象address 根据用户输入的表单数据addressForm来设置该对象的属性 然后调用addressDAO对象的insert()函数插入对象address 如果还要返回到列表页面 因此使用与list()中相同的方法查询数据列表 并保存在request中 同时保存一条ActionMessages消息 如果当前输入的联系人姓名存在则保存一条错误信息 并返回到新建联系人页面
- public ActionForward insert(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
-
- ActionMessages messages = new ActionMessages();
- AddressForm addressForm = (AddressForm) form;
- String username = (String)request.getSession().getAttribute(Constants.USERNAME_KEY);
-
- // check if exists
- boolean b = addressDAO.i***ist(username, addressForm.getName());
- if (!b) {
- // insert object
- Address address = new Address();
- address.setUsername(username);
- address.setName(addressForm.getName());
- address.set***(addressForm.get***());
- address.setMobile(addressForm.getMobile());
- address.setEmail(addressForm.getEmail());
- address.setQq(addressForm.getQq());
- address.setCompany(addressForm.getCompany());
- address.setAddress(addressForm.getAddress());
- address.setPostcode(addressForm.getPostcode());
- addressDAO.insert(address);
-
- // save messages
- messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
- "address.message.add.success"));
- saveErrors(request, messages);
- List list = addressDAO.findAllByUsername(username);
- request.setAttribute("addressList", list);
- return mapping.findForward(Constants.LIST_KEY);
- } else {
- messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
- "address.message.add.failed"));
- saveErrors(request, messages);
- request.setAttribute("addressFormBean", addressForm);
- return mapping.findForward(Constants.ADD_KEY);
- }
- }
注意添加两个ActionMessages的消息 在资源文件中添加标签 并转换成中文的资源文件 同时在英文资源中添加英文的标签
我们在两个页面中添加如下所示的全局信息输出代码
- <b><html:errors property="org.apache.struts.action.GLOBAL_MESSAGE" /></b>
3 实现修改联系人功能
在列表页面的数据航中 每一行数据都对应一个修改按钮 单击该按钮即可进入到修改页面 在进入到修改页面address_edit.jsp时 会调用AddressAction.jsp的edit()函数 此时我们需要在该函数中查询当前id的联系人信息
首先从request中取得要显示的联系人id 该id是从列表进入到修改页面传递的参数 如果该id为空 则保存一条错误信息 直接返回到列表页面 如果该id 存在 则调用addressDAO的findById()函数从数据库中查找到联系人对象address 如果找不到该对象 则保存一条错误信息并返回否则就创建一个addressForm对象 将address的属性保存在addressForm对象中 并将addressForm对象保存在request中 供在页面中提取
- public ActionForward edit(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- if (isTimeout(request)) {
- return mapping.findForward(Constants.INDEX_KEY);
- }
- ActionMessages messages = new ActionMessages();
- String id = request.getParameter("id");
-
- if (id == null) {
- // id not exist, save messages
- messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
- "address.message.edit.notexist"));
- saveErrors(request, messages);
- return mapping.findForward(Constants.LIST_KEY);
- } else {
- // get object
- Address address = addressDAO.findById(id);
- if (address == null) {
- messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
- "address.message.edit.notexist"));
- saveErrors(request, messages);
- return mapping.findForward(Constants.LIST_KEY);
- }
-
- // save form object
- AddressForm addressForm = new AddressForm();
- addressForm.setId(address.getId().toString());
- addressForm.setName(address.getName());
- addressForm.set***(address.get***());
- addressForm.setMobile(address.getMobile());
- addressForm.setEmail(address.getEmail());
- addressForm.setQq(address.getQq());
- addressForm.setCompany(address.getCompany());
- addressForm.setAddress(address.getAddress());
- addressForm.setPostcode(address.getPostcode());
-
- // save in request
- request.setAttribute("addressFormBean", addressForm);
- return mapping.findForward(Constants.EDIT_KEY);
- }
- }
在address_edit.jsp中 我们已经增加了表单维持的代码 此时在该页面的表单中显示查询到的联系人的各个字段的信息 但是还有一耳光隐藏框id 需要保存当前联系人的id信息 因此按照其他表单显示数据的方式 将这个表单的显示代码设为hidden
- <logic:present name="addressFormBean">
- <html:hidden property="id" name="addressFormBean"/>
- </logic:present>
- <logic:notPresent name="addressFormBean">
- <input type="hidden" name="id">
- </logic:notPresent>
在修改联系人页面address_edit.jsp中 当点击 提交 按钮时 就会调用 AddressAction.java 的update()函数 在该函数中来实现修改数据的操作
首先从当前Session中取得已经登录的用户名 然后再创建一个Address对象address 根据addressForm的属性来设置该对象的属性 调用addressDAO对象的update()函数来更新当前的联系人到数据库中 并保存一条成功消息 由于还要返回到列表页面 因此使用与list() 相同的方法查询数据列表 并保存在request中
更新后的update()函数
- public ActionForward update(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- ActionMessages messages = new ActionMessages();
- AddressForm addressForm = (AddressForm) form;
- Address address = new Address();
- String username = (String)request.getSession().getAttribute(Constants.USERNAME_KEY);
-
- // update object
- address.setId(new Integer(addressForm.getId()));
- address.setUsername(username);
- address.setName(addressForm.getName());
- address.set***(addressForm.get***());
- address.setMobile(addressForm.getMobile());
- address.setEmail(addressForm.getEmail());
- address.setQq(addressForm.getQq());
- address.setCompany(addressForm.getCompany());
- address.setAddress(addressForm.getAddress());
- address.setPostcode(addressForm.getPostcode());
- addressDAO.update(address);
-
- // save messages
- messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
- "address.message.edit.success"));
- saveErrors(request, messages);
-
- List list = addressDAO.findAllByUsername(username);
- request.setAttribute("addressList", list);
- ActionForward forward = mapping.findForward(Constants.LIST_KEY);
- return (forward);
- }
同样添加国际标签
需要在address_edit.jsp页面中添加下面的全局信息输出代码
- <b><html:errors property="org.apache.struts.action.GLOBAL_MESSAGE" /></b>
4 实现删除联系人功能
- public ActionForward delete(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- ActionMessages messages = new ActionMessages();
- String id = request.getParameter("id");
- if (id == null) {
- // if id not exist
- messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
- "address.message.edit.notexist"));
- } else {
- // delete object
- addressDAO.delete(id);
-
- // save messages
- messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
- "address.message.delete.success"));
- String username = (String)request.getSession().getAttribute(Constants.USERNAME_KEY);
- List list = addressDAO.findAllByUsername(username);
- request.setAttribute("addressList", list);
- }
- saveErrors(request, messages);
- return mapping.findForward(Constants.LIST_KEY);
- }
添加国际标签
阅读(2645) | 评论(1) | 转发(0) |