项目重构03 解决Scala类型与Java类型冲突

package cn.edu.jxnu.base.controller.admin.system

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.beans.factory.annotation.Autowired
import cn.edu.jxnu.base.service.IResourceService
import cn.edu.jxnu.base.controller.BaseController
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.PathVariable
import cn.edu.jxnu.base.entity.ZtreeView
import java.util.{ List => JavaList }
import scala.collection.immutable.{ List => _, _ }
import cn.edu.jxnu.base.service.specification.SimpleSpecificationBuilder
import org.apache.commons.lang3.StringUtils
import cn.edu.jxnu.base.entity.Resource
import org.springframework.data.domain.Page
import cn.edu.jxnu.base.service.specification.SpecificationOperator.Operator
import org.springframework.ui.ModelMap
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import cn.edu.jxnu.base.common.JsonResult

@Controller
@RequestMapping(value = { Array("/admin/resource") })
class ResourceScala @Autowired() (val resourceService: IResourceService) extends BaseController {

    @RequestMapping(value = { Array("/tree/{resourceId}") })
    @ResponseBody
    def tree(@PathVariable resourceId: Int): JavaList[ZtreeView] = {
        resourceService.tree(resourceId)

    }

    @RequestMapping(value = { Array("/index") })
    def index() = {
        "admin/resource/index"
    }

    @RequestMapping(value = { Array("/list") })
    @ResponseBody
    def list(): Page[Resource] = {
        val builder = new SimpleSpecificationBuilder[Resource]()
        val searchText = request.getParameter("searchText")
        if (StringUtils.isNotBlank(searchText)) {
            builder.add("name", Operator.likeAll.name(), searchText)
        }
        resourceService.findAll(builder.generateSpecification(), getPageRequest())
    }

    @GetMapping(value = { Array("/add") })
    def add(map: ModelMap) = {
        val list = resourceService.findAll()
        map.put("list", list)
        "admin/resource/form"
    }

    @GetMapping(value = { Array("/edit/{id}") })
    def edit(@PathVariable id: Int, map: ModelMap) = {
        val resource = resourceService.find(id)
        map.put("resource", resource)
        val list = resourceService.findAll()
        map.put("list", list)
        "admin/resource/form"
    }

    @ResponseBody
    @PostMapping(value = { Array("/edit") })
    def edit(resource: Resource, map: ModelMap): JsonResult = {
        try {
            resourceService.saveOrUpdate(resource)
            // 可能存在需要在增加资源的时候默认给管理员增加权限
        } catch {
            case e: Exception =>
                return JsonResult.failure(e.getMessage())
        }
        JsonResult.success()
    }

    @PostMapping(value = { Array("/delete/{id}") })
    @ResponseBody
    def delete(@PathVariable id: Int, map: ModelMap): JsonResult = {
        try {
            resourceService.delete(id);
        } catch {
            case e: Exception =>
                e.printStackTrace();
                return JsonResult.failure(e.getMessage());
        }
        JsonResult.success();
    }
}

1、隐藏某个类型

import scala.collection.immutable.{ List => _, _ }

经测试还是可以通过增加import重新导入一个

2、重命名导入的类型

import java.util.{ List => JavaList }

可以直接使用JavaList了

def tree(@PathVariable resourceId: Int): JavaList[ZtreeView] = {
        resourceService.tree(resourceId)

    }

选其一即可。重命名加上Java前缀个人感觉更好

PS: Eclipse不能对Scala进行快捷键组织导入,提示也不好。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值