有几个关键性问题,请老师解答一下
package com.vincent.canvas.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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();
Canvas canvas = new Canvas();
// /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 = (List<Category>) 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= (List<Category>) 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;
}
}
<%@ 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="#">登录</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><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>
<%-- <div class="col-xs-12 col-md-9 text-right">--%>
<%-- <form action="/admin/Cake/list.do" method="post">--%>
<%-- 共${pageInfo.total}条 ${pageInfo.pageNum}/${pageInfo.pages}--%>
<%-- <div class="btn-group">--%>
<%-- <button type="submit" name="pageNum" value="${pageInfo.pageNum-1}" class="btn btn-default light">--%>
<%-- <i class="fa fa-chevron-left"></i>--%>
<%-- </button>--%>
<%-- <button type="submit" name="pageNum" value="${pageInfo.pageNum+1}" class="btn btn-default light">--%>
<%-- <i class="fa fa-chevron-right"></i>--%>
<%-- </button>--%>
<%-- </div>--%>
<%-- </form>--%>
<%-- </div>--%>
</section>
<footer>
copy@vincent
</footer>
</body>
</html>
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);
//编辑商品
@Update("update canvas set name=#{name},categoryId=#{categoryId},price=#{price},smallImg=#{smallImg},createTime=#{createTime},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;
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();
}
}
问题1:为什么我在数据库中插入的数据无法在canvas_list页面中显示出来
问题2:为什么canvas_list的JSP文件中我修改
<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>
他们的value值,会报错,而且页面也不显示了!
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星