Dynamically switch struts2 Internationalization

Source: Internet
Author: User
Tags i18n

Ideas:

1. Configure the default language in struts. xml:

<Constant name = "sturts. locale" value = "zh_cn"/>

2. on the JSP page, set some links (<A> </a>). These links are action classes, and then set the corresponding parameters in the action through these links, and save it in the application object.

3. on the JSP page after the jump, you can obtain the value of the corresponding property of the Application object, later, you can set the hidden attribute <input type = "hidden" name = "request_locale" value = "<% = (string) application in the corresponding form. getattribute ("language")/>. In this way, the corresponding language will be displayed on the jump page in the future.

4. The specific code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
Pageencoding = "UTF-8" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> login </title>
</Head>
<Body>
<%
String lang = (string) application. getattribute ("Lang ");
If (lang = NULL)
{
Lang = "zh_cn ";
}
Session. setattribute ("language", Lang );
%>
<Table align = "center" border = "1">
<Caption> login </caption>
<S: Form method = "Post" name = "loginform" Action = "login" namespace = "/" Validate = "false">
<Tr>
<TD>
<S: Text name = "label_user"> </S: Text>
</TD>
<TD>
<S: textfield name = "username" size = "20"> </S: textfield>
</TD>
</Tr>

<Tr>
<TD>
<S: Text name = "label_password"> </S: Text>
</TD>
<TD>
<S: textfield name = "password" size = "20"> </S: textfield>
</TD>
</Tr>
<Input type = "hidden" name = "request_locale" value = "<% = (string) Session. getattribute (" language ") %>"/>
<Tr>
<TD colspan = "2" align = "center">
<S: Submit type = "input" Key = "label_submit"> </S: Submit>
<S: reset type = "password" Key = "label_reset"> </S: reset>
</TD>
</Tr>

<Tr>
<TD colspan = "2">
<S: fielderror>
</S: fielderror>
</TD>
</Tr>
</S: Form>
</Table>

<! -- Select the language -->

<S: URL id = "zhongwen" Action = "changelang. Action">
<S: Param name = "language"> zh_cn </S: param>
</S: URL>
<S: a href = "% {zhongwen}"> Chinese </s: A>

<S: URL id = "English" Action = "changelang. Action">
<S: Param name = "language"> en_us </S: param>
</S: URL>
<S: a href = "% {English}"> English </s: A>
</Body>
</Html>

// User model

Package Yang. WWW;

Public class userbean
{
Private string username;
Private string password;


Public userbean (){}


Public String GetUserName (){
Return username;
}
Public String GetPassword (){
Return password;
}


Public void setusername (string username ){
This. Username = username;
}
Public void setpassword (string password ){
This. Password = password;
}
}


// Set the language:

Package Yang. WWW;

Import java. util. locale;

Import org. Apache. struts2.servletactioncontext;

Import com. opensymphony. xwork2.actioncontext;
Import com. opensymphony. xwork2.actionsupport;

Public class changelang extends actionsupport
{
/**
*
*/
Private Static final long serialversionuid = 1l;
Private string language;

Public String getlanguage (){
Return language;
}
Public void setlanguage (string language ){
This. Language = language;
}
Public String execute ()
{
String [] strings = This. getlanguage (). Split ("_");
Locale locale = new locale (strings [0], strings [1]);
Servletactioncontext. getcontext (). setlocale (locale );
Actioncontext. getcontext (). getapplication (). Put ("Lang", this. getlanguage ());
Return success;
}
}

// Login action

Package Yang. WWW;

Import com. opensymphony. xwork2.actioncontext;
Import com. opensymphony. xwork2.actionsupport;
Import com. opensymphony. xwork2.modeldriven;

Public class login extends actionsupport implements modeldriven <userbean>
{
/**
*
*/
Private Static final long serialversionuid = 1l;
Private userbean user = new userbean ();
@ Override
Public userbean GetModel (){
// Todo auto-generated method stub
Return user;
}

Actioncontext context = actioncontext. getcontext ();
Public String execute ()
{
Boolean rightuser = "yangzhiyong". Equals (this. GetModel (). GetUserName ());
Boolean rightpassword = "yangzhiyong". Equals (this. GetModel (). GetPassword ());
If (rightuser & rightpassword)
{
Context. getsession (). Put ("username", this. GetModel (). GetUserName ());
Return success;
}
Else
{
Return Error;
}
}

Public void validate ()
{
If (this. GetModel (). GetUserName (). Length () <6)
{
This. addfielderror ("username", this. gettext ("label_usererror "));
}
If (this. GetModel (). GetPassword (). Length () <5)
{
This. addfielderror ("password", this. gettext ("label_passworderror "));
}
}
}


Struts. xml:

<? 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>
<Constant name = "struts. locale" value = "zh_cn"/>
<Constant name = "struts. Custom. i18n. Resources" value = "i18n/mymessage"/>

<Constant name = "struts. UI. Theme" value = "simple"/>
<Constant name = "struts. UI. templatedir" value = "template"/>
<Constant name = "struts. UI. templatesuffix" value = "FTL"/>

<Package name = "hello" extends = "struts-Default" namespace = "/">
<Action name = "login" class = "yang. www. login">
<Result name = "success">/JSP/welcome. jsp </result>
<Result name = "error">/JSP/error. jsp </result>
<Result name = "input">/index. jsp </result>
</Action>
<Action name = "changelang" class = "yang. www. changelang">
<Result name = "success">/index. jsp </result>
</Action>
</Package>

<! -- Add packages here -->

</Struts>

// Error. jsp

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
Pageencoding = "UTF-8" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> login error </title>
</Head>
<Body>
<H1> sorry login failed! </H1>
</Body>
</Html>

// Welcome. jsp

<? XML version = "1.0" encoding = "UTF-8"?>
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8"
Pageencoding = "UTF-8" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> login success! </Title>
</Head>
<Body>
<S: property value = "# session. username"/>
<H3> welcome, you login successfully! </H3>
<S: Text name = "welcome"> </S: Text>
</Body>
</Html>

Complete and runable examples can be downloaded from my resources. This may be more understandable. Haha. If you have any questions, please contact us.

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.