梦境迷离

梦境亦是美,醒来亦是空。

项目重构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进行快捷键组织导入,提示也不好。
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34446485/article/details/80334598
文章标签: Scala
个人分类: Scala
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭