SPRINGMVC Internationalization Issues

Source: Internet
Author: User

1. First add 3 properties files under Src file

 A, message.properties

1 message.username=username2 Message.password=password

 B, Message_en.properties

1 message.username=username2 Message.password=password

 C, message_zh.properties

1 message.username=\u7528\u6237\u540d2 message.password=\u5bc6\u7801

2. Configuring the Spring-mvc.xml File

1 <BeanID= "Messagesource"class= "Org.springframework.context.support.ResourceBundleMessageSource">2     <!--Specify the file base name -3      < Propertyname= "BaseName"value= "message"/>4      <!--when a resource file is not found, use this base name file -5      < Propertyname= "Usecodeasdefaultmessage"value= "true" />    6  </Bean>

This configures the property usercodeasdefaultmessage=true. In other language areas, the corresponding internationalization file is not found. such as the Japanese language area, Korean area. The Message.properties file is used by default. When false, the display has a problem.

3. Configure Controller

1 @Controller 2  Public class Usercontroller {  3  4     @RequestMapping ("/updateuser")  5      Public String Update () {  6  7         return "Success"; 8     }  9      }

4. JSP page

To make the JSP page use internationalized information. You need to add a FMT tag. The label is in the Jstl label table. How to add

1 <% @ taglib Prefix = " FMT " URI = " http://java.sun.com/jsp/jstl/fmt " %>

  index.jsp

1 <%@ Page Language="Java"Import="java.util.*"pageencoding="Utf-8"%>2 <%@ taglib Prefix="FMT"URI="http://java.sun.com/jsp/jstl/fmt" %>3 4 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">5 <HTML>6   <Head>7     <title>My JSP ' hello.jsp ' starting page</title>8   </Head>9   Ten   <Body> One      A     <Fmt:messageKey= "Message.username"/><inputtype= "text"name= "username" /><BR/> -     <Fmt:messageKey= "Message.password"/><inputtype= "text"name= "Password" /><BR/> -   </Body> the </HTML>

  hello.jsp

1 <%@ Page Language="Java"Import="java.util.*"pageencoding="Utf-8"%>2 <%@ taglib Prefix="FMT"URI="http://java.sun.com/jsp/jstl/fmt" %>3 4 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">5 <HTML>6   <Head>7     <title>My JSP ' hello.jsp ' starting page</title>8   </Head>9   Ten   <Body> One      A     <Fmt:messageKey= "Message.username"/><inputtype= "text"name= "username" /><BR/> -     <Fmt:messageKey= "Message.password"/><inputtype= "text"name= "Password" /><BR/> -   </Body> the </HTML>

We access the success.jsp page via URL.

Access Address: Http://localhost:8080/springmvc-1/updateUser

We visit index.jsp page http://localhost:8080/springmvc-1

As a result, this results

Why JSP pages are the same. Why is the result different?

The reason is that the index.jsp page can be accessed directly, while the success.jsp page under Web-inf, cannot be accessed directly, and needs to be forwarded through the servlet in a way that is sufficient. In Springmvc, the Dispatcherservlet front-end controller intercepts all requests for http://localhost:8080/springmvc-1/updateUser access. Make a SPRINGMVC to deal with. Resourcebundlemessagesource is configured in Spring-mvc.xml, so internationalized information can be used on success pages.

In summary: To use page-based internationalization information, you need to use a forwarding method to take effect.

  

SPRINGMVC Internationalization Issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.