开发Portlet第二步:如何将Crystal静态Portlet转变成基于测试数据的动态Portlet?
当基于Crystal的静态Portlet开发完成后,在与后台服务联调前,还需要将Portlet转换成基于测试数据的动态Portlet。具体步骤如下:
分步指南
- 复制Portlet项目,并修改相关的pom.xml、src/main/webapp/WEB-INF/liferay-plugin-package.xml、src/main/webapp/WEB-INF/liferay-plugin-package.properties文件中的Atifact-ID、module-id等信息,区别原静态Portlet项目,防止两个项目重复,导致不能在一个环境中运行;
- 修改src/main/webapp/WEB-INF/portlet.xml、src/main/webapp/WEB-INF/liferay-portlet.xml、/static-development-portlet/src/main/webapp/WEB-INF/liferay-display.xml中的portlet-name等信息,区别原项目的portlet-name信息,防止与原静态portlet不能在一个环境中运行;
- 修改src/main/webapp/WEB-INF/spring下****-portlet.xml文件名,保持****部分与相应portlet-name性息(去除横杠)一致;
- 查看****-portlet.xml文件中定义的包路径是否是项目所需,否则修改;
- 查看src/main/java下的包路径是否跟****-portlet.xml文件中定义的包路径一致,否则修改;
创建测试数据项目:
- 创建Java Maven项目****-service-stub,结构如下:
- 修改pom.xml文件,添加远程服务api包依赖:
- 根据api实现相关服务的实现类,但方法内部只需返回设计的测试数据即可,如下:
@Service public class ApplicationManager implements ApplicationService { /* (non-Javadoc) * @see com.gsoft.portlet.uum.application.ApplicationService#list() */ @Override public List<Application> list() { List<Application> list = new ArrayList<Application>(); for(int i = 0; i < 20; i++){ Application app = new Application(); app.setId(new Long(i)); app.setName("Name " + i); app.setCode("code " + i); app.setUrl("http://localhsot/" + i); app.setDisabled(i %3 == 0 ? true : false); list.add(app); } return list; } /* (non-Javadoc) * @see com.gsoft.portlet.uum.application.ApplicationService#getById(java.lang.Long) */ @Override public Application getById(Long id) { Application app = new Application(); app.setId(10L); app.setName("Name 10"); app.setCode("code 10"); app.setUrl("http://localhsot/10"); return app; } }
修改静态Portlet项目为动态测试Portlet:
- 在Porltet项目中引入测试数据项目及远程服务接口;
-
修改src/main/java下相应Portlet类,调用远程api及测试数据服务,返回测试数据;
@Controller public class ApplicationManagementPortlet extends AbstractCrystalPortlet { @Resource private AuthSystemService authSystemService; @RequestMapping public String view(Model model, RenderRequest request) { List<AuthSystemDto> result = authSystemService.finaAllAuthSystemList(); request.setAttribute("result", result); return "view"; } @RequestMapping(params = "action=add") public String view4add(Model model, @Param String rediract) { model.addAttribute("rediract", rediract); return "add"; } @RequestMapping(params = "action=edit") public String view4edit(Model model, @RequestParam String rediract,@RequestParam Long id) { model.addAttribute("rediract", rediract); AuthSystemDto authSystemDto = authSystemService.findOne(id); model.addAttribute("system", authSystemDto); return "add"; } @RequestMapping(params = "action=del") public String delete(Model model, RenderRequest request, @RequestParam Long id) { authSystemService.deleteAuthSystem(id); return "pop_up_success"; } @RequestMapping(params = "action=editState") public String editState(Model model, RenderRequest request, @RequestParam Long id,@RequestParam String state) { AuthSystemDto authSystemDto = new AuthSystemDto(); authSystemDto.setId(id); if(StringUtils.equals(UserRepConstants.UserRepState.DISABLE, state)) { authSystemDto.setState(UserRepConstants.UserRepState.ENABLE); } else { authSystemDto.setState(UserRepConstants.UserRepState.DISABLE); } authSystemService.updateAuthSystemState(authSystemDto ); return "pop_up_success"; } @RequestMapping(params = "action=save") public String save(Model model, @ModelAttribute("authSystem")AuthSystemDto authSystem, @RequestParam String rediract){ if(StringUtils.isEmpty(authSystem.getState())) { authSystem.setState("1"); } if(authSystem.getId() == null) { authSystemService.saveAuthSystem(authSystem); }else { authSystemService.updateAuthSystem(authSystem); } model.addAttribute("rediract", rediract); return "pop_up_success"; } @RequestMapping(params = "action=check") public ModelAndView checkDupCode(Model model, RenderRequest request, @RequestParam Long id,@RequestParam String code) throws JsonProcessingException { AuthSystemDto system = new AuthSystemDto(); system.setCode(code); system.setId(id); Boolean exists = authSystemService.existsAuthSystemCode(system); Map<String, Object> result = new HashMap<String, Object>(); result.put("success", Boolean.TRUE); result.put("data", exists); return ajax(result); } }
- 修改src/main/webapp/WEB-INF/jsp下相关jsp文件,增加标签,获取后台portlet类返回的数据,并依据原静态HTML代码格式,动态生成相应的代码。