我的一个国际化问题:
一个登陆界面,如下:
下面有一个“English”和“chinese”的链接,只要点击任何一个都能国际化,而不是要通过更改Internet的语言设置,再刷新来更改?主页面中的:<li><html:link action="/Locale?language=en">English</html:link></li>
                  <li><html:link action="/Locale?language=zh"useLocalEncoding="true">chinese</html:link></li>
要怎么写这个Action来完成啊?请说的详细点啊!!!我是新手,很垃圾的~~~`帮帮忙啊~~~~~
我在这里谢过了!!!!

解决方案 »

  1.   

    写locale到cookie,然后... 其它的就不能让系统自动控制了,你的从cookie读取,然后自己设置locale了,比较麻烦的
      

  2.   

    我的意思就像struts包下的struts-mailreader里的例子一样,点击链接就能完成各种语言的转换!
    Struts   1.2.1自带的例子Mailreader   Example包含了日本资源文件,虽然我们不需要日本资源,但它对于我们学习如何实现“国际化信息”是非常有好处的,实现方式很简单,例如自带的例子welcome.jsp有如下语言类型选择:  
      <ul>  
      <li><html:link   action="/Locale?language=en">English</html:link></li>  
       
      <li><html:link   action="/Locale?language=ja"   useLocalEncoding="true">Japanese</html:link></li>  
       
      <li><html:link   action="/Locale?language=ru"   useLocalEncoding="true">Russian</html:link></li>  
       
      </ul>  
       
      在LocaleAction.java中进行处理:  
       
      public   ActionForward   execute(ActionMapping   mapping,  
       
                                        ActionForm   form,  
       
                                        HttpServletRequest   request,  
       
                                        HttpServletResponse   response)  
       
              throws   Exception   {  
       
         
       
                      String   language   =   request.getParameter(LANGUAGE);  
       
                      String   country   =   request.getParameter(COUNTRY);  
       
         
       
                      Locale   locale   =   getLocale(request);  
       
         
       
                      if   ((!isBlank(language))   &&   (!isBlank(country)))   {  
       
                              locale   =   new   Locale(language,   country);  
       
                      }  
       
                      else   if   (!isBlank(language))   {  
       
                              locale   =   new   Locale(language,   "");  
       
                      }  
       
         
       
                      HttpSession   session   =   request.getSession();  
       
                      session.setAttribute(Globals.LOCALE_KEY,   locale);  
       
         
       
                      String   target   =   request.getParameter(PAGE);  
       
                      if   (!isBlank(target))   return   new   ActionForward(target);  
       
         
       
                      target   =   request.getParameter(FORWARD);  
       
                      if   (isBlank(target))   target   =   mapping.getParameter();  
       
                      if   (isBlank(target))   {  
       
                              log.warn(LOCALE_LOG);  
       
                              return   null;  
       
                      }  
       
                      return   mapping.findForward(target);  
       
              }   难到就写上面的代码就可以了吗?
      

  3.   

    我认为可以!
    存到session也是一个不错的主意,不过他下次再来,还得重新设置一次。呵呵!
      

  4.   

    struts的国际化主要包含以下几个部分:
    1、资源文件:在struts-config.xml中配置、创建各种语言的资源文件并写入内容、通过“native2ascii”转为unicode编码
    2、引用资源文件:<bean:message key="***"/>
    3、编码切换:也就是点击链接后Action中做的,也可以通过过滤器、拦截器等其他方式设置具体过程会包含一些其他细节,google“struts国际化”,会有很多例子。
      

  5.   

    印象中Struts 1好像不支持楼主所说的在页面加链接来改变语言的,只能是自动检测IE的语言来决定用哪个语言包。
    Struts 2支持用户在程序中自行选择语言。
      

  6.   

    首先,你可以把索要设置的的语言写入cookie里比如:0代表China  1代表English
    我这里有代码,你可以查考下:import org.apache.struts.Globals;
    import java.util.Locale;
    import javax.servlet.http.Cookie;
      public   ActionForward   execute(ActionMapping   mapping,      
                                       ActionForm   form,  HttpServletRequest   request,       
                                        HttpServletResponse   response)   
        
                                          throws   Exception   {           LoginForm frm = (LoginForm) actionForm;        
            String param="0";
            Cookie[] allcookie=servletRequest.getCookies();               
            if(frm.getLanguage()==null){    //首先获取cookie
            for(int i=0;i<allcookie.length;i++){     
             if(allcookie[i].getName().equals("language")){         
             param=allcookie[i].getValue();
             break;
             }
             else{       //set cookie default 0
             Cookie cookie=new Cookie("language",param);
             cookie.setMaxAge(60*60*24*365);       //一年 
               servletResponse.addCookie(cookie);                      
             }
            }
        }        
            else{    //set cookie        
             Cookie cookie=new Cookie("language",frm.getLanguage()); //设置 cookie 0
           cookie.setMaxAge(60*60*24*365);                     //30*60 半小时
             servletResponse.addCookie(cookie);      
             param=frm.getLanguage();        
            }       
       
            switch(new Integer(param).intValue()){
            case 0:
             servletRequest.getSession().setAttribute(Globals.LOCALE_KEY,Locale.CHINA);
             break;
            case 1:
             servletRequest.getSession().setAttribute(Globals.LOCALE_KEY,Locale.ENGLISH);
             break;   
            default:
             servletRequest.getSession().setAttribute(Globals.LOCALE_KEY,Locale.ENGLISH);
         break;        
            }     return actionMapping.findForward("index");

      

  7.   

    servletRequest
    servletResponse
    都没有被声明啊!!在哪声明啊!!!!
      

  8.   

    public   ActionForward   execute(ActionMapping   mapping,      
                                       ActionForm   form,  HttpServletRequest   request,       
                                        HttpServletResponse   response)   {改成public   ActionForward   execute(ActionMapping   mapping,      
                                       ActionForm   form,  HttpServletRequest   servletRequest,          HttpServletResponse   servletResponse)  { 
      

  9.   

    然后在LoginForm里写:private String language;
    和他的get和set方法是不是啊?
    public String getLanguage() {
    return language;
    }
    public void setLanguage(String language) {
    this.language = language;
    }
      

  10.   

    那在login.jsp中,两个链接:
    <li> <html:link action="/Locale?language=en"> English </html:link> </li>
                       <li> <html:link action="/Locale?language=zh"useLocalEncoding="true"> chinese </html:link> </li>需要改吗?
      

  11.   

    我一点链接出现错误:HTTP Status 404 - Invalid path /Locale was requested
    type Status reportmessage Invalid path /Locale was requesteddescription The requested resource (Invalid path /Locale was requested) is not available.
    怎么回事啊?
      

  12.   

    对,get set 
    login.jsp你自己试试看,只要能执行到Aciton就可以了,不行的话在改咯
      

  13.   

    你的action 是否继承extends DispatchAction
    还是其他的?
      

  14.   

    没有,我就是建了个普通的action!
      

  15.   

    class CookieAction extends Action{
    ........
    }
      

  16.   

    你的<html:link action="..."/>这里一定是写错了,你自己看看吧
      

  17.   

    <html:link action="/Locale?language=en">  English  </html:link>
    改成:
    html:link action="/cookie.do?language=en">  English  </html:link>
    还是有错!!呵呵!!
      

  18.   


    html:link action="/cookie.do?language=en">   English   </html:link> class CookieAction extends DispatchAction{  public   ActionForward   en(ActionMapping   mapping,      
                                       ActionForm   form,  HttpServletRequest   request,       
                                        HttpServletResponse   response) 
        .........
        }
    }