首页>代码>基于SSM的小星星少儿教育系统>/EED/src/main/java/com/smart/controller/AdminController.java
package com.smart.controller;

import com.smart.dao.AdminDao;
import com.smart.dao.ContentDao;
import com.smart.dao.SourceDao;
import com.smart.dao.TypeDao;
import com.smart.domain.Admin;
import com.smart.domain.Content;
import com.smart.domain.Source;
import com.smart.domain.Type;
import com.smart.service.AdminService;
import com.smart.service.ContentService;
import com.smart.service.SourceService;
import com.smart.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class AdminController {

    @Autowired
    private SourceService sourceService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private AdminService adminService;

    @RequestMapping("test")
    public String toTest(){return "test";}


    @RequestMapping("toLogin")
    public String toLogin(){return "/admin/login";}

    @RequestMapping("login")
    public ModelAndView Login(Admin admin){
        ModelAndView modelAndView=new ModelAndView();
        Admin admin2=adminService.selectAdmin(admin);
        if (admin2!=null){
            modelAndView.setViewName("/admin/manager");
        }else {
            modelAndView.setViewName("/admin/login");
            modelAndView.addObject("tips","<script>alert('用户名或密码错误')</script>");
        }
        return modelAndView;
    }

    @RequestMapping("toMain")
    public String toAdminMain(){
        return "/admin/manager";
    }

    @RequestMapping("toaddLaiyuan")
    public String toaddLaiyuan(){
        return "/admin/addLaiyuan";
    }

    @RequestMapping("toAddType")
    public String toAddType(){
        return "/admin/addType";
    }

    @RequestMapping("toContent")
    public ModelAndView toContent(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/admin/content");
        List list=contentService.getAll();
        modelAndView.addObject("contents",list);
        return modelAndView;
    }

    @RequestMapping("toaddContent")
    public ModelAndView toaddContent(){
        ModelAndView modelAndView=new ModelAndView();
        List types=typeService.getAll();
        List sources=sourceService.getAll();
        modelAndView.addObject("types",types);
        modelAndView.addObject("sources",sources);
        modelAndView.setViewName("/admin/addContent");
        return modelAndView;
    }

    @RequestMapping("tolaiyuan")
    public ModelAndView tolaiyuan(){
        ModelAndView modelAndView =new ModelAndView();
        List list=sourceService.getAll();
        modelAndView.setViewName("/admin/laiyuan");
        modelAndView.addObject("sources",list);
        return modelAndView;
    }

    @RequestMapping("toType")
    public ModelAndView toType(){
        List lists=typeService.getAll();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("types",lists);
        modelAndView.setViewName("/admin/type");
        return modelAndView;
    }

    @RequestMapping("addLaiyuan")
    public ModelAndView addLaiyuan(Source source){
        ModelAndView modelAndView=new ModelAndView();
        sourceService.addSource(source);
        modelAndView.addObject("tips", "<script>alert('添加成功!')</script>");
        modelAndView.setViewName("/admin/addLaiyuan");
        return modelAndView;
    }

    @RequestMapping("addType")
    public ModelAndView addType(Type type){
        ModelAndView modelAndView=new ModelAndView();
        typeService.addType(type);
        modelAndView.addObject("tips","<script>alert('添加成功!')</script>");
        modelAndView.setViewName("/admin/addType");
        return modelAndView;
    }

    @RequestMapping("addContent")
    public ModelAndView addContent(MultipartFile file,String editorValue,Content content) throws IOException {
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("redirect:toContent");
        System.out.println(content.getTitle());
        System.out.println(content.getFromName());
        //保存数据库的路径
        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath="";
        //定义 文件名
        String filename=null;
        if(!file.isEmpty()){
            //生成uuid作为文件名称 防止重复上传
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            //获得文件类型(可以判断如果不是图片,禁止上传)
            String contentType=file.getContentType();
            //获得文件后缀名
            String suffixName=contentType.substring(contentType.indexOf("/")+1);
            //得到 文件名
            filename=uuid+"."+suffixName;

            //根据后缀判断文件的

            switch (suffixName){
                case "mp3":localPath="E:\\WorkShop\\EED\\src\\main\\webapp\\static\\music\\";
                break;
                case "mp4": localPath="E:\\WorkShop\\EED\\src\\main\\webapp\\static\\video\\";
                break;
                case "jpg":
                case  "png":localPath="E:\\WorkShop\\EED\\src\\main\\webapp\\static\\picture\\";
                break;
                default:localPath="E:\\WorkShop\\EED\\src\\main\\webapp\\static\\other\\";
            }

            //文件保存路径
            file.transferTo(new File(localPath+filename));
            content.setUrlContext(filename);
        }
        content.setText(editorValue);
        contentService.addContent(content);
        return modelAndView;
    }

    @RequestMapping("deleteOne")
    public String deleteOne(int id){
        contentService.deleteContent(id);
        return "redirect:toContent";
    }

    @RequestMapping("updateOne")
    public ModelAndView updateOne(int id){
        Content content=contentService.selectContent(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("content",content);
        modelAndView.setViewName("/admin/updateContent");
        return  modelAndView;
    }
}
最近下载更多
爱吃鱼的猫Vital  LV6 2023年7月24日
jk-mack  LV5 2023年2月19日
zxc131313  LV12 2022年11月21日
Ficxtion  LV2 2022年10月28日
a2831244768  LV4 2022年10月27日
wz520135  LV7 2022年10月16日
qwer123546  LV13 2022年9月18日
evagsd  LV6 2022年8月30日
lichengai  LV7 2022年8月17日
zxs872  LV2 2022年8月14日
最近浏览更多
zayuchuan 4月6日
暂无贡献等级
admin_z  LV22 1月28日
liiiyou  LV1 1月26日
Camellia-while  LV1 1月2日
tangjianzhong 2023年12月19日
暂无贡献等级
haha不会 2023年12月12日
暂无贡献等级
lasttimeapp 2023年11月27日
暂无贡献等级
jswlsq 2023年11月21日
暂无贡献等级
32111152967 2023年11月9日
暂无贡献等级
再曾一碗饭 2023年10月24日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友