Java EE + Spring + Hibernate can't save UTF-8 characters into MySQL Database -
this question might marked duplicate, however, none of stackoverflow answers didn't me. i'm making website using java ee, spring , hibernate, other technologies, however, these relevant ones. website web store, , admin should able add product, it's name, price, manufacturer, etc... working, on website working except saving utf-8 characters in database. problem not in database, it's set utf-8, , if go phpmyadmin, can change characters in desired format, , display properly.
here's 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 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" %>
try these steps
put utf-8 encoding filter in web.xml (the filter must 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>
if use maven, add below pom.xml
<properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> ... </properties>
if use tomcat, add
uriencoding="utf-8"
server.xml this<connector port="8080" protocol="http/1.1" connectiontimeout="20000" redirectport="8443" uriencoding="utf-8" />
Comments
Post a Comment