gpt4 book ai didi

java - 可选 int 参数 '' 存在,但由于被声明为基本类型而无法转换为空值

转载 作者:行者123 更新时间:2023-12-02 09:57:52 25 4
gpt4 key购买 nike

BrandDao.java

package dao;

import java.util.List;
import model.pojo.Brands;
import model.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;


public class BrandDao {

public static List<Brands> list() {
List<Brands> brands = null;
try {
Session session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery("from Brands");
brands = query.list();

session.close();
}
catch (HibernateException ex) {
ex.printStackTrace();
}
return brands;
}

public Brands getBrandById(int brandId) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Brands brand = (Brands) session.get(Brands.class, brandId);
tx.commit();
session.close();
return brand;
}

public Brands addBrand(Brands brand) {
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(brand);
tx.commit();
session.close();
} catch (HibernateException ex) {
ex.printStackTrace();
}

return brand;
}
}

BrandsController.java

package controller;

import dao.BrandDao;
import java.io.IOException;
import java.util.List;
import model.pojo.Brands;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BrandsController {

ModelAndView mav = new ModelAndView();

@RequestMapping(value="/admin/brands")
public ModelAndView index() {
List<Brands> brands = BrandDa.list();
mav.addObject("brands", brands);
mav.setViewName("brands-admin");
return mav;
}

@RequestMapping(value="/admin/brands/add", method = RequestMethod.POST)
public String addBrand(@RequestParam("photo") MultipartFile photo, @RequestParam("brandName") String name) throws IOException {

Brands brand = new Brands();
BrandDao brandDao = new BrandDa();
byte[] photoBytes = photo.getBytes();
brand.setBrandLogo(photoBytes);
brand.setBrandName(name);
brandDao.addBrand(brand);
return "redirect:/admin/brands";
}

@RequestMapping(value="/admin/brands/getBrand", method = RequestMethod.GET)
@ResponseBody
public Brands getBrand(int brandId) {
BrandDao brandDao = new BrandDa();
return brandDao.getBrandById(brandId);
}
}

品牌管理.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>House of Beauty</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/styles.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
<script>
$(document).ready(function() {
$('#getBrand').on('click', function(event) {
event.preventDefault();
var href = $(this).attr('href');

$.get(href, function(brand, status) {
$('#BrandForm #name').val(brand.getBrandName());
$('#BrandForm #photo').val(brand.getBrandLogo());
});
$('#BrandForm').modal('show');
});
});
</script>
</head>
<body>
<div class="modal fade" id="BrandForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title w-100" id="myModalLabel">Edit brand</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/admin/brands/update">
<div class="form-group">
<input type="text" name="brandName" id="name" placeholder="Name" cssClass="form-control"/>
</div>
<div class="form-group">
<input type="file" name="photo" id="photo"/>
</div>
<div class="row form-bottom">
<div class="col">
<input type="submit" value="Save" class="btn" id="btnAdd">
<input type="submit" value="Delete" class="btn" id="btnAdd">
</div>
</div>
<form>
</div>
</div>
</div>
</div>
<div class="modal fade" id="AddBrandForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title w-100" id="myModalLabel">Add brand</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<form method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/admin/brands/add">
<div class="form-group">
<input type="text" name="brandName" placeholder="Name" cssClass="form-control"/>
</div>
<div class="form-group">
<input type="file" name="photo"/>
</div>
<div class="row form-bottom">
<div class="col">
<input type="submit" value="Add" class="btn" id="btnAdd">
</div>
</div>
<form>
</div>
</div>
</div>
</div>
<header>
...
<div id="main_content">
<div class="container">
<div class="row header-content">
<div class="col title-page">
<h3>Brands</h3>
</div>
<div class="col">
<a href="#AddBrandForm" data-dismiss="modal" data-toggle="modal">Add brand</a>
</div>
</div>
<hr>
<div class="row">
<c:forEach items="${brands}" var="brand">
<div class="col">
<div class="brand-item">
<a href="${pageContext.request.contextPath}/admin/brands/getBrand?id=<c:out value="${brand.getBrandId()}"/>" id="getBrand">
<div class="image">
<img src="${pageContext.request.contextPath}/images?id=<c:out value="${brand.getBrandId()}"/>" />
</div>
<p><c:out value="${brand.getBrandName()}"/></p>
</a>
</div>
</div>
</c:forEach>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>

我正在尝试从模式中编辑品牌,就像添加它一样。我对 spring 和 hibernate 完全陌生,我不知道我的做法是否正确。通过单击应该打开模式的品牌链接,我收到此错误:

HTTP Status 500 - Internal Server Error
type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Optional int parameter 'brandId' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
root cause

java.lang.IllegalStateException: Optional int parameter 'brandId' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.

GlassFish Server Open Source Edition 4.1.1

请,如果有人可以帮助我如何获取品牌的值(value)并能够通过模式对其进行编辑,我将非常感谢您的帮助。提前致谢。

最佳答案

在您的 Controller 类中,“brandId”正在接收空值,而您已将其定义为不能为空的“int”类型(正如您的错误语句也提到的那样)。从“int”更改为“Integer”。

此外,不确定您在请求中从哪里获取此参数。我知道您还需要定义路径参数。 (这只是一个观察)

关于java - 可选 int 参数 '' 存在,但由于被声明为基本类型而无法转换为空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55861726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com