我第一次使用JSP,Servlets,Tomcat服务器,MySQL和Netbeans创建一个简单的网站(如果有帮助的话)。
感谢JPA EntityManager ,我可以将数据插入我的数据库,如下所示:
EntityManager entityManager = Persistence.createEntityManagerFactory("test").createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(usr);
entityManager.getTransaction().commit();
其中usr是由Netbeans生成的实体类用户。 一切都很完美,除了它不支持重音等字符。
假设我想插入“cliché”这个词:
数据库字段为 utf8_unicode_ci 。如果我尝试从MySQL提示符中进行简单的INSERT,例如
INSERT INTO table (name) VALUES("ééé");
它工作得很好。
正如我在某些网站上所读到的,我使用以下行修改了 persistence.xml :
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull"/>
但结果是一样的。
编辑:以下是用户类
的属性@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findByIdUsers", query = "SELECT u FROM Users u WHERE u.idUsers = :idUsers"),
@NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
@NamedQuery(name = "Users.findByPasswd", query = "SELECT u FROM Users u WHERE u.passwd = :passwd"),
@NamedQuery(name = "Users.findByFirstName", query = "SELECT u FROM Users u WHERE u.firstName = :firstName"),
@NamedQuery(name = "Users.findByLastName", query = "SELECT u FROM Users u WHERE u.lastName = :lastName"),
@NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
@NamedQuery(name = "Users.findByEmailAndPwd", query = "SELECT u FROM Users u WHERE u.email = :email AND u.passwd = :passwd")})
public class Users implements Serializable {
@Basic(optional = false)
@Column(name = "address")
private String address;
@Basic(optional = false)
@Column(name = "city")
private String city;
@Basic(optional = false)
@Column(name = "postal")
private String postal;
@Basic(optional = false)
@Column(name = "phone")
private String phone;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idUsers")
private Integer idUsers;
@Basic(optional = false)
@Column(name = "username")
private String username;
@Basic(optional = false)
@Column(name = "passwd")
private String passwd;
@Basic(optional = false)
@Column(name = "firstName")
private String firstName;
@Basic(optional = false)
@Column(name = "lastName")
private String lastName;
@Basic(optional = false)
@Column(name = "email")
private String email;
当我在网上看到时,我还尝试通过添加以下行来修改我的 catalina.bat ,但它没有改变任何内容。
设置JAVA_OPTS =%JAVA_OPTS%-Djavax.servlet.request.encoding = UTF-8 -Dfile.encoding = UTF-8
非常感谢任何帮助。
答案 0 :(得分:0)
尝试在persistence.xml中添加此配置
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.charSet">UTF-8</property>
答案 1 :(得分:0)
您可以将unicode编码过滤器添加到web.xml:
<filter>
<filter-name>CharsetFilter</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>
如果你使用Spring,你可以使用'org.springframework.web.filter.CharacterEncodingFilter'类对所有请求进行编码,否则你应该使用其他编码类。
答案 2 :(得分:0)
由于VGR的答案,我能够解决我的问题:HttpServletRequest UTF-8 Encoding
非常感谢你的帮助。