您的位置:首页 > 编程语言 > Java开发

Struts 1 学习笔记-5-2(编写一个简单的支持I18N的登录系统)

2008-02-16 17:21 861 查看
1.首先编写了一个ChangeLanguageAction.java:


package com.codedestiny.struts;




import java.util.Locale;


import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpServletResponse;


import org.apache.struts.Globals;


import org.apache.struts.action.Action;


import org.apache.struts.action.ActionForm;


import org.apache.struts.action.ActionForward;


import org.apache.struts.action.ActionMapping;






public class ChangeLanguageAction extends Action ...{






   public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{


      String lan = request.getParameter("lan");


      Locale currentLocale = null;




      if ("zh".equals(lan))...{


          currentLocale = new Locale("zh", "CN");




      } else if ("en".equals(lan)) ...{


          currentLocale = new Locale("en", "US");


      }




      if (currentLocale != null) ...{


          request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);


      }


      return mapping.findForward("success"); 


   }


}

 

2.这样在显示层增加如下链接,通过点击即可实现切换Locale:


<a href="changelan.do?lan=zh">切换成中文</a><br>


<a href="changelan.do?lan=en">切换成英文</a><br>

 

3.MessageResources.properties(前面的代码和Struts默认的相同):


# -- standard errors --


errors.header=<UL>


errors.prefix=<LI>


errors.suffix=</LI>


errors.footer=</UL>


# -- validator --


errors.invalid={0} is invalid.


errors.maxlength={0} can not be greater than {1} characters.


errors.minlength={0} can not be less than {1} characters.


errors.range={0} is not in the range {1} through {2}.


errors.required={0} is required.


errors.byte={0} must be an byte.


errors.date={0} is not a date.


errors.double={0} must be an double.


errors.float={0} must be an float.


errors.integer={0} must be an integer.


errors.long={0} must be an long.


errors.short={0} must be an short.


errors.creditcard={0} is not a valid credit card number.


errors.email={0} is an invalid e-mail address.


# -- other --


errors.cancel=Operation cancelled.


errors.detail={0}


errors.general=The process did not complete. Details should follow.


errors.token=Request could not be completed. Operation is not in sequence.


# -- welcome --


welcome.title=Struts Blank Application


welcome.heading=Welcome!


welcome.message=To get started on your own application, copy the struts-blank.war to a new WAR file using the name for your application. Place it in your container's "webapp" folder (or equivalent), and let your container auto-deploy the application. Edit the skeleton configuration files as needed, restart your container, and you are on your way! (You can find the application.properties file with this message in the /WEB-INF/src/java/resources folder.)


drp.user.name=User Name


drp.user.password=User Password


drp.user.login=Please Login


drp.user.login.success=Login Success!


drp.user.not.found=Not Found User


dpr.user.password.error=Password Error

 

4.MessageResources_zh_CN.properties用来支持中文(只贴出和上面不同的部分,中文已转为ISO-8859-1):


drp.user.name=User Name


drp.user.password=User Password


drp.user.login=Please Login


drp.user.login.success=Login Success!


drp.user.not.found=Not Found User


dpr.user.password.error=Password Error

 

5.LoginAction.java:

 


package com.codedestiny.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;






public class LoginAction extends Action ...{




    




    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception ...{


        LoginActionForm lag = (LoginActionForm)form;


        String username = lag.getUsername();


        String password = lag.getPassword();




        ActionMessages messages = new ActionMessages();




        try ...{


            


            //调用助手类进行合法性检查


            UserHelper.validate(username, password);


            


            //创建消息


            ActionMessage msg = new ActionMessage("drp.user.login.success");


            messages.add("loginsuccess", msg);




            //传递消息


            this.saveMessages(request, messages);


            


            return mapping.findForward("success");




        }catch(UserNotFoundException unfe) ...{


            unfe.printStackTrace();


            ActionMessage msgError = new ActionMessage("drp.user.not.found");


            messages.add("usernotfound", msgError);


            


            //传递错误消息


            this.saveErrors(request, messages);                


            //this.saveMessages(request, messages);




        }catch(PasswordErrorException pee) ...{


            pee.printStackTrace();


            ActionMessage msgError = new ActionMessage("dpr.user.password.error");


            messages.add("passworderror", msgError);


            


            //传递错误消息


            this.saveErrors(request, messages);


            //this.saveMessages(request, messages);


        }


        return mapping.findForward("error");


    }


}



 

6.login.jsp:




<%...@ page language="java" contentType="text/html; charset=GB18030"


    pageEncoding="GB18030"%>




<%...@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>  




<%...@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>  


<!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=GB18030">


        <title></title>


    </head>


    <body>


        <hr>


         <html:errors/>    


        <form action="login.do">


            <bean:message key="drp.user.name"/>:<input type="text" name="username"><br>


            <bean:message key="drp.user.password"/>:<input type="password" name="password"><br>


            <hr>


            <input type="submit" name="btn_login" value="<bean:message key="drp.user.login"/>">


        </form>


    </body>


</html>



 

7.login_success.jsp:




<%...@ page language="java" contentType="text/html; charset=GB18030"


    pageEncoding="GB18030"%>




<%...@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>  




<%...@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>  


<!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=GB18030">


<title></title>


</head>


<body>


    <html:messages id="msg" message="true" property="loginsuccess">


        <bean:write name="msg"/>


    </html:messages>


</body>


</html>

 

8.测试截图:

中文:



英文:



登录成功:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐