painting中category的值为null
package com.imooc.mgallery.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Patch;
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 com.imooc.mgallery.entity.Painting;
import com.imooc.mgallery.service.PaintingService;
import com.imooc.mgallery.utils.PageModel;
import javafx.scene.control.Alert;
/**
* Servlet implementation class ManageController
*/
@WebServlet("/manage")
public class ManageController extends HttpServlet {
private PaintingService paintingService =new PaintingService();
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ManageController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//修改默认的post方式提交的字符集乱码问题 因为post调用了doget方法
request.setCharacterEncoding("utf-8");
//解决输出代码中文乱码问题
response.setContentType("text/html;charset=utf-8");
//如果实现一个servlet处理不同的功能呢 增删改查操作呢
//每次访问servlet的时候携带一个method参数来判定你是什么请求
String method =request.getParameter("method");
//代表这还是list展示列表的请求
//首先判断进来的list的参数是否为空
if (method==null) {
System.out.println("输入method参数" );
}
if (method.equals("list")) {
this.list(request,response);
}
if (method.equals("delete")) {
this.delete(request, response);
}
if (method.equals("update")) {
}if (method.equals("show_create")) {
this.showCreatePage(request,response);
}if (method.equals("create")) {
this.create(request,response);
}
}
private void create(HttpServletRequest request, HttpServletResponse response) {
//因为这个新增是关于文件的提交 所以不能用传统的方式 request.getparam("param");
//因为默认entype是url字符串格式存储 而文件是二进制数据所以不能这样获取
//String pname =request.getParameter("pname"); 这里不行了 因为涉及文件的读取
//System.out.println(pname);
//1初始化fileupload组件
//FileItemFactory用于将前端表单的数据转换为一个个fileItem对象
//servletFileUpload 则是 为FileUpload提供Java web的http请求解析
FileItemFactory factory =new DiskFileItemFactory(); //这是一个工厂 这里每一个涉及文件提交的表单数据都是一个FileItem文件对象
ServletFileUpload sf =new ServletFileUpload(factory);
//2遍历所有FileItem文件
try {
List<FileItem> list =sf.parseRequest(request);
Painting painting =new Painting();
for (FileItem fileItem : list) {
if (fileItem.isFormField()) {
System.out.println(fileItem.getFieldName()+":"+fileItem.getString());
switch (fileItem.getFieldName()) {
case "pname":
painting.setPname(fileItem.getString("utf-8"));
break;
case "category":
painting.setCategroy(Integer.parseInt(fileItem.getString("utf-8")));
break;
case "price":
painting.setPrice(Integer.parseInt(fileItem.getString("utf-8")));
break;
case "description":
painting.setDescription(fileItem.getString("utf-8"));
break;
default:
break;
}
}else {
//3文件保存到服务器目录 确定这里是文件
String path =request.getServletContext().getRealPath("/upload");
System.out.println(path);
//避免文件名 写一个方法些不同的名字 使用随机数 或者当前时间作为文件名UUID
//扩展名也不是唯一的 通过获取原始文件的名字截取最后三位
//fileItem.getName()为了获取原始文件名字 只为了获取后三位文件扩展名
String suffix=fileItem.getName().substring(fileItem.getName().lastIndexOf("."));
String fileName=UUID.randomUUID().toString()+suffix;
fileItem.write(new File(path,fileName));
painting.setPriview("/upload"+fileName);
}
}
paintingService.create(painting);//新增功能处理
response.sendRedirect("/manage?method=list");//返回列表页展示 因为关联比较紧密 可以用重定向
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //对原有表单数据对象进行转换
}
private void showCreatePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("WEB-INF/jsp/create.jsp").forward(request, response);
}
private void delete(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
}
/**
* 这里就是分页的代码
* @param request
* @param response
* @throws IOException
* @throws ServletException
*/
private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String page =request.getParameter("p");
String rows=request.getParameter("r");
if (page == null) {
page="1";
}
if (rows==null) {
rows="6";
}
PageModel pageModel= paintingService.getRawData(Integer.parseInt(page), Integer.parseInt(rows));
request.setAttribute("pageModel", pageModel);
List<Painting> list=pageModel.getPageData();
request.getRequestDispatcher("WEB-INF/jsp/list.jsp").forward(request, response);
}
}
我获取的方式全部是一样的 打印出来categroy里面也有值 已提交就报错 说categroy为null 空指针异常
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.parseInt(Integer.java:615)
at com.imooc.mgallery.utils.XmlDataSource.reload(XmlDataSource.java:77)
at com.imooc.mgallery.utils.XmlDataSource.append(XmlDataSource.java:129)
at com.imooc.mgallery.dao.PaintingDAO.create(PaintingDAO.java:32)
at com.imooc.mgallery.service.PaintingService.create(PaintingService.java:27)
at com.imooc.mgallery.controller.ManageController.create(ManageController.java:127)
at com.imooc.mgallery.controller.ManageController.doGet(ManageController.java:73)
at com.imooc.mgallery.controller.ManageController.doPost(ManageController.java:177)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星