这个问题可能被标记为重复,但是,StackOverflow的任何答案都没有帮助我。 我正在使用Java EE,Spring和Hibernate创建一个网站,以及其他一些技术,但是,这些是唯一相关的。 该网站是一个网上商店,管理员应该能够添加产品,它的名称,价格,制造商等... 这一切都正常,除了在数据库中保存UTF-8字符外,网站上的所有内容都有效。 问题不在数据库中,它全部设置为utf-8,如果我转到phpmyadmin,我也可以更改所需格式的字符,它将正确显示。
以下是添加代码的相关产品......
管理产品
@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实施
@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();
}
产品型号
@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;
}
}
最后是addProduct jsp页面
<%@ 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" %>
答案 0 :(得分:3)
指定数据驱动程序时,可以在hibernate xml文件中设置以下内容:
jdbc:mysql://localhost/mydatabase?useUnicode=true&characterEncoding=UTF-8
答案 1 :(得分:2)
尝试以下步骤
将UTF-8编码过滤器放在web.xml中(过滤器必须是 第一个过滤器)
<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>
如果您使用Maven,请将以下内容添加到您的pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
...
</properties>
如果您使用tomcat,请将URIEncoding="UTF-8"
添加到您的server.xml,如下所示
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
答案 2 :(得分:0)
也许是拼写错误?
accept-charset
属性acceptCharset
属性