有2个问题请老师解答一下
问题1:前台页面图片显示不出来
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
<link rel="stylesheet" type="text/css" href="/style/common.css" />
<link rel="stylesheet" type="text/css" href="/style/index.css" />
</head>
<body>
<div class="header">
<div class="logo f1">
<img src="/image/logo.png">
</div>
<div class="auth fr">
<ul>
<li><a href="/toLogin.do">登录</a></li>
<li><a href="#">注册</a></li>
</ul>
</div>
</div>
<div class="content">
<div class="banner">
<img class="banner-img" src="/image/welcome.png" width="732px" height="372" alt="图片描述">
</div>
<div class="img-content">
<ul>
<c:forEach items="${pageInfo.list}" var="list">
<li>
<img class="img-li-fix" src="${list.smallImg}">
<div class="info">
<h3 class="img_title">${list.name}</h3>
<p>
${list.description}
</p>
<div class="btn">
<a href="/detail.do?id=${list.id}" class="edit">详情</a>
</div>
</div>
</li>
</c:forEach>
</ul>
</div>
<div class="page-nav">
<ul>
<li><a href="">首页</a></li>
<li><a href="">上一页</a></li>
<li><a href=""><span class="first-page">1</span></a></li>
<li><a href="">2</a></li>
<li><a href="">3</a></li>
<li><a href="">4</a></li>
<li><a href="">5</a></li>
<li>...</li>
<li><a href="">98</a></li>
<li><a href="">99</a></li>
<li><a href="">下一页</a></li>
<li><a href="">尾页</a></li>
</ul>
</div>
</div>
<div class="footer">
<p><span>M-GALLARY</span><img draggable="false" class="moco-emoji" alt="©" src="https://www.imooc.com/static/moco/v1.0/images/face/36x36/a9.png" data-src="©" style="cursor: pointer;">2019 POWERED BY vincent.INC</p>
</div>
</body>
</html>
package com.vincent.canvas.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vincent.canvas.biz.AccountBiz;
import com.vincent.canvas.biz.CanvasBiz;
import com.vincent.canvas.biz.CategoryBiz;
import com.vincent.canvas.biz.impl.AccountBizImpl;
import com.vincent.canvas.biz.impl.CanvasBizImpl;
import com.vincent.canvas.biz.impl.CategoryBizImpl;
import com.vincent.canvas.entity.Account;
import com.vincent.canvas.entity.Canvas;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class DefaultController {
private AccountBiz accountBiz= new AccountBizImpl();
private CategoryBiz categoryBiz=new CategoryBizImpl();
private CanvasBiz canvasBiz=new CanvasBizImpl();
// /toLogin.do
public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/pages/admin/login.jsp").forward(request,response);
}
// /login.do
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("account");
String password = request.getParameter("password");
Account account = accountBiz.login(name,password);
if(account==null)
response.sendRedirect("toLogin.do");
else{
request.getSession().setAttribute("ACCOUNT",account);
response.sendRedirect("/admin/Canvas/list.do");
}
}
// /quit.do
public void quit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getSession().setAttribute("ACCOUNT",null);
response.sendRedirect("toLogin.do");
}
// /index.do
public void index(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//分页
String pageNum = request.getParameter("pageNum");
if(pageNum==null){
pageNum="1";
}
PageHelper.startPage(Integer.parseInt(pageNum),6);
List<Canvas> list = canvasBiz.getShow();
PageInfo pageInfo = PageInfo.of(list);
request.setAttribute("pageInfo",pageInfo);
request.getRequestDispatcher("/WEB-INF/pages/index.jsp").forward(request,response);
}
// /detail.do
public void detail(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
Canvas canvas = canvasBiz.get(id);
request.setAttribute("canvas",canvas);
request.getRequestDispatcher("/WEB-INF/pages/detail.jsp").forward(request,response);
}
}
问题2:油画列表点击编辑修改保存之后,没有反应
package com.vincent.canvas.controller;
import com.vincent.canvas.biz.CanvasBiz;
import com.vincent.canvas.biz.CategoryBiz;
import com.vincent.canvas.biz.impl.CanvasBizImpl;
import com.vincent.canvas.biz.impl.CategoryBizImpl;
import com.vincent.canvas.entity.Canvas;
import com.vincent.canvas.entity.Category;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class CanvasController {
private CanvasBiz canvasBiz = new CanvasBizImpl();
private CategoryBiz categoryBiz=new CategoryBizImpl();
// /admin/canvas/list.do
public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Canvas> list=canvasBiz.getAll();
request.setAttribute("list",list);
request.getRequestDispatcher("/WEB-INF/pages/admin/canvas_list.jsp").forward(request,response);
}
// /admin/canvas/toAdd.do 打开添加界面
public void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
List<Category> list = categoryBiz.getRoot();
request.setAttribute("list",list);
request.getRequestDispatcher("/WEB-INF/pages/admin/canvas_add.jsp").forward(request,response);
}
// /admin/canvas/add.do 添加完成提交
public void add(HttpServletRequest request,HttpServletResponse response) throws IOException, FileUploadException {
Canvas canvas=builder(request);
canvasBiz.add(canvas);
response.sendRedirect("list.do");
}
// /admin/canvas/toUpdate.do
public void toUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
int id=Integer.parseInt(request.getParameter("id"));
Canvas canvas=canvasBiz.get(id);
request.setAttribute("canvas",canvas);
List<Category> list= categoryBiz.getRoot();
request.setAttribute("list",list);
request.getRequestDispatcher("/WEB-INF/pages/admin/canvas_update.jsp").forward(request,response);
}
// /admin/canvas/update.do
public void update(HttpServletRequest request,HttpServletResponse response) throws IOException, FileUploadException {
Canvas canvas=builder(request);
canvasBiz.edit(canvas);
response.sendRedirect("list.do");
}
// /admin/Canvas/remove.do
public void remove(HttpServletRequest request,HttpServletResponse response) throws IOException {
int id=Integer.parseInt(request.getParameter("id"));
canvasBiz.remove(id);
response.sendRedirect("list.do");
}
private Canvas builder(HttpServletRequest request) throws FileUploadException, UnsupportedEncodingException {
Canvas canvas = new Canvas();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
List<FileItem> list = upload.parseRequest(request);
for(FileItem item:list){
//判断是表单项还是文件上传项
if(item.isFormField()){
//表单项
if(item.getFieldName().equals("id")){
canvas.setId(Integer.parseInt(item.getString("UTF-8")));
}
if(item.getFieldName().equals("categoryId")){
canvas.setCategoryId(Integer.parseInt(item.getString("UTF-8")));
}
if(item.getFieldName().equals("name")){
canvas.setName(item.getString("UTF-8"));
}
if (item.getFieldName().equals("price")){
canvas.setPrice(Integer.parseInt(item.getString("UTF-8")));
}
if(item.getFieldName().equals("description")){
canvas.setDescription(item.getString("UTF-8"));
}
if(item.getFieldName().equals("smallImg")&&canvas.getSmallImg()==null){
canvas.setSmallImg(item.getString("UTF-8"));
}else{
//文件上传项
if(item.getFieldName().equals("smallImg")){
if(item.getSize()<=100) continue;
String rootPath=request.getServletContext().getRealPath("/");
String path=item.getName();
String type=".jpg";
if(path.indexOf(".")!=-1){
type=path.substring(path.lastIndexOf("."));
}
path="/download/images/"+System.currentTimeMillis()+type;
try {
item.write(new File(rootPath+path));
canvas.setSmallImg(path);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
return canvas;
}
}
package com.vincent.canvas.dao;
import com.vincent.canvas.entity.Canvas;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface CanvasDao {
//添加商品
@Insert("insert into canvas (name,creator,categoryId,price,smallImg,createTime,description) values (#{name},#{creator},#{categoryId},#{price},#{smallImg},#{createTime},#{description})")
@Options(useGeneratedKeys = true,keyProperty = "id")
void insert(Canvas canvas);
//编辑商品,createTime=#{createTime}
@Update("update canvas set name=#{name},categoryId=#{categoryId},price=#{price},smallImg=#{smallImg},description=#{description} where id=#{id}")
void update(Canvas canvas);
//删除商品
@Delete("delete from canvas where id=#{id}")
void delete(int id);
//查询单个商品
@Select("select c.*,ca.name cname from canvas c inner join category ca on c.categoryId=ca.id where c.id=#{id}")
@Results(id = "all",value = {
@Result(column = "id",property = "id",id = true),
@Result(column = "categoryId",property = "categoryId"),
@Result(column = "name",property = "name"),
@Result(column = "creator",property = "creator"),
@Result(column = "price",property = "price"),
@Result(column = "smallImg",property = "smallImg"),
@Result(column = "createTime",property = "createTime"),
@Result(column = "updateTime",property = "updateTime"),
@Result(column = "description",property = "description"),
@Result(column = "details",property = "details"),
@Result(column = "cname",property = "category.name")
})
Canvas select(int id);
//查询全部商品
@Select("select c.*,ca.name cname from canvas c inner join category ca on c.categoryId=ca.id order by c.id desc")
@ResultMap("all")
List<Canvas> selectAll();
//前台页面展示
@Select("select c.*,ca.name cname from canvas c inner join category ca on c.categoryId=ca.id")
@ResultMap("all")
List<Canvas> selectShow();
}
package com.vincent.canvas.biz.impl;
import com.vincent.canvas.biz.CanvasBiz;
import com.vincent.canvas.dao.CanvasDao;
import com.vincent.canvas.entity.Canvas;
import com.vincent.canvas.global.DaoFactory;
import java.util.Date;
import java.util.List;
public class CanvasBizImpl implements CanvasBiz {
private CanvasDao canvasDao = DaoFactory.getInstance().getDao(CanvasDao.class);
public void add(Canvas canvas) {
Date now = new Date();
canvas.setCreateTime(now);
canvas.setUpdateTime(now);
canvasDao.insert(canvas);
}
public void edit(Canvas canvas) {
canvasDao.update(canvas);
}
public void remove(int id) {
canvasDao.delete(id);
}
public Canvas get(int id) {
return canvasDao.select(id);
}
public List<Canvas> getAll() {
return canvasDao.selectAll();
}
public List<Canvas> getShow() {
return canvasDao.selectShow();
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<meta charset="UTF-8">
<title>油画列表</title>
<link rel="stylesheet" href="/css/index.css">
<link rel="stylesheet" href="/css/bootstrap.min.css">
</head>
<body>
<header>
<div class="container">
<nav>
<a href="/admin/Category/list.do" >分类一</a>
</nav>
<nav>
<a href="/admin/Category/list.do" >分类</a>
</nav>
<nav>
<a href="/login.do">登录</a>
<a href="#" onclick="alert('功能暂未开放');">注册</a>
</nav>
</div>
</header>
<section class="banner">
<div class="container">
<div>
<h1>油画</h1>
<p>油画列表</p>
</div>
</div>
</section>
<section class="main">
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>名称</th>
<th>分类</th>
<th>价格</th>
<th>创建时间</th>
<th>最后修改时间</th>
<th>描述</th>
<th>编辑</th>
<th>删除</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="canvas">
<tr>
<td>${canvas.name}</td>
<td>${canvas.category.name}</td>
<td>${canvas.price}</td>
<td>${canvas.createTime}</td>
<td>${canvas.updateTime}</td>
<%-- <td><fmt:formatNumber type="currency" pattern="#,#00.00#" value="${canvas.price}"/></td>--%>
<%-- <td><fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss" value="${canvas.createTime}"/></td>--%>
<%-- <td><fmt:formatDate pattern="yyyy-MM-dd HH:mm:ss" value="${canvas.updateTime}"/></td>--%>
<td>${canvas.description}</td>
<td><a href="/admin/Canvas/toUpdate.do?id=${canvas.id}">编辑</a></td>
<td><a href="/admin/Canvas/remove.do?id=${canvas.id}">删除</a>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</section>
<section class="page">
<div class="container">
<div id="fatie">
<a href="/admin/Canvas/toAdd.do"><button>新建</button></a>
</div>
</div>
</section>
<footer>
copy@vincent
</footer>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<meta charset="UTF-8">
<title>编辑</title>
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="#">
油画管理系统
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, admin!</h1>
<p>请小心的编辑油画记录</p>
</div>
<div class="page-header">
<h3><small>编辑</small></h3>
</div>
<form class="form-horizontal" action="/admin/Canvas/update.do" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="${canvas.id}">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">名称 :</label>
<div class="col-sm-8">
<input name="name" class="form-control" id="name" value="${canvas.name}">
<input type="hidden" name="id" class="form-control" id="id" value="001">
</div>
</div>
<div class="form-group">
<label for="categoryId" class="col-sm-2 control-label">分类 :</label>
<select id="categoryId" name="categoryId" class="col-sm-2 form-control" style="width: auto">
<c:forEach items="${list}" var="cat">
<option id="${cat.id}" value="${cat.id}">${cat.name}</option>
</c:forEach>
</select>
</div>
<div class="form-group">
<label for="price" class="col-sm-2 control-label">价格 :</label>
<div class="col-sm-8">
<input name="price" type="number" class="form-control" id="price" value="${canvas.price}">
</div>
</div>
<div class="form-group">
<label for="smallImg" class="col-sm-2 control-label">图片 :</label>
<div class="col-sm-8">
<input id="smallImg" name="smallImg" class="file-loading"
type="file" multiple accept=".jpg,.jpeg,.png" data-min-file-count="1"
data-show-preview="true" value="${canvas.smallImg}">
</div>
</div>
<div class="form-group">
<label for="description" class="col-sm-2 control-label">描述 :</label>
<div class="col-sm-8">
<input name="description" type="text" class="form-control" id="description" value="${canvas.description}">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
<footer class="text-center" >
copy@vincent
</footer>
</body>
</html>
正在回答 回答被采纳积分+1
package com.vincent.canvas.biz;
import com.vincent.canvas.entity.Canvas;
import java.util.List;
public interface CanvasBiz {
//添加商品
void add(Canvas canvas);
//编辑商品
void edit(Canvas canvas);
//删除商品
void remove(int id);
//获取单个商品
Canvas get(int id);
//获取全部商品
List<Canvas> getAll();
//前台展示
List<Canvas> getShow();
}
package com.vincent.canvas.biz.impl;
import com.vincent.canvas.biz.CanvasBiz;
import com.vincent.canvas.dao.CanvasDao;
import com.vincent.canvas.entity.Canvas;
import com.vincent.canvas.global.DaoFactory;
import java.util.Date;
import java.util.List;
public class CanvasBizImpl implements CanvasBiz {
private CanvasDao canvasDao = DaoFactory.getInstance().getDao(CanvasDao.class);
public void add(Canvas canvas) {
Date now = new Date();
canvas.setCreateTime(now);
canvas.setUpdateTime(now);
canvasDao.insert(canvas);
}
public void edit(Canvas canvas) {
canvasDao.update(canvas);
}
public void remove(int id) {
canvasDao.delete(id);
}
public Canvas get(int id) {
return canvasDao.select(id);
}
public List<Canvas> getAll() {
return canvasDao.selectAll();
}
public List<Canvas> getShow() {
return canvasDao.selectShow();
}
}
package com.vincent.canvas.dao;
import com.vincent.canvas.entity.Canvas;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface CanvasDao {
//添加商品
@Insert("insert into canvas (name,creator,categoryId,price,smallImg,createTime,description) values (#{name},#{creator},#{categoryId},#{price},#{smallImg},#{createTime},#{description})")
@Options(useGeneratedKeys = true,keyProperty = "id")
void insert(Canvas canvas);
//编辑商品,createTime=#{createTime}
@Update("update canvas set name=#{name},categoryId=#{categoryId},price=#{price},smallImg=#{smallImg},description=#{description} where id=#{id}")
void update(Canvas canvas);
//删除商品
@Delete("delete from canvas where id=#{id}")
void delete(int id);
//查询单个商品
@Select("select c.*,ca.name cname from canvas c inner join category ca on c.categoryId=ca.id where c.id=#{id}")
@Results(id = "all",value = {
@Result(column = "id",property = "id",id = true),
@Result(column = "categoryId",property = "categoryId"),
@Result(column = "name",property = "name"),
@Result(column = "creator",property = "creator"),
@Result(column = "price",property = "price"),
@Result(column = "smallImg",property = "smallImg"),
@Result(column = "createTime",property = "createTime"),
@Result(column = "updateTime",property = "updateTime"),
@Result(column = "description",property = "description"),
@Result(column = "details",property = "details"),
@Result(column = "cname",property = "category.name")
})
Canvas select(int id);
//查询全部商品
@Select("select c.*,ca.name cname from canvas c inner join category ca on c.categoryId=ca.id order by c.id desc")
@ResultMap("all")
List<Canvas> selectAll();
//前台页面展示
@Select("select c.*,ca.name cname from canvas c inner join category ca on c.categoryId=ca.id")
@ResultMap("all")
List<Canvas> selectShow();
}
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星