我的一个国际化问题:
一个登陆界面,如下:
下面有一个“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来完成啊?请说的详细点啊!!!我是新手,很垃圾的~~~`帮帮忙啊~~~~~
我在这里谢过了!!!!
一个登陆界面,如下:
下面有一个“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来完成啊?请说的详细点啊!!!我是新手,很垃圾的~~~`帮帮忙啊~~~~~
我在这里谢过了!!!!
解决方案 »
- Hibernate查询空值针
- Hibernate1对多单向(注解实现)映射问题
- [email\\](.[^\\[]*)(\\[\\/email\\])这个正则表达式是什么意思啊?
- 在自己内部定义自己一个实例?我一直很奇怪,这个一般是干什么用的。
- 连接池找不到属性文件!!!
- JSP文章发布求助
- 大家帮一下忙,有没有计算TEXT输入框的英文和中文实际字符长度的JS
- 新手上门!请高手看看这个错误!
- 高手帮我看看这段代码,关于ArrayList 的get(int)方法的
- 关于在sqlserver中查询的问题!!!
- tomcat结束进程后启动不了
- 我用的是resin服务器运行jsp,但总是找不到com.*;
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);
} 难到就写上面的代码就可以了吗?
存到session也是一个不错的主意,不过他下次再来,还得重新设置一次。呵呵!
1、资源文件:在struts-config.xml中配置、创建各种语言的资源文件并写入内容、通过“native2ascii”转为unicode编码
2、引用资源文件:<bean:message key="***"/>
3、编码切换:也就是点击链接后Action中做的,也可以通过过滤器、拦截器等其他方式设置具体过程会包含一些其他细节,google“struts国际化”,会有很多例子。
Struts 2支持用户在程序中自行选择语言。
我这里有代码,你可以查考下: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");
}
servletResponse
都没有被声明啊!!在哪声明啊!!!!
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {改成public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
和他的get和set方法是不是啊?
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
<li> <html:link action="/Locale?language=en"> English </html:link> </li>
<li> <html:link action="/Locale?language=zh"useLocalEncoding="true"> chinese </html:link> </li>需要改吗?
type Status reportmessage Invalid path /Locale was requesteddescription The requested resource (Invalid path /Locale was requested) is not available.
怎么回事啊?
login.jsp你自己试试看,只要能执行到Aciton就可以了,不行的话在改咯
还是其他的?
........
}
改成:
html:link action="/cookie.do?language=en"> English </html:link>
还是有错!!呵呵!!
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)
.........
}
}