Java EE + Spring + Hibernate can't save UTF-8

2019-07-31 02:36发布

问题:

This question might be marked as duplicate, however, NONE of the StackOverflow answers didn't help me. I'm making a website using Java EE, Spring and Hibernate, with some other technologies, however, these are the only relevant ones. The website is a web store, and the admin should be able to add a product, it's name, price, manufacturer, etc... This is all working, everything on the website is working except saving the UTF-8 characters in the Database. The problem is not in the Database, it's all set to utf-8, and if I go to phpmyadmin, I can also change the characters in the desired format, and it will display properly.

Here's the relevant product adding code...

Admin Product

@Controller
@RequestMapping("/admin")
public class AdminProduct {

    private Path path;

    @Autowired
    ProductService productService;



    @RequestMapping("/product/addProduct")
    public String addProduct(Model model) {

        Product product = new Product();
        product.setProductCategory("toys");
        product.setProductStatus("available");

        model.addAttribute("product", product);

        return "addProduct";


    }



    @RequestMapping(value = "/product/addProduct", method = RequestMethod.POST)
    public String addProductPost(@Valid @ModelAttribute("product") Product product, BindingResult result, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        if(result.hasErrors()) {
            return "addProduct";
        }

        productService.addProduct(product);

        MultipartFile productImage = product.getProductImage();
        String rootDirectory = request.getSession().getServletContext().getRealPath("/");

        path = Paths.get(rootDirectory + "\\WEB-INF\\resources\\images\\" + product.getProductId() + ".png");

        if(productImage != null && !productImage.isEmpty()) {
            try {
                productImage.transferTo(new File(path.toString()));
            } catch(Exception e) {
                throw new RuntimeException("Product image saving failed!");
            }
        }

        return "redirect:/admin/productInventory";
    }

ProductDao Implementation

 @Repository
    @Transactional
    public class ProductDaoImpl implements ProductDao {

        @Autowired
        private SessionFactory sessionFactory;

    public Product getProductById(int id) {
        Session session = sessionFactory.getCurrentSession();
        Product product = (Product) session.get(Product.class, id);
        session.flush();

        return product;
    }

    public List<Product> getProductList() {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Product");
        List<Product> productList = query.list();
        session.flush();
        return productList;
    }

        public void  addProduct(Product product) {
            Session session = sessionFactory.getCurrentSession();
           session.saveOrUpdate(product);
            session.flush();

        }

        public void  editProduct(Product product) {
            Session session = sessionFactory.getCurrentSession();
            session.saveOrUpdate(product);
            session.flush();

        }

        public void  deleteProduct(Product product) {
            Session session = sessionFactory.getCurrentSession();
            session.delete(product);
            session.flush();

    }

Product Model

@Entity
public class Product implements Serializable {


    private static final long serialVersionUID = -509245862136222627L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int productId;

    @NotEmpty (message = "Ime proizvoda ne moze biti prazno!")
    private String productName;

    private String productCategory;
    private String productDescription;

    @Min(value=0, message = "Cena proizvoda ne moze biti manja od nule!")
    private double productPrice;

    private String productCondition;
    private String productStatus;

    @Min(value=0, message = "Broj na lageru ne moze biti manji od nule!")
    private int unitInStock;

    private String productManufacturer;

    @Transient
    private MultipartFile productImage;

    @OneToMany(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JsonIgnore
    private List<CartItem> cartItemList;

    public int getProductId() {
        return productId;
    }

    public void setProductId(int productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductCategory() {
        return productCategory;
    }

    public void setProductCategory(String productCategory) {
        this.productCategory = productCategory;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(double productPrice) {
        this.productPrice = productPrice;
    }

    public String getProductCondition() {
        return productCondition;
    }

    public void setProductCondition(String productCondition) {
        this.productCondition = productCondition;
    }

    public String getProductStatus() {
        return productStatus;
    }

    public void setProductStatus(String productStatus) {
        this.productStatus = productStatus;
    }

    public int getUnitInStock() {
        return unitInStock;
    }

    public void setUnitInStock(int unitInStock) {
        this.unitInStock = unitInStock;
    }

    public String getProductManufacturer() {
        return productManufacturer;
    }

    public void setProductManufacturer(String productManufacturer) {
        this.productManufacturer = productManufacturer;
    }

    public MultipartFile getProductImage() {
        return productImage;
    }

    public void setProductImage(MultipartFile productImage) {
        this.productImage = productImage;
    }

    public List<CartItem> getCartItemList() {
        return cartItemList;
    }

    public void setCartItemList(List<CartItem> cartItemList) {
        this.cartItemList = cartItemList;
    }
}

And finally the addProduct jsp page

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <%@page pageEncoding="UTF-8"%>
    <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@include file="/WEB-INF/views/template/header.jsp" %>

    <div class="container-wrapper">
        <div class="container">

            <div class="page-header">
                <h1>Dodaj Proizvod</h1>
        <p class="lead">Popunite informacije dole kako biste dodali proizvod:</p>

            </div>

            <form:form action="${pageContext.request.contextPath}/admin/product/addProduct" method="post" commandName="product" enctype="multipart/form-data" acceptCharset="UTF-8">

                <div class="form-group">
                    <label for="name">Ime</label> <form:errors path="productName" cssStyle="color:red"/>
                    <form:input path="productName" id="name" class="form-Control" acceptCharset="UTF-8"/>
                </div>
     <%--Category --%>
            <div class="form-group">
            <label for="category">Kategorija</label>
            <label class="checkbox-inline"><form:radiobutton  acceptCharset="UTF-8" path="productCategory" id="category" value="Školski/Kancelarijski pribor"/> Školski/Kancelarijski pribor</label>
            <label class="checkbox-inline"><form:radiobutton  acceptCharset="UTF-8" path="productCategory" id="category" value="Pokloni"/> Pokloni</label>
            <label class="checkbox-inline"><form:radiobutton  acceptCharset="UTF-8" path="productCategory" id="category" value="Igračke"/> Igračke</label>
            </div>

     <%--Description --%>
            <div class="form-group">
                <label for="description">Opis</label>
                <form:textarea path="productDescription" id="description" class="form-Control" acceptCharset="UTF-8"/>
            </div>
    <%--Price --%>
            <div class="form-group">
                <label for="price">Cena</label> <form:errors path="productPrice" cssStyle="color:red"/>
                <form:input path="productPrice" id="price" class="form-Control"/>
            </div>

    <%--Status --%>
            <div class="form-group">
                <label for="status">Status</label>
                <label class="checkbox-inline"><form:radiobutton path="productStatus" id="status" value="Dostupno"/> Dostupno</label>
                <label class="checkbox-inline"><form:radiobutton path="productStatus" id="status" value="Nedostupno"/> Nedostupno</label>

            </div>

                <%--Manufacturer --%>
            <div class="form-group">
                <label for="productManufacturer">Proizvođač</label>
                <form:input path="productManufacturer" id="manufacturer" class="form-Control" acceptCharset="UTF-8"/>
            </div>

            <div class="form-group">
                <label class="control-label" for="productImage">Odaberi sliku</label>
                <form:input id="productImage" path="productImage" type="file" class="form:input-large"/>
            </div>

            <br><br>

            <input type="submit" value="Dodaj" class="btn btn-default">
            <a href="<c:url value="/admin/productInventory"/>" class="btn btn-default">Odustani</a>
        </form:form>

    <%@include file="/WEB-INF/views/template/footer.jsp" %>

回答1:

Try these steps

  1. Put the UTF-8 encoding filter in your web.xml (the filter must be first filter)

     <filter>
         <filter-name>encoding-filter</filter-name>
         <filter-class>
             org.springframework.web.filter.CharacterEncodingFilter
         </filter-class>
         <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
         </init-param>
         <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
         </init-param>
      </filter>
    
     <filter-mapping>
          <filter-name>encoding-filter</filter-name>
          <url-pattern>/*</url-pattern>
     </filter-mapping>
    
  2. If you use Maven, add below to your pom.xml

      <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          ...
       </properties>
    
  3. If you use tomcat, add URIEncoding="UTF-8" to your server.xml like this

    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"  redirectPort="8443"  URIEncoding="UTF-8" />
    


回答2:

You can set the following in hibernate xml file when specifying the data driver:

jdbc:mysql://localhost/mydatabase?useUnicode=true&characterEncoding=UTF-8


回答3:

Perhaps a spelling error?

  • HTML has accept-charset attribute
  • JavaScript has acceptCharset property

Reference: https://msdn.microsoft.com/en-us/library/ms533061