SPRINGMVC problems encountered in the process of internationalization

Source: Internet
Author: User

Foreword: In the process of using SPRINGMVC to realize internationalization, it seems simple, actually still encountered some small problems, now I summarize the following problems.

Note: The editor used by the author is IntelliJ Ieda version 14

1. internationalized resource file path problem.

, the Internationalized resource drop location has a directory, and the Internationalized resource file is configured in the Springmvc.xml configuration file as follows.

Note: ① Note the basename, here is a directory: the previous message is the directory name , the latter message for the internationalization of the base file name , the specific internationalization of the file loading method please check the relevant information.

② if the internationalized resource file is placed directly under the resources directory, the value of basename is directly internationalized to the underlying file name, so that it is p:basename= "message".

③ if the internationalized resource file is in the package, the wording here is p:basename= "Com.xx.xx.message".

④ if the internationalization is unsuccessful, the possible reason is that the BaseName property is incorrect, you should check the location of the internationalized resource file placement, whether the directory is stored.

2. On the homepage internationalization unsuccessful question

When the Web. xml file is configured as follows, the internationalization of the home page is likely to appear??? Message.username??? The situation.

Note: Of course, the default servlet is configured in Springmvc.xml, otherwise there are 404 problems with direct access to the static resource file.

When it comes to internationalization of the home page, there may be??? Message.username??? (Note: The author uses the FMT label internationalization), the specific reason: through the above configuration, in the home page JSP, the request will not pass through the Dispatcherservlet, therefore cannot request the internationalization resource file, concrete solution such as (later I will propose another solution)

will be the original FMT label, Packing one layer can solve the above problems, note the basename, with the table of contents

Note: The author's internationalized resource file is configured as follows:

3. The above solutions, while addressing the internationalization of the home page, may then encounter the following problems:

① in the internationalization of the general home page will have a Language conversion button, in English and Chinese switch. because our homepage uses the static JSP page, and does not pass through the Dispatcherservlet forwarding, therefore when carries on the switch the time will appear how the homepage jumps to the homepage question .

② may also appear cannot change HTTP accept Header-use A different locale resolution strategy. Problem

Before solving the above two problems, first of all, the author's interceptor configuration is as follows:

Note here sessionlocalresolver, its bean ID value must be localeresolver, otherwise will be reported above ② problem, special attention. Therefore the solution for the ② problem is marked.

For the first page of the English-Chinese switching jump problem, the author uses the following "Double home" form.

(1) What is "Double home", (that is, there is a index.jsp in the WebApp directory (this is labeled a), and there is a index.jsp in the Web-inf directory).

(2) The specific implementation method, in a page jump, the index/index.jsp request, so that can be forwarded through the Dispatcherservlet, The exact code is as shown.

Note: Only this line of code exists in page A.

(3) in the controller, write a control of the index, the code is as follows:

Note: It is also necessary to configure the view parser, which is not given in this paper, and the reader can consult the relevant data for configuration.

Through the above, you can display the internationalization of the homepage, and will not appear??? Message.username??? The phenomenon. FMT labels do not have to be packaged , as follows.

(4) In the first page (index/index.jsp) to switch between English and Chinese, add the following two buttons.

Note: lang is the parameter name configured in the Interceptor; language is the controller

The specific code for the language controller is as follows:

Note: redirect is used here to redirect to the index controller, which resolves the presence of/language?lang=en_us in the browser address bar.

Note: Actually does not adopt "the Double homepage" The form also may realize above operation, concrete method:

① There is no index.jsp page under WebApp, the index.jsp page only exists under Web-inf.

② Modify the configuration of the Dispatcherservlet in Web. XML, the first index controller configuration is also configured accordingly,

Attention of the configuration, and then through the controller home forwarding, you can achieve the internationalization of the home page.

I am accustomed to the "double home" way to visit the home page, the author felt that the configuration in Web. XML is more elegant, (#^.^#)

by Shawn Chen 2018.1.27. PM.

SPRINGMVC problems encountered in the process of internationalization

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.