3

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" %>
David Misic
  • 81
  • 3
  • 11
  • Have you tried debugging your controller to look at the values? Maybe the encoding is already wrong in the request from the form. – smsnheck Nov 08 '16 at 09:30
  • I tried it, the controller also prints out the wrong values... – David Misic Nov 08 '16 at 09:31
  • Also after the call of `request.setCharacterEncoding("UTF-8");` ? – smsnheck Nov 08 '16 at 09:33
  • Yes, even after the call, the result is still the same... – David Misic Nov 08 '16 at 09:38
  • delete <%@page pageEncoding="UTF-8"%> inside html tag. – Ye Win Nov 08 '16 at 09:44
  • if deleted is not ok, set utf-8 encode filter in your spring config – Ye Win Nov 08 '16 at 09:47
  • Deleted it, still the same :( And set the spring config to: *.jsp UTF-8 – David Misic Nov 08 '16 at 09:54
  • did you try to add a filter called before of all spring stack and check how are the UTF-8 characters encoded there? So you can understand if it's something related to spring (although this seems strange to me) or in the communication from the client side (browser) and server side; in this case you should opportunely configure tomcat as suggested in another comment – Angelo Immediata Nov 08 '16 at 11:17
  • What happens? Sometimes the 'garbage' is a good clue of what went wrong. See http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Nov 13 '16 at 04:33

3 Answers3

6

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

jdbc:mysql://localhost/mydatabase?useUnicode=true&characterEncoding=UTF-8
KayV
  • 12,987
  • 11
  • 98
  • 148
2

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" />
    
Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
0

Perhaps a spelling error?

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

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

Rick James
  • 135,179
  • 13
  • 127
  • 222