At present, many projects in the company need internationalization, and now the way is to replace the display by JS method, the process of switching language caused the Chinese and English language flicker appear, resulting in a very bad user experience. In addition, the use of JS replacement method configuration of the JSON format of the configuration file, not easy to read and write, resulting in the development of different module colleagues write files when the conflict, and JS loading for other JS loading caused a variety of unexpected bugs, the developers caused great pain, Therefore, based on Struts2 and SPRINGMVC two kinds of methods, this paper briefly expounds the realization of the internationalization of Struts2 and SPRINGMVC two kinds.
Article structure
First, the use of Struts2 way to achieve internationalization
1. Principle of realization
1-1. Source code Comment
1-2. Flowchart
2, Environment building W
2-1. Create a new Dynamic Web project
2-2. Add struts2 configuration file under current src directory
2-3. Join the jar package that the struts2 runs on
2-4. Add internationalized Resource Files
3. Create action class and configure Struts2.xml
3-1. Action Class
3-2, Struts2.xml
3-3. JSP page
4. Realize the effect
Second, the use of SPRINGMVC way to achieve internationalization
1. Principle of realization
2. Engineering structure
3. Detailed catalogue
4. Realize the Display
Summarize
First, the use of Struts2 way to achieve internationalization
1. Principle of realization
Struts 2 internationalization is based on the internationalization of Java, as well as by providing different country/locale message resources, and then loading the specified locale corresponding resource file via ResourceBundle. The message corresponding to the specified key in the resource file is obtained---the whole process is the same as the internationalization of the Java program, but the STRUTS2 framework further encapsulates the internationalization of the Java program, thus simplifying the internationalization of the application.
The following is a detailed description of the internationalization implementation principle through source notes and flowcharts.
1-1 Source Comments
1-2 flowchart
Based on the local object obtained in the request parameter, the local object is determined to be empty, if not null, the local string is saved to the session, and if it is empty, the local saved in session is obtained from the session, if the session Gets the local object from the request message header, and finally saves the local object to the value stack's map stack.
2, the construction of STRUTS2 environment
2-1 Create a new Dynamic Web project
Add the core filter of Struts2 to the Web. XML, see Struts-2.3.15.3\apps\struts2-blank\web-inf\web.xml replication in the STRUTS2 official example.
2-2 Add struts2 configuration file under current src directory
Can copy Struts-2.3.15.3\apps\struts2-blank\web-inf\src\java\struts.xml
2-3 adding the jar packages that the struts2 runs on
2-4 Adding an internationalized resource file
The file contents are as follows
At this point, we are ready to finish the work.
3. Create the Action class and configure the Struts.xml
4. Realize the effect
Click to go to login screen
Switch to Engilish
Second, the use of SPRINGMVC way to achieve internationalization
1. Principle of realization
By default, SPRINGMVC determines the localization type of the client based on the Accept-language parameter.
When the request is accepted, SPRINGMVC looks for a localized parser (Localresolver) in the context, which is found and used to obtain the localized type information for the request.
SPRINGMVC also allows you to assemble an interceptor that dynamically changes the localization type, so that you can control the localization type of a single request by specifying a request parameter.
2. Engineering structure
SRC contains the Chinese and English, the default language of the resource files, the effect will be based on the JSP page key value from the international luxury resource file to find the matching value.
Springdispatcherservlet-servlet.xml writes the relevant configuration of the SPRINGMVC.
3. Detailed catalogue
SRC under Properties files are stored in fields that require internationalization. exist in the form of Key-value.
The name must comply with the Basename_ national language code. Properties format. such as I18n_zh_cn.properties.
Lib stores the jar packages that are dependent on SPRINGMVC.
WebContent the next Pages folder name must be Springdispatcherservlet-servlet.xml
The configured <property name= "prefix" value= "/web-inf/pages/" ></property> consistent.
The following two JSP pages login is responsible for showing the change of the state of English and Chinese, success is responsible for the display of the switch page in English state maintenance. INDEX.JSP is responsible for the initial language selection.
Springdispatcherservlet-servlet.xml is a configuration file for Springmvc. The specific content is as follows:
4. Realize the Display
The first time you open the index.jsp page, the program will match the corresponding Internationalized resource file according to the current browser's local object, click the link of switching language, will re-match the internationalized resource file according to the link parameter, has achieved the effect of language switching.
index.jsp
Click on the language to enter login.jsp, the first choice is Chinese
Click the 中文版 link again
Can be determined that the Chinese and English switch successfully, and no flicker, to give users an excellent experience.
Next, verify that the Chinese and English states are maintained on different pages. English Status Click Success Page link
Switch to Chinese status click Success Page
Summarize
At present, Struts2 and SPRINGMVC are more popular view layer framework, mastering the characteristics and implementation of the two, can greatly simplify our development, improve our development efficiency, and bring excellent user experience for the project. Struts2 simple and easy to use tags, springmvc powerful annotations, mapping features, greatly liberated the engineers to build the front-end page of productivity, so that the project's view layer structure is clear, control layer and persistence layer code is more concise and convenient.
Struts2 and Springmvc each have an advantage, Struts2 's entrance is filter, and Springmvc's entry is SERVLET,STRUTS2 based on global configuration, and Springmvc is based on method. So SPRINGMVC than Struts2 processing faster, and SPRINGMVC label is to simplify the development, so I recommend the use of SPRINGMVC implementation method.
Printing http://www.biyinjishi.com/
Printing Knowledge http://www.biyinjishi.com/kdocs/
Printing Tips http://www.biyinjishi.com/kdocslist/1/
Print Template http://www.biyinjishi.com/kdocslist/2/
Printing Process http://www.biyinjishi.com/kdocslist/3/
Printed material http://www.biyinjishi.com/kdocslist/4/
Print Creative http://www.biyinjishi.com/kdocslist/5/
Printing Platform http://www.biyinjishi.com/kdocslist/6/
Ordinary business card http://www.biyinjishi.com/products/a10-b1010/d100001/
mid-end business card http://www.biyinjishi.com/products/a10-b1010/d100002/
High-end business card http://www.biyinjishi.com/products/a10-b1010/d100003/
Creative Business Card http://www.biyinjishi.com/products/a10-b1010/d100004/
Membership card http://www.biyinjishi.com/products/a10-b1020/
PVC Membership Card http://www.biyinjishi.com/products/a10-b1020/d100006/
Magnetic stripe membership card http://www.biyinjishi.com/products/a10-b1020/d100007/
Chip membership card http://www.biyinjishi.com/products/a10-b1020/d100008/
Invitation Letter Http://www.biyinjishi.com/products/a10-b1030/
Invitation http://www.biyinjishi.com/products/a10-b1030/
Card http://www.biyinjishi.com/products/a10-b1035/
label http://www.biyinjishi.com/products/a10-b1035/
promotional material http://www.biyinjishi.com/products/a20/
single page http://www.biyinjishi.com/products/a20-b2010/
Folding http://www.biyinjishi.com/products/a20-b2010/
Leaflet Page http://www.biyinjishi.com/products/a20-b2010/d100015/
Propaganda Hinge http://www.biyinjishi.com/products/a20-b2010/d100016/
Poster http://www.biyinjishi.com/products/a20-b2030/d100020/
Display Board http://www.biyinjishi.com/products/a20-b2030/d100021/
Exhibition stand http://www.biyinjishi.com/products/a20-b2035/d100021
X-Show Stand http://www.biyinjishi.com/products/a20-b2035/d100023/
Easy Paula http://www.biyinjishi.com/products/a20-b2035/d100024/
Tote Bag http://www.biyinjishi.com/products/a20-b2050/d100024
Paper tote Bag http://www.biyinjishi.com/products/a20-b2050/d100026/
Fabric Tote Bag http://www.biyinjishi.com/products/a20-b2050/d100027/
Two ways of implementing internationalization in Java