I. Concepts
Internationalization: the language on the interface can be changed based on the region where the user is located.
II. The example below will teach you how to use Struts to internationalize.
1. Compile resource files
This resource file is the character displayed on the interface. The resource file contains English and Chinese resource files, so that we can convert the resource file to convert Chinese and English on the interface.
Download the resource file here>
2. compile related interfaces.
Index. jsp
<% @ Page language = "java" import = "java. util. * "pageEncoding =" GB18030 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> My JSP 'index. jsp 'starting page
Login
Chinese and English
Http://blog.csdn.net/gwblue/article/details/login.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
Insert title here<%--
--%>
Login_success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
Insert title here
Login_error.jsp
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
Insert title here
3. compile related classes
ChangeLanguageAction. java
Package com. bjpowernode. struts; import java. util. locale; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. struts. action. action; import org. apache. struts. action. actionForm; import org. apache. struts. action. actionForward; import org. apache. struts. action. actionMapping;/*** manual language switching * @ author Administrator **/public class ChangeLanguageAction extends Action {@ Overridepublic ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, httpServletResponse response) throws Exception {String lang = request. getParameter ("lang"); Locale locale = Locale. getDefault (); if ("zh ". equals (lang) {locale = new Locale ("zh", "CN");} else if ("en ". equals (lang) {locale = new Locale ("en", "US");} // set Locale to session // request. getSession (). setAttribute (Globals. LOCALE_KEY, locale); this. setLocale (request, locale); return mapping. findForward ("index ");}}
LoginAction. java
Package com. bjpowernode. struts; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. struts. action. action; import org. apache. struts. action. actionForm; import org. apache. struts. action. actionForward; import org. apache. struts. action. actionMapping; import org. apache. struts. action. actionMessage; import org. apache. struts. action. actionMessages; /* ** log on to the Action * to obtain form data, call the business logic, and return the redirection information. ** @ author Administrator **/public class LoginAction extends Action {@ Overridepublic ActionForward execute (ActionMapping mapping, actionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {LoginActionForm laf = (LoginActionForm) form; String username = laf. getUsername (); String password = laf. getPassword (); UserManager userManager = new UserManager (); ActionMessages messages = new ActionMessages (); try {userManager. login (username, password); // create the international message text ActionMessage message = new ActionMessage ("login. success ", username); messages. add ("login_success_1", message); // transmits the international message this. saveMessages (request, messages); return mapping. findForward ("success");} catch (UserNotFoundException e) {e. printStackTrace (); // creates the internationalized message text ActionMessage error = new ActionMessage ("login. user. not. found ", username); messages. add ("error_1", error); // transfer the international message this. saveErrors (request, messages);} catch (PasswordErrorException e) {e. printStackTrace (); // creates the internationalized message text ActionMessage error = new ActionMessage ("login. password. error "); messages. add ("error_2", error); // transfer the international message this. saveErrors (request, messages);} return mapping. findForward ("error ");}}
LoginActionForm. java
Package com. bjpowernode. struts; import org. apache. struts. action. actionForm;/*** log on to ActionForm, responsible for form data collection * The form attributes must be consistent with the get and set attributes in ActionForm * @ author Administrator **/@ SuppressWarnings ("serial ") public class LoginActionForm extends ActionForm {private String username; private String password; 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 ;}}
PasswordErrorException. java
package com.bjpowernode.struts;public class PasswordErrorException extends RuntimeException {public PasswordErrorException() {// TODO Auto-generated constructor stub}public PasswordErrorException(String message) {super(message);// TODO Auto-generated constructor stub}public PasswordErrorException(Throwable cause) {super(cause);// TODO Auto-generated constructor stub}public PasswordErrorException(String message, Throwable cause) {super(message, cause);// TODO Auto-generated constructor stub}}
UserManager. java
package com.bjpowernode.struts;public class UserManager {public void login(String username, String password) {if (!"admin".equals(username)) {throw new UserNotFoundException();}if (!"admin".equals(password)) {throw new PasswordErrorException();}}}
UserNotFoundException. java
package com.bjpowernode.struts;public class UserNotFoundException extends RuntimeException {public UserNotFoundException() {// TODO Auto-generated constructor stub}public UserNotFoundException(String message) {super(message);// TODO Auto-generated constructor stub}public UserNotFoundException(Throwable cause) {super(cause);// TODO Auto-generated constructor stub}public UserNotFoundException(String message, Throwable cause) {super(message, cause);// TODO Auto-generated constructor stub}}
4, the configuration of the struts-config.xml.
3. Feelings. Many places are used for internationalization. Large websites have international files.