public static final String ERROR_VIEW = \public static final String SUCCESS_VIEW = \//显示检索结果的视图的名称 public static final String USER_LIST_VIEW = \} 【com.paic.userapp.management.web.util.WebContextNames】:没有变化
【com.paic.userapp.management.biz.util.BizContextNames】:没有变化
【com.paic.userapp.management.dto.UserDTO】:没有变化
6.3
Web层开发与实现
通过之前的功能分析,在“检索用户”功能的Web层有两种动作:一是“searchUser.screen”,这个动作将输入检索条件的页面显示出来,细节参考接下来的配置部分;二是“searchUser.do”,这个动作完成实际的检索功能。
需要开发的内容包括:“searchUser.jsp”,用于输入检索条件;“userList.jsp”,用于显示检索到的用户列表,使用JSTL的“c”标签进行显示处理;检索的Web层功能控制器为
“com.paic.userapp.management.web.controller.SearchUserController”。相关代码如下,注意其中的注释信息:
【webroot\\management\\searchUser.jsp】 <%@ include file=\ 【webroot\\management\%userList.jsp】 <%@ include file=\ ID Name Description
本文内容涉及中国平安保险(集团)股份有限公司商业秘密,未经书面许可,不得以任何形式披露、传播或扩散。
?中国平安保险(集团)股份有限公司,版权所有,不得侵犯
第 36 页 共 43 页
package com.paic.userapp.management.web.controller; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.paic.pafa.app.dto.ServiceRequest; import com.paic.pafa.app.dto.ServiceResponse; import com.paic.pafa.app.web.exception.PafaWebException; import com.paic.pafa.app.web.servlet.ModelAndView; import com.paic.pafa.app.web.servlet.mvc.AbstractController; import com.paic.userapp.management.util.ServiceRequestID; import com.paic.userapp.management.web.util.ModelNames; import com.paic.userapp.management.web.util.ViewNames; import com.paic.userapp.management.web.util.WebContextNames; public class SearchUserController extends AbstractController{ protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse httpResponse) throws Exception { //准备ServiceRequest String name=request.getParameter(\idPiece\ ServiceRequest serviceRequest = new ServiceRequest(); serviceRequest.setRequestedServiceID(ServiceRequestID.SEARCH_USER); serviceRequest.setParameter(\idPiece\ //发送ServiceRequest以获得ServiceResponse ServiceResponse serviceResponse = dispatchService(serviceRequest,WebContextNames.PAFA_AC); //准备Web视图 Map model = serviceResponse.getModel(); return new ModelAndView(ViewNames.USER_LIST_VIEW, ModelNames.MODEL_USER_LIST, model.get(ServiceResponse.SERVICE_RESPONSE_RESULT)); } } 6.4
配置Ibatis SqlMapping
为进行数据检索,还需要在“sqlmap-mapping.xml”中增加SQL查询语句,如下表中突出的部分:
【src\\config\\biz\\sql-mapping.xml】:新增Search语句
?中国平安保险(集团)股份有限公司,版权所有,不得侵犯
第 37 页 共 43 页
Biz层开发与实现
Biz层的框架也通过之前“新增用户”功能建立了起来,只需在Service和DAO中增加新的检索方法即可,但是我们还需要一个独立的Action处理用于检索的ServiceRequest,如下面的代码所示,注意其中突出的部分:
【com.paic.userapp.management.integration.UserDAO】 package com.paic.userapp.management.integration; import java.util.List; import com.paic.pafa.app.integration.dao.PafaDAOException; import com.paic.pafa.app.integration.dao.PafaDAO; import com.paic.userapp.management.dto.UserDTO; public interface UserDAO extends PafaDAO{ public void createUser(UserDTO user) throws PafaDAOException; public List searchUserByIdPiece(String idPiece) throws PafaDAOException; } 【com.paic.userapp.management.integration.impl.UserIbatisDAO】 package com.paic.userapp.management.integration.impl; import java.util.List; import com.paic.pafa.app.integration.dao.PafaDAOException; import com.paic.pafa.app.lwc.service.persistence.dao.ibatis.support.SqlMapClientDaoSupport; import com.paic.userapp.management.dto.UserDTO; import com.paic.userapp.management.integration.UserDAO; import com.paic.pafa.app.lwc.core.context.support.PafaCoreContexton; import com.paic.pafa.app.lwc.service.persistence.dao.DataAccessException; public class UserIbatisDAO extends SqlMapClientDaoSupport implements UserDAO{ public void createUser(UserDTO user) throws PafaDAOException{ //获得当前事务的ID String txnId = PafaCoreContexton.getInstance().getThreadContext().getTxnID(); try{ //通过Ibatis执行名称为“addUser”的SQL语句 getSqlMapClientTemplate().insert(\}catch(DataAccessException e){ 本文内容涉及中国平安保险(集团)股份有限公司商业秘密,未经书面许可,不得以任何形式披露、传播或扩散。
?中国平安保险(集团)股份有限公司,版权所有,不得侵犯
第 38 页 共 43 页
//将数据库例外包装为pafa的例外类型 throw new PafaDAOException(txnId, \ \} } public List searchUserByIdPiece(String idPiece)throws PafaDAOException{ //获得当前事务的ID String txnId = PafaCoreContexton.getInstance().getThreadContext().getTxnID(); try{ //通过Ibatis执行名称为“searchUserByIdPiece”的SQL语句 return getSqlMapClientTemplate(). queryForList(\}catch(DataAccessException e){ //将数据库例外包装为pafa的例外类型 throw new PafaDAOException(txnId, \ \e);} } public void setBeanName(String arg0) { // TODO Auto-generated method stub } }
【com.paic.userapp.management.biz.service.UserService】:定义新的search方法 package com.paic.userapp.management.biz.service; import java.util.List; import com.paic.pafa.app.biz.service.BusinessServiceException; import com.paic.userapp.management.dto.UserDTO; public interface UserService { public void addUser(UserDTO user)throws BusinessServiceException; public List searchUser(String idPiece) throws BusinessServiceException; } 【com.paic.userapp.management.biz.service.impl.UserPojoService】:实现search方法 package com.paic.userapp.management.biz.service.impl; import java.util.List; import com.paic.pafa.app.biz.service.BaseService; import com.paic.pafa.app.biz.service.BusinessServiceException; import com.paic.pafa.app.integration.dao.PafaDAOException; import com.paic.userapp.management.biz.service.UserService; import com.paic.userapp.management.biz.util.BizContextNames; import com.paic.userapp.management.dto.UserDTO; import com.paic.userapp.management.integration.UserDAO; 本文内容涉及中国平安保险(集团)股份有限公司商业秘密,未经书面许可,不得以任何形式披露、传播或扩散。
?中国平安保险(集团)股份有限公司,版权所有,不得侵犯
第 39 页 共 43 页
public class UserPojoService extends BaseService implements UserService{ public void addUser(UserDTO user)throws BusinessServiceException{ try { //通过Context获得DAO实例 UserDAO userDAO = (UserDAO) context. getBean(BizContextNames.USER_DAO); userDAO.createUser(user); } catch (PafaDAOException e) { throw new BusinessServiceException(e); } } public List searchUser(String idPiece) throws BusinessServiceException{ try { UserDAO userDAO = (UserDAO) context. getBean(BizContextNames.USER_DAO); return userDAO.searchUserByIdPiece(idPiece); } catch (PafaDAOException e) { throw new BusinessServiceException(e); } } } 【com.paic.userapp.management.biz.action.SearchUserAction】 package com.paic.userapp.management.biz.action; import java.util.HashMap; import java.util.List; import java.util.Map; import com.paic.pafa.app.biz.action.AbstractAction; import com.paic.pafa.app.biz.service.BusinessServiceException; import com.paic.pafa.app.dto.ServiceRequest; import com.paic.pafa.app.dto.ServiceResponse; import com.paic.userapp.management.biz.service.UserService; import com.paic.userapp.management.biz.util.BizContextNames; import com.paic.pafa.app.lwc.core.context.support.PafaCoreContexton; import com.paic.pafa.core.exceptions.PafaRuntimeException; public class SearchUserAction extends AbstractAction{ public ServiceResponse perform(ServiceRequest request) throws BusinessServiceException { //获得当前事务的ID String txnId = PafaCoreContexton.getInstance().getThreadContext().getTxnID(); try{ //获取检索条件 String idPiece=(String)request.getParameter(\ 本文内容涉及中国平安保险(集团)股份有限公司商业秘密,未经书面许可,不得以任何形式披露、传播或扩散。
?中国平安保险(集团)股份有限公司,版权所有,不得侵犯
第 40 页 共 43 页