Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

完成优秀论文上传、下载模块

  • Loading branch information...
commit 7a5f1c46720a07e1362d2bfd204099c359982457 1 parent 29ece81
LICH authored
View
51 WebContent/WEB-INF/views/admin/paperManage.jsp
@@ -0,0 +1,51 @@
+<%@ page contentType="text/html;charset=UTF-8"%>
+<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
+<c:set var="ctx" value="${pageContext.request.contextPath}" />
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<title>管理员管理</title>
+</head>
+<body>
+ <div class="row">
+ <div class="span6 offset3">
+ <h3 style="text-align: center;">管理员管理</h3>
+
+ <table class="table table-bordered">
+ <thead>
+ <tr>
+ <th colspan="2" style="vertical-align: middle;">文件名</th>
+ <th style="vertical-align: middle;">操作</th>
+ </tr>
+ </thead>
+ <tbody>
+ <c:forEach items="${papers}" var="paper" varStatus="st">
+ <tr>
+ <form:url value="${ctx}/file/paper/${paper.filename}" var="url" htmlEncoding="true" />
+ <td colspan="2"><a href="${ctx}/file/paper/${paper.filename}">${paper.description}</a></td>
+ <td><a href="${ctx}/admin/paperManage/del/${paper.id}" class="btn btn-mini">删除</a></td>
+ </tr>
+ </c:forEach>
+ <form action="${ctx}/admin/paperManage/add" method="post" enctype="multipart/form-data">
+ <tr>
+ <td><input type="text" class="input-medium" name="description" /></td>
+ <td><input type="file" class="input-medium" name="uploadFile" /></td>
+ <td><input type="submit" class="btn btn-primary btn-mini" value="添加" /></td>
+ </tr>
+ </form>
+ </tbody>
+ </table>
+ </div>
+ <!-- /span6 offset3 -->
+ </div>
+ <!-- /row -->
+
+ <!-- Javascript -->
+ <script type="text/javascript">
+ $(document).ready(function() {
+ $('#li-admin_material').addClass('active');
+ });
+ </script>
+</body>
+</html>
View
34 WebContent/WEB-INF/views/index.jsp
@@ -63,31 +63,10 @@
<div class="span8 offset2">
<h3>下载</h3>
<table class="table table-bordered" style="margin-top: 20px;">
- <thead>
- <tr>
- <th colspan="2">优秀论文</th>
- </tr>
- </thead>
<tbody>
- <tr>
- <td>人生的思考</td>
- <td><a>下载</a></td>
- </tr>
- <tr>
- <td>人生的思考</td>
- <td><a>下载</a></td>
- </tr>
- <tr>
- <td>人生的思考</td>
- <td><a>下载</a></td>
- </tr>
- <tr>
- <td>人生的思考</td>
- <td><a>下载</a></td>
- </tr>
<c:forEach items="${papers}" var="paper">
<tr>
- <td>${paper.description}</td>
+ <td width="80%">${paper.description}</td>
<td><a href="${ctx}/file/paper/${paper.filename}">下载</a></td>
</tr>
</c:forEach>
@@ -98,17 +77,6 @@
</div>
<!-- /row -->
- <div class="row">
- <div class="span8 offset2">
- <ul>
- <li><p>计算机学院计算机学院计算机学院计算机学院计算机学院计算机学院计算机学院</p>
- <p>aaaaaa</p></li>
- </ul>
- </div>
- <!-- /span12 -->
- </div>
- <!-- /row -->
-
<!-- Javascript -->
<script type="text/javascript">
$(document).ready(function() {
View
3  resource/applicationContext-mvc.xml
@@ -24,6 +24,9 @@
<property name="suffix" value=".jsp" />
</bean>
+ <!-- 支持上传文件 -->
+ <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
+
<!-- Shiro异常映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
View
24 src/im/lich/gdms/core/service/admin/PaperService.java
@@ -1,7 +1,10 @@
package im.lich.gdms.core.service.admin;
+import java.io.IOException;
import java.util.List;
+import org.springframework.web.multipart.MultipartFile;
+
import im.lich.gdms.base.service.BaseService;
import im.lich.gdms.core.model.admin.Paper;
@@ -11,4 +14,25 @@
* @return List<Paper> 优秀论文
*/
public List<Paper> getPapers();
+
+ /**
+ * 获取优秀论文(URL编码)
+ * @return List<Paper> 优秀论文
+ */
+ public List<Paper> getPapersURLEncoded();
+
+ /**
+ * 保存优秀论文
+ * @param descption 论文描述
+ * @param uploadFile 上传文件
+ * @return
+ */
+ public Paper savePaper(String description, MultipartFile uploadFile) throws IOException;
+
+ /**
+ * 删除优秀论文
+ * @param id ID
+ * @return Paper or null
+ */
+ public Paper delPaper(Long id);
}
View
51 src/im/lich/gdms/core/service/admin/impl/PaperServiceImpl.java
@@ -5,12 +5,19 @@
import im.lich.gdms.core.model.admin.Paper;
import im.lich.gdms.core.service.admin.PaperService;
+import java.io.File;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
import java.util.List;
import javax.annotation.Resource;
+import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.Assert;
+import org.springframework.web.multipart.MultipartFile;
import com.google.common.collect.Lists;
@@ -20,6 +27,7 @@
@Resource
private PaperDao paperDao;
+ @Override
public List<Paper> getPapers() {
//防止修改数据被持久化
List<Paper> _papers = paperDao.findAll();
@@ -28,4 +36,47 @@
return papers;
}
+
+ @Override
+ public List<Paper> getPapersURLEncoded() {
+ List<Paper> papers = getPapers();
+ for (Paper p : papers) {
+ String filename = p.getFilename();
+ try {
+ String filenameURLEncoded = URLEncoder.encode(filename, "UTF-8");
+ logger.debug("下载地址转码 {} - {}", filename, filenameURLEncoded);
+ p.setFilename(filenameURLEncoded);
+ } catch (UnsupportedEncodingException e) {
+ logger.warn(e.toString());
+ }
+ }
+ return papers;
+ }
+
+ @Override
+ @Transactional(readOnly = false)
+ public Paper savePaper(String description, MultipartFile uploadFile) throws IOException {
+ org.springframework.core.io.Resource res = applicationContext.getResource("/file/paper");
+ Assert.isTrue(res.exists());
+ File dir = res.getFile();
+ Assert.isTrue(dir.isDirectory());
+
+ String filename = uploadFile.getOriginalFilename();
+ File f = new File(dir, filename);
+ Assert.isTrue(f.createNewFile());
+ uploadFile.transferTo(f);
+
+ if (StringUtils.isEmpty(description))
+ description = filename;
+ Paper paper = new Paper(description, filename);
+ return paperDao.save(paper);
+ }
+
+ @Override
+ @Transactional(readOnly = false)
+ public Paper delPaper(Long id) {
+ Paper paper = paperDao.findOne(id);
+ paperDao.delete(paper);
+ return paper;
+ }
}
View
61 src/im/lich/gdms/core/web/admin/AdminPaperManageController.java
@@ -0,0 +1,61 @@
+package im.lich.gdms.core.web.admin;
+
+import im.lich.gdms.base.web.BaseController;
+import im.lich.gdms.core.model.admin.Paper;
+import im.lich.gdms.core.service.admin.PaperService;
+
+import java.io.IOException;
+import java.util.List;
+
+import javax.annotation.Resource;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.multipart.MultipartFile;
+
+@Controller
+@RequestMapping("/admin")
+public class AdminPaperManageController extends BaseController {
+
+ @Resource
+ private PaperService paperService;
+
+ @RequestMapping(value = { "/paperManage" })
+ public String showPaperManage(Model model) {
+ List<Paper> papers = paperService.getPapersURLEncoded();
+ model.addAttribute("papers", papers);
+
+ return "admin/paperManage";
+ }
+
+ @RequestMapping(value = { "/paperManage/add" }, method = RequestMethod.POST)
+ public String addOrUpdatePaperManage(String description, MultipartFile uploadFile, Model model) throws IOException {
+ logger.debug("获取新增的Paper: {}-{}", description, uploadFile.getOriginalFilename());
+
+ //保存
+ boolean success = false;
+ if (paperService.savePaper(description, uploadFile) != null) {
+ success = true;
+ }
+ model.addAttribute("success", success);
+
+ return "forward:/admin/paperManage";
+ }
+
+ @RequestMapping(value = { "/paperManage/del/{id}" })
+ public String deletePaperManage(@PathVariable("id") Long id, Model model) {
+ logger.debug("删除Paper: {}", id);
+
+ //保存
+ boolean success = false;
+ if (paperService.delPaper(id) != null) {
+ success = true;
+ }
+ model.addAttribute("success", success);
+
+ return "forward:/admin/paperManage";
+ }
+}
View
2  src/im/lich/gdms/core/web/root/IndexController.java
@@ -49,7 +49,7 @@ public String index(Model model) {
model.addAttribute("adminNewses", adminNewses);
logger.debug("adminNewses:{}", adminNewses.size());
//获取优秀论文列表
- List<Paper> papers = paperService.getPapers();
+ List<Paper> papers = paperService.getPapersURLEncoded();
model.addAttribute("papers", papers);
return "index";
}
Please sign in to comment.
Something went wrong with that request. Please try again.