Hướng dẫn khắc phục lỗi font chữ trong java spring framework

Ở bài này,mình sẽ hướng dẫn các bạn cách khắc phục lỗi font chữ trong java spring.

Đầu tiên chúng ta vào phần demo chạy web đã nhé :

– Đây là trang đăng ký tài khoản,(giao diện khi chạy lên )

 

Giao diện khi chạy trang web

Khi nhập thông tin vào form đăng ký,chúng ta nhấn nút đăng ký để chuyển sang trang hiển thị thông tin người dùng đã nhập đã đăng ký tài khoản.

thong_tin_dang_ky

Như các bạn thấy,ở đây đối với username và password là số chúng ta ko nhập có dấu tiếng việt nên hiển thị bình thường.Nhưng đối với fullname ,khi nhập vào dấu có tiếng việt thì sẽ bị lỗi font chứ ngay.

faststoneeditor1

Vậy làm sao để khắc phục lỗi font chữ khi chúng ta nhập có dấu tiếng việt trong java spring.Và đây là câu trả lời :

Các bạn chỉ cần thêm đoạn code này vào trong file web.xml (ko quan trọng vị trí nhé).

	<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>
    
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
        </jsp-property-group>
    </jsp-config>

Ok! Bay giờ thì sẽ nhập thông tin vào xem lại kết quả nhé các bạn. ket_qua Như các thấy rằng chúng ta đã hiển thị được tiếng việt như mong muốn.

Dưới đây là source code để các bạn tham khảo.Chúc các bạn thực hiện thành công,hãy comment dưới bài viết để được mình hỗ trợ nếu gặp khó khăn nhé!

1.RegController.java

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import entities.Reg;

@Controller
@RequestMapping(value="reg")
public class RegController {
	
	@RequestMapping(method=RequestMethod.GET)
	public String index(){
		return "reg";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String index(@ModelAttribute Reg objReg, ModelMap modelMap){
		System.out.println("Username : "+objReg.getUsername());
		System.out.println("Password : "+objReg.getPassword());
		System.out.println("Fullname : "+objReg.getFullname());
		modelMap.addAttribute("objReg", objReg);
		return "reginfor";
	}
	
}

2.Reg.java

package entities;

public class Reg {
	private int id;
	private String username;
	private String password;
	private String fullname;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getFullname() {
		return fullname;
	}
	public void setFullname(String fullname) {
		this.fullname = fullname;
	}
	
	
}

3.reg.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Đăng ký thông tin tài khoản</title>
</head>
<body style="margin:0 auto; width:1024px">
	<h1>Đăng ký thông tin tài khoản</h1>
	<form action="${pageContext.request.contextPath}/reg" method="post">
		Username: <input name="username" value="" type="text" /><br /><br />
		Password: <input name="password" value="" type="password" /><br /><br />
		Fullname : <input name="fullname" value="" type="text" /><br /><br />
		<input name="submit" value="Đăng ký" type="submit" />
	</form>
</body>
</html>

4.reginfor.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Thông tin đăng ký</title>
</head>
<body>
	<div>
		<h1>Thông tin đăng ký</h1>
		<p>Username : ${objReg.username } </p>
		<p>Password : ${objReg.password } </p>
		<p>Fullname : ${objReg.fullname } </p>
	</div>
</body>
</html>

5.dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">
	<context:component-scan base-package="controller" />
	<mvc:annotation-driven/>
	<bean id="jspviewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

6.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>demo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    
  </welcome-file-list>
  
   <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  	
  	<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>
    
    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
        </jsp-property-group>
    </jsp-config>
    
</web-app>

 

x