Java tutorial
/* * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.wiiyaya.consumer.web.main.controller; import com.wiiyaya.consumer.web.main.constant.MainURIResource; import com.wiiyaya.framework.common.exception.BusinessException; import com.wiiyaya.framework.common.exception.ValidateException; import com.wiiyaya.framework.common.model.easyui.EasyUiComboBox; import com.wiiyaya.framework.common.model.easyui.EasyUiDataGrid; import com.wiiyaya.framework.common.model.easyui.EasyUiTree; import com.wiiyaya.provider.main.enums.ResourceType; import com.wiiyaya.provider.main.model.ResourceDto; import com.wiiyaya.provider.main.service.PrivilegeService; import com.wiiyaya.provider.main.service.ResourceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * <p>??</p> * <p> * <p>??</p> * <p> * <p></p> * * @author wiiyaya */ @Controller public class ResourceController { @Autowired private ResourceService resourceService; @Autowired private PrivilegeService privilegeService; /** * ?? * @param parentId id? * @return */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_TREE, method = { RequestMethod.GET }) @ResponseBody public List<EasyUiTree> searchMenu(@RequestParam(value = "id", required = false) Long parentId) { if (parentId == null) { return resourceService.getMainResourceTree(); } else { return resourceService.getChildResourceTree(parentId); } } /** * ??? * @param resourceDto ? * @return ?? */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_LIST_INIT, method = { RequestMethod.GET }) public String resourceInit(@ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto) { return MainURIResource.PAGE_SYS_RESOURCE_LIST; } /** * ? * @param resourceDto ? * @param pageRequest ? * @return ?? */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_LIST, method = { RequestMethod.GET }) @ResponseBody public EasyUiDataGrid<ResourceDto> search( @ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto, Pageable pageRequest) { return resourceService.getAllResources(resourceDto, pageRequest); } /** * ?? * @param resourceDto ? * @return ? */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_ADD_INIT, method = { RequestMethod.GET }) public String addInit(@ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto) { resourceDto.setType(ResourceType.M); return MainURIResource.PAGE_SYS_RESOURCE_MAINTAIN; } /** * ? * @param resourceDto ?? * @param result ? * @throws ValidateException * @throws BusinessException */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_ADD, method = { RequestMethod.POST }) @ResponseBody public void add(@Validated @ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto, BindingResult result) throws ValidateException, BusinessException { if (result.hasErrors()) { throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(), result.getFieldError().getDefaultMessage()); } resourceService.saveResource(resourceDto); } /** * ?? * @param model ? * @param resourceId ?id * @return ?? * @throws BusinessException */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_AMD_INIT, method = { RequestMethod.GET }) public String amdInit(Model model, @PathVariable(MainURIResource.RESOURCE_ID) Long resourceId) throws BusinessException { ResourceDto resourceDto = resourceService.getResourceById(resourceId); model.addAttribute(MainURIResource.MODEL_RESOURCE_DTO, resourceDto); return MainURIResource.PAGE_SYS_RESOURCE_MAINTAIN; } /** * ? * @param resourceDto ?? * @param result ? * @throws ValidateException * @throws BusinessException */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_AMD, method = { RequestMethod.PUT }) @ResponseBody public void amd(@Validated @ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto, BindingResult result) throws ValidateException, BusinessException { if (result.hasErrors()) { throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(), result.getFieldError().getDefaultMessage()); } resourceService.updateResource(resourceDto); } /** * ???? * @param model ? * @param resourceId ?id * @return ???? * @throws BusinessException */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_AUTH_AMD_INIT, method = { RequestMethod.GET }) public String authAmdInit(Model model, @PathVariable(MainURIResource.RESOURCE_ID) Long resourceId) throws BusinessException { ResourceDto resourceDto = resourceService.getResourceById(resourceId); List<EasyUiComboBox> auths = privilegeService.getResourcePrivilege(resourceId); List<EasyUiComboBox> others = privilegeService.getOtherPrivilege(resourceId); model.addAttribute(MainURIResource.MODEL_RESOURCE_DTO, resourceDto); model.addAttribute(MainURIResource.MODEL_RESOURCE_PRIVILEGE_LIST, auths); model.addAttribute(MainURIResource.MODEL_PRIVILEGE_OTHER_LIST, others); return MainURIResource.PAGE_SYS_RESOURCE_AUTH_MAINTAIN; } /** * ??? * @param resourceDto ?? * @param result ? * @throws ValidateException * @throws BusinessException */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_AUTH_AMD, method = { RequestMethod.PUT }) @ResponseBody public void authAmd(@Validated @ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto, BindingResult result) throws ValidateException, BusinessException { if (result.hasErrors()) { throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(), result.getFieldError().getDefaultMessage()); } resourceService.updateResourceAuth(resourceDto); } /** * ?? * @param model ? * @param resourceId ?id * @return ?? * @throws BusinessException */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_CONTENT_AMD_INIT, method = { RequestMethod.GET }) public String contentAmdInit(Model model, @PathVariable(MainURIResource.RESOURCE_ID) Long resourceId) throws BusinessException { ResourceDto resourceDto = resourceService.getResourceById(resourceId); List<EasyUiComboBox> contents = resourceService.getResourceContent(resourceId); List<EasyUiComboBox> others = resourceService.getOtherResource(resourceId); model.addAttribute(MainURIResource.MODEL_RESOURCE_DTO, resourceDto); model.addAttribute(MainURIResource.MODEL_RESOURCE_CONTENT_LIST, contents); model.addAttribute(MainURIResource.MODEL_CONTENT_OTHER_LIST, others); return MainURIResource.PAGE_SYS_RESOURCE_CONTENT_MAINTAIN; } /** * ? * @param resourceDto ?? * @param result ? * @throws ValidateException * @throws BusinessException */ @RequestMapping(value = MainURIResource.PATH_SYS_RESOURCE_CONTENT_AMD, method = { RequestMethod.PUT }) @ResponseBody public void contentAmd(@Validated @ModelAttribute(MainURIResource.MODEL_RESOURCE_DTO) ResourceDto resourceDto, BindingResult result) throws ValidateException, BusinessException { if (result.hasErrors()) { throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(), result.getFieldError().getDefaultMessage()); } resourceService.updateResourceContent(resourceDto); } }