自學SpringBoot的過程中,發現開源的Ruoyi比較不錯,而且作者公開全部的源碼,再次謝謝作者
順便幫這個業界良心做個推廣 https://www.ruoyi.vip/ ,有興趣學習和提高的網友們可以去學習下。
先總結,代碼在下面
(1)要保存的這個表,只有一個字段是用來存儲圖片路徑的字段;其他字段都是正常字段,和上傳無關;
(2)基本思路是先上傳,獲得上傳的文件名及路徑后,再保存相關信息入數據庫;
遇到的問題有
(1)不需要修改form的 enctype屬性,我的代碼是沒有的
enctype=”multipart/form-data”
(2)用Ruoyi開源的自動生成的工具,生成的代碼,但是因為這個僅僅是工具,需要改造成要符合上傳圖片預覽的情況
所以要改原來的代碼;
我參考了Ruoyi自己帶的jasny.html文件 ,而Ruoyi參考的是 https://github.com/jasny/bootstrap 上傳組件
(3)修改后台接收部分要修改
自動生成的是
public AjaxResult addSave(AppGoods appGoods)
保留這種方式,會報錯
Failed to convert property value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.String' for property 'goodsImg'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.String' for property 'goodsImg': no matching editors or conversion strategy found
修改成 public AjaxResult addSave(@RequestParam(value="goodsImg",required=false) MultipartFile file,AppGoods appGoods)
還是會報錯
Failed to convert property value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.String' for property 'goodsImg'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'java.lang.String' for property 'goodsImg': no matching editors or conversion strategy found
萬般無奈,修改成
public AjaxResult addSave(@RequestParam(value="goodsImg",required=false) MultipartFile file, HttpServletRequest request_in)
重新按照字段進行賦值就不報錯了
感覺一個一個字段賦值,有些蠢,不知道有沒有更好的解決方法
代碼如下
另外,ajax提交時,這個也是不能變成 contentType: "application/json"
只有 contentType: false 時,才是能夠成功的,
主要是我對JS底層還是不夠充分了解啊。
新增的畫面
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
<th:block th:include="include :: header('新增/goods')" />
<th:block th:include="include :: datetimepicker-css" />
<th:block th:include="include :: jasny-bootstrap-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
<form class="form-horizontal m" id="form-goods-add" >
<div class="form-group">
<label class="col-sm-3 control-label">商品名:</label>
<div class="col-sm-8">
<input id="goodsName" name="goodsName" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">進貨價:</label>
<div class="col-sm-8">
<input id="purchasePrice" name="purchasePrice" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">進貨日期:</label>
<div class="col-sm-8">
<div class="input-group date">
<input id="purchaseDate" name="purchaseDate" class="form-control" placeholder="yyyy-MM-dd" type="text">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">圖片路徑:</label>
<div class="col-sm-8">
<!-- <textarea name="goodsImg" class="form-control"></textarea>-->
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 400px; height: 200px;"></div>
<div >
<span class="btn btn-white btn-file"><span class="fileinput-new">選擇圖片</span><span class="fileinput-exists">更改</span><input id="goodsImg" name="goodsImg" type="file"></span>
<a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
</div>
</div>
</div>
</div>
</form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: jasny-bootstrap-js" />
<script th:inline="javascript">
var prefix = ctx + "app/goods"
$("#form-goods-add").validate({
focusCleanup: true
});
function submitHandler() {
var formData = new FormData();
if ($('#goodsImg')[0].files[0] == null) {
$.modal.alertWarning("請先選擇文件路徑");
return false;
}
formData.append('goodsName', $("#goodsName")[0].value);
formData.append('purchasePrice', $("#purchasePrice")[0].value);
formData.append('purchaseDate', $("#purchaseDate")[0].value);
formData.append('goodsImg', $('#goodsImg')[0].files[0]);
$.ajax({
url: prefix + "/add",
type: 'post',
cache: false,
data: formData,
processData: false,
contentType: false,
dataType: "json",
success: function(result) {
$.operate.successCallback(result);
}
});
}
$("input[name='purchaseDate']").datetimepicker({
format: "yyyy-mm-dd",
minView: "month",
autoclose: true
});
</script>
</body>
</html>
后台接收數據的Controller
package com.ruoyi.app.controller;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.app.domain.AppGoods;
import com.ruoyi.app.service.IAppGoodsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
/**
* /goodsController
*
* @author ruoyi
* @date 2021-12-28
*/
@Controller
@RequestMapping("/app/goods")
public class AppGoodsController extends BaseController
{
private String prefix = "app/goods";
@Autowired
private IAppGoodsService appGoodsService;
@RequiresPermissions("app:goods:view")
@GetMapping()
public String goods()
{
return prefix + "/goods";
}
/**
* 查詢/goods列表
*/
@RequiresPermissions("app:goods:list")
@PostMapping("/list")
@ResponseBody
public TableDataInfo list(AppGoods appGoods)
{
startPage();
List<AppGoods> list = appGoodsService.selectAppGoodsList(appGoods);
return getDataTable(list);
}
/**
* 導出/goods列表
*/
@RequiresPermissions("app:goods:export")
@Log(title = "/goods", businessType = BusinessType.EXPORT)
@PostMapping("/export")
@ResponseBody
public AjaxResult export(AppGoods appGoods)
{
List<AppGoods> list = appGoodsService.selectAppGoodsList(appGoods);
ExcelUtil<AppGoods> util = new ExcelUtil<AppGoods>(AppGoods.class);
return util.exportExcel(list, "/goods數據");
}
/**
* 新增/goods
*/
@GetMapping("/add")
public String add()
{
return prefix + "/add";
}
/**
* 新增保存/goods
*/
@RequiresPermissions("app:goods:add")
@Log(title = "/goods", businessType = BusinessType.INSERT)
@PostMapping("/add")
@ResponseBody
public AjaxResult addSave(@RequestParam(value="goodsImg",required=false) MultipartFile file)
{
AppGoods appGoods =new AppGoods();
try {
if (file != null) {
// 上傳文件路徑
String filePath = RuoYiConfig.getUploadPath();
// 上傳並返回新文件名稱
String fileName = null;
fileName = FileUploadUtils.upload(filePath, file);
appGoods.setGoodsName(getRequest().getParameter("goodsName"));
appGoods.setPurchaseDate(DateUtils.parseDate(getRequest().getParameter("purchaseDate"),new String[]{"yyyy-MM-dd"}));
appGoods.setPurchasePrice(org.apache.commons.lang3.math.NumberUtils.toScaledBigDecimal(getRequest().getParameter("purchasePrice")));
appGoods.setGoodsImg(fileName);
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return toAjax(appGoodsService.insertAppGoods(appGoods));
}
/**
* 修改/goods
*/
@RequiresPermissions("app:goods:edit")
@GetMapping("/edit/{id}")
public String edit(@PathVariable("id") Long id, ModelMap mmap)
{
AppGoods appGoods = appGoodsService.selectAppGoodsById(id);
mmap.put("appGoods", appGoods);
return prefix + "/edit";
}
/**
* 修改保存/goods
*/
@RequiresPermissions("app:goods:edit")
@Log(title = "/goods", businessType = BusinessType.UPDATE)
@PostMapping("/edit")
@ResponseBody
//public AjaxResult editSave(AppGoods appGoods)
public AjaxResult editSave(@RequestParam(value="goodsImg",required=false) MultipartFile file)
{
AppGoods appGoods =new AppGoods();
try {
if (file != null) {
// 上傳文件路徑
String filePath = RuoYiConfig.getUploadPath();
// 上傳並返回新文件名稱
String fileName = null;
fileName = FileUploadUtils.upload(filePath, file);
appGoods.setId(new Long(getRequest().getParameter("id")));
appGoods.setGoodsName(getRequest().getParameter("goodsName"));
appGoods.setPurchaseDate(DateUtils.parseDate(getRequest().getParameter("purchaseDate"),new String[]{"yyyy-MM-dd"}));
appGoods.setPurchasePrice(org.apache.commons.lang3.math.NumberUtils.toScaledBigDecimal(getRequest().getParameter("purchasePrice")));
appGoods.setGoodsImg(fileName);
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return toAjax(appGoodsService.updateAppGoods(appGoods));
}
/**
* 刪除/goods
*/
@RequiresPermissions("app:goods:remove")
@Log(title = "/goods", businessType = BusinessType.DELETE)
@PostMapping( "/remove")
@ResponseBody
public AjaxResult remove(String ids)
{
return toAjax(appGoodsService.deleteAppGoodsByIds(ids));
}
}
修改頁面代碼
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
<th:block th:include="include :: header('修改商品')" />
<th:block th:include="include :: datetimepicker-css" />
<th:block th:include="include :: jasny-bootstrap-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
<form class="form-horizontal m" id="form-goods-edit" th:object="${appGoods}">
<input name="id" th:field="*{id}" type="hidden">
<div class="form-group">
<label class="col-sm-3 control-label">商品名:</label>
<div class="col-sm-8">
<input name="goodsName" id="goodsName" th:field="*{goodsName}" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">進貨價:</label>
<div class="col-sm-8">
<input name="purchasePrice" id="purchasePrice" th:field="*{purchasePrice}" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">進貨日期:</label>
<div class="col-sm-8">
<div class="input-group date">
<input id="purchaseDate" name="purchaseDate" th:value="${#dates.format(appGoods.purchaseDate, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">圖片</label>
<div class="col-sm-8">
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 400px; height: 200px;">
<!-- <img th:src="@{/img/profile.jpg}">-->
<img th:src="*{goodsImg}"/>
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"></div>
<div>
<span class="btn btn-white btn-file"><span class="fileinput-new">選擇圖片</span><span class="fileinput-exists">更改</span><input id="goodsImg" name="goodsImg" type="file"></span>
<a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
</div>
</div>
</div>
</div>
</form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: jasny-bootstrap-js" />
<script th:inline="javascript">
var prefix = ctx + "app/goods";
$("#form-goods-edit").validate({
focusCleanup: true
});
// function submitHandler() {
// if ($.validate.form()) {
// $.operate.save(prefix + "/edit", $('#form-goods-edit').serialize());
// }
// }
function submitHandler() {
//debugger;
var formData = new FormData();
if ($('#goodsImg')[0].files[0] == null) {
$.modal.alertWarning("請先選擇文件路徑");
return false;
}
formData.append('id', $("#id")[0].value);
formData.append('goodsName', $("#goodsName")[0].value);
//alert($("#goodsName")[0].value);
formData.append('purchasePrice', $("#purchasePrice")[0].value);
formData.append('purchaseDate', $("#purchaseDate")[0].value);
formData.append('goodsImg', $('#goodsImg')[0].files[0]);
$.ajax({
url: prefix + "/edit",
type: 'post',
cache: false,
data: formData,
processData: false,
contentType: false,
//contentType: "application/json", //--錯誤的配置
dataType: "json",
success: function(result) {
$.operate.successCallback(result);
}
});
}
$("input[name='purchaseDate']").datetimepicker({
format: "yyyy-mm-dd",
minView: "month",
autoclose: true
});
</script>
</body>
</html>
列表頁面代碼
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<th:block th:include="include :: header('商品列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
<div class="row">
<div class="col-sm-12 search-collapse">
<form id="formId">
<div class="select-list">
<ul>
<li>
<label>商品名:</label>
<input type="text" name="goodsName"/>
</li>
<li>
<label>進貨價:</label>
<input type="text" name="purchasePrice"/>
</li>
<li>
<label>進貨日期:</label>
<input type="text" class="time-input" placeholder="請選擇進貨日期" name="purchaseDate"/>
</li>
<li>
<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i> 搜索</a>
<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i> 重置</a>
</li>
</ul>
</div>
</form>
</div>
<div class="btn-group-sm" id="toolbar" role="group">
<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="app:goods:add">
<i class="fa fa-plus"></i> 添加
</a>
<a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="app:goods:edit">
<i class="fa fa-edit"></i> 修改
</a>
<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="app:goods:remove">
<i class="fa fa-remove"></i> 刪除
</a>
<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="app:goods:export">
<i class="fa fa-download"></i> 導出
</a>
</div>
<div class="col-sm-12 select-table table-striped">
<table id="bootstrap-table"></table>
</div>
</div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
var editFlag = [[${@permission.hasPermi('app:goods:edit')}]];
var removeFlag = [[${@permission.hasPermi('app:goods:remove')}]];
var prefix = ctx + "app/goods";
$(function() {
var options = {
url: prefix + "/list",
createUrl: prefix + "/add",
updateUrl: prefix + "/edit/{id}",
removeUrl: prefix + "/remove",
exportUrl: prefix + "/export",
modalName: "商品",
columns: [{
checkbox: true
},
{
field: 'id',
title: '',
visible: false
},
{
field: 'goodsName',
title: '商品名'
},
{
field: 'purchasePrice',
title: '進貨價'
},
{
field: 'purchaseDate',
title: '進貨日期'
},
{
field:'goodsImg',
title: '圖片',
formatter: function(value, row, index) {
// 圖片預覽(注意:如存儲在本地直接獲取數據庫路徑,如有配置context-path需要使用ctx+路徑)
// 如:/profile/upload/2019/08/08/3b7a839aced67397bac694d77611ce72.png
return $.table.imageView(row.goodsImg);
}
},
{
title: '操作',
align: 'center',
formatter: function(value, row, index) {
var actions = [];
actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>編輯</a> ');
actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>刪除</a>');
return actions.join('');
}
}]
};
$.table.init(options);
});
</script>
</body>
</html>
很久沒有寫JAVA代碼了,記錄一下~
2022年1月5日重新修改了Controller代碼
修改了列表及修改代碼
再次感謝若依的作者,真的不錯。TEXT類型的字段,能夠單獨生成富文本模式,且支持上傳
