com.wiiyaya.consumer.web.main.controller.PrivilegeController.java Source code

Java tutorial

Introduction

Here is the source code for com.wiiyaya.consumer.web.main.controller.PrivilegeController.java

Source

/*
 * 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.provider.main.model.PrivilegeDto;
import com.wiiyaya.provider.main.service.PrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>??</p>
 * <p>
 * <p>??</p>
 * <p>
 * <p></p>
 *
 * @author wiiyaya
 */
@Controller
public class PrivilegeController {

    @Autowired
    private PrivilegeService privilegeService;

    @RequestMapping(value = MainURIResource.PATH_SYS_PRIVILEGE_ADD_INIT, method = { RequestMethod.GET })
    public String addInit(Model model,
            @ModelAttribute(MainURIResource.MODEL_PRIVILEGE_DTO) PrivilegeDto privilegeDto) {
        return MainURIResource.PAGE_SYS_PRIVILEGE_MAINTAIN;
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_PRIVILEGE_ADD, method = { RequestMethod.POST })
    @ResponseBody
    public PrivilegeDto add(
            @Validated @ModelAttribute(MainURIResource.MODEL_PRIVILEGE_DTO) PrivilegeDto privilegeDto,
            BindingResult result) throws ValidateException, BusinessException {
        if (result.hasErrors()) {
            throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                    result.getFieldError().getDefaultMessage());
        }
        return privilegeService.savePrivilege(privilegeDto);
    }
}