Struts 2 internationalization - Chinese English conversion

Keywords: Programming Struts xml Java JSP

First, create two files under src to provide the resource files required by the program

 

 

(careless programmers should pay attention to the File name, which is in the form of key and value.) the specific code is as follows:

 

 


 

 

 

Next, write the code in index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib  prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Switch between Chinese and English</title>
  </head>
  
  <body>
  
   <s:i18n name="message">  
  	<s:text name="check"></s:text>:
  	<a href="login.action?request_locale=zh_CN"><s:text name="chinese"></s:text></a>  
	<a href="login.action?request_locale=en_US"><s:text name="english"></s:text></a>
   <form action="<%=basePath%>t1" method="post">
             <table>
               <tr>
                 <td><s:text name="login.username"/></td>
                 <td><input type="text" name="user.userName"/></td>
               </tr>
               <tr>
                 <td><s:text name="login.password"/></td>
                 <td><input type="text" name="user.password"/></td>
               </tr>
               <tr>
                <td colspan="2"><input type="submit" value="<s:text name="login.btn"/>"/></td>
              </tr>              
             </table>                             
    </form>
   </s:i18n>
  </body>
</html>

Where the login of a tag is the action name attribute of struts.xml.

Secondly, the controller layer encapsulates the indexAction classes, but "execute" and "success" are variable factors. Remember to configure the corresponding property name in struts.xml, otherwise the corresponding action cannot be found.

package com.hnpi.action;

import com.opensymphony.xwork2.ActionSupport;

public class indexAction extends ActionSupport{
	private String name;
	private String pwd;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	public String execute(){
		return "success";
	}
}

In creating an action, there is only one execute method

package com.hnpi.action;

public class LanguageAction {
	
	public String execute(){
		
		return "success";
	}

}

Then configure the most important struts.xml page.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
   "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

 <package name="default" namespace="/" extends="struts-default">

		<action name="t1" class="com.hnpi.action.indexAction">
			<result name="success">/welcome.jsp</result>
		
		</action>
		<action name="login" class="com.hnpi.action.LanguageAction">
		<result name="success">/index.jsp</result>
		
		</action>

	</package>

</struts>

Be careful about the path of class and the attribute name of action name (the name of the result name attribute is the return content of the execute class in the encapsulation class)

Finally, configure the web.xml file. The content of the configuration file is a fixed collocation, which must be remembered.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Finally, my program running effect chart

Posted by riceje7 on Tue, 10 Dec 2019 21:41:20 -0800