视频列表的android客户端和springmvc服务端实现(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_23195583/article/details/54343107

在jsp页面通过业务逻辑层进行相应的数据操作,因为我把用户登录注销了,所以当前用户是空(这里偷懒了,没有加用户的数据库,因为系统我一个人用就把用户定死了)。
这里我用了三个jsp页面进行相应的列表插入修改操作。不过首先还得先看controller:

package com.fanyafeng.controller;

import com.fanyafeng.interceptor.AccessRequired;
import com.fanyafeng.model.ItemsCustomModel;
import com.fanyafeng.model.ItemsQueryVo;
import com.fanyafeng.model.VideosModel;
import com.fanyafeng.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;

/**
 * Author: fanyafeng
 * Data: 17/1/4 10:48
 * Email: [email protected]
 */
@Controller
@RequestMapping(value = "/videos")
public class VideosController {

    @Autowired
    private VideoService videoService;

    @RequestMapping(value = "/videosList")
    public String videoList(Model model, VideosModel videosModel) {

//        List<ItemsCustomModel> itemsCustomModelList = itemsService.findItemsList(itemsQueryVo);
//        model.addAttribute("itemsList", itemsCustomModelList);

        List<VideosModel> videosModelList = videoService.findAllVideo();
        model.addAttribute("videoList", videosModelList);
        return "videos/videoList";
    }

    @AccessRequired(required = false)
    @RequestMapping(value = "/findVideoByPage", produces = "application/json")
    @ResponseBody
    public Map<String, Object> getVideoList(int page) {
        Map<String, Object> objectsMap = new HashMap<>();
        objectsMap.put("state", "ok");
        objectsMap.put("videoList", videoService.findVideoByPage(page));
        return objectsMap;
    }

    /**
     * 返回文件类的接口
     *
     * @return
     */
    @RequestMapping(value = "/getPic", produces = MediaType.IMAGE_JPEG_VALUE)//图片可以
    @ResponseBody
    public byte[] getPic() {
        File f = new File("/Users/fanyafeng/IntelliJProject/SpringMybatisDemo/src/main/resources/apk/9.jpg");
        return getByte(f);
    }


    public static byte[] getByte(File file) {
        byte[] bytes = null;
        if (file != null) {
            InputStream is = null;
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            int length = (int) file.length();
            if (length > Integer.MAX_VALUE)   //当文件的长度超过了int的最大值
            {
                System.out.println("this file is max ");
                return null;
            }
            bytes = new byte[length];
            int offset = 0;
            int numRead = 0;
            try {
                while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
                    offset += numRead;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            //如果得到的字节长度和file实际的长度不一致就可能出错了
            if (offset < bytes.length) {
                System.out.println("file length is error");
                return null;
            }
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bytes;
    }

    @RequestMapping(value = "/insertVideo")
    public String insertVideo() {
        return "videos/insertVideo";
    }

    @RequestMapping(value = "/editVideo")
    public String editVide(Model model, Integer id) {
        VideosModel videosModel = videoService.findVideoById(id);
        if (videosModel != null) {
            model.addAttribute("video", videosModel);
        }
        return "videos/editVideo";
    }

    @RequestMapping(value = "/editVideoSubmit")
    public String insertVideoSubmit(HttpServletRequest httpServletRequest, @ModelAttribute("video") VideosModel videosModel) {

        if (videosModel.getId() == 0) {
            System.out.print("获取的id的数值:无");
            videoService.insertVideo(videosModel);
        } else {
            videoService.updateVideoById(videosModel);
        }
        return "redirect:videosList";
    }

}

看一下列表jsp:

<%--
  Created by IntelliJ IDEA.
  User: fanyafeng
  Date: 16/10/18
  Time: 下午12:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
    <title>视频列表</title>
    <script type="text/javascript">
        function deleteItems() {
//            提交form
            document.itemsForm.action = "${pageContext.request.contextPath}/items/deleteItems";
            document.itemsForm.submit();
        }

        function queryItems() {
//            提交form
            document.itemsForm.action = "${pageContext.request.contextPath}/items/itemsList";
            document.itemsForm.submit();
        }
    </script>
</head>
<body>
当前用户:${username}
<c:if test="${username!=null}">
    <a href="${pageContext.request.contextPath}/logout">退出</a>
</c:if>
<form name="itemsForm" action="${pageContext.request.contextPath}/items/itemsList" method="post">
    查询条件:
    <table width="100%" border="1">
        <tr>
            <td>商品名称:<input name="itemsCustomModel.name"></td>
            <td>
                <input type="button" value="查询" onclick="queryItems()">
                <input type="button" value="批量删除" onclick="deleteItems()">
            <td><a href="${pageContext.request.contextPath}/items/editItemsQuery">批量修改</a></td>
            <td><a href="${pageContext.request.contextPath}/videos/insertVideo">添加视频</a></td>
            </td>
        </tr>
    </table>

    视频列表:
    <table border="1" width="100%">
        <tr>
            <td>选择</td>
            <td >视频标题</td>
            <td >视频图片</td>
            <td >视频连接</td>
            <td >视频描述</td>
            <td >操作</td>
        </tr>
        <c:forEach items="${videoList}" var="item">
            <tr>
                <td><input type="checkbox" name="itemId" value="${item.id}"/></td>
                    <%--<td><fmt:formatDate value="${item.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>--%>
                <td>${item.title}</td>
                <td><div style="width:200px;height:50px;overflow-x:hidden;overflow-y:hidden;">${item.img}</div></td>
                <td><div style="width:200px;height:50px;overflow-x:hidden;overflow-y:hidden;">${item.video_url}</div></td>
                <td><div style="width:200px;height:50px;overflow-x:hidden;overflow-y:hidden;">${item.des}</div></td>

                <td><a href="${pageContext.request.contextPath}/videos/editVideo?id=${item.id}">修改</a></td>

                <%--<td><a href="${pageContext.request.contextPath}/videos/editVideo?id=${item.id}">删除</a></td>--%>
            </tr>
        </c:forEach>
    </table>


</form>
</body>
</html>

这里需要在控制层传入一个model的列表,进行显示,修改的话传给下一个control是一个model并不仅仅是一个id,增加的话什么都没有传,还有我设计的数据库id是自增的,修改的话是根据id进行相应数据的修改。
插入和编辑差不多,关键的是sql语句区别比较大
插入页面:

<%--
  Created by IntelliJ IDEA.
  User: fanyafeng
  Date: 16/10/18
  Time: 下午2:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>修改视频信息</title>
</head>
<body>
<form id="itemForm" action="${pageContext.request.contextPath }/videos/editVideoSubmit" method="get">
    <%--<input type="hidden" name="id" value="${video.id }"/>--%>
    修改视频信息:
    <table width="100%" border=1>
        <tr>
            <td>视频标题</td>
            <td>
                <textarea rows="3" cols="180" name="title">${video.title }</textarea>
            </td>
        </tr>
        <tr>
            <td>视频简介</td>
            <td>
                <textarea rows="3" cols="180" name="des">${video.des }</textarea>
            </td>
        </tr>
        <tr>
            <td>视频头图</td>
            <td>
                <textarea rows="3" cols="180" name="header_img">${video.header_img }</textarea>
            </td>
        </tr>
        <tr>
            <td>视频占位图</td>
            <td>
                <textarea rows="3" cols="180" name="img">${video.img }</textarea>
            </td>
        </tr>
        <tr>
            <td>视频连接</td>
            <td>
                <textarea rows="3" cols="180" name="video_url">${video.video_url }</textarea>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="提交"/>
            </td>
        </tr>
    </table>

</form>
</body>
</html>

修改页面:

<%--
  Created by IntelliJ IDEA.
  User: fanyafeng
  Date: 16/10/18
  Time: 下午2:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>修改视频信息</title>
</head>
<body>
<form id="itemForm" action="${pageContext.request.contextPath }/videos/editVideoSubmit" method="get">
    <input type="hidden" name="id" value="${video.id }"/>
    修改视频信息:
    <table width="100%" border=1>
        <tr>
            <td>视频标题</td>
            <td>
                <textarea rows="3" cols="180" name="title">${video.title }</textarea>
            </td>
        </tr>
        <tr>
            <td>视频简介</td>
            <td>
                <textarea rows="3" cols="180" name="des">${video.des }</textarea>
            </td>
        </tr>
        <tr>
            <td>视频头图</td>
            <td>
                <textarea rows="3" cols="180" name="header_img">${video.header_img }</textarea>
            </td>
        </tr>
        <tr>
            <td>视频占位图</td>
            <td>
                <textarea rows="3" cols="180" name="img">${video.img }</textarea>
            </td>
        </tr>
        <tr>
            <td>视频连接</td>
            <td>
                <textarea rows="3" cols="180" name="video_url">${video.video_url }</textarea>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="提交"/>
            </td>
        </tr>
    </table>

</form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_23195583/article/details/54343107