求助!jsf 国际化问题!

shuye100 2007-11-13 05:26:29
自己找了个视频练了一下JSF,但是碰到问题了。没有错误提示,帮帮忙把!以下是主页面源代码:
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSF 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<f:view locale="#{User.locale}">
<f:loadBundle basename="org.shuye100.resource.message" var="msg"/>
<h:form id="loginForm" rendered="true">
<h:outputText value="#{msg.userName}"></h:outputText><h:inputText id="userName" required="false" rendered="true" value="#{User.userName}"></h:inputText>
<br><h:outputText value="#{msg.password}"></h:outputText><h:inputSecret id="password" required="false" rendered="true" value="#{User.password}"></h:inputSecret>

<br><h:commandButton value="#{msg.login}" action="#{User.verfy}"></h:commandButton>
<br><h:selectOneMenu value="#{User.locale}">//实现中英文切换
<f:selectItem itemValue="en" itemLabel="#{msg.english}"/>
<f:selectItem itemValue="cn" itemLabel="#{msg.chinese}"/>
<f:converter converterId="LocaleConverter"/>
</h:selectOneMenu>
</h:form>
</f:view>
</body>
</html>
以下是faces=config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN" "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config >
<managed-bean>
<managed-bean-name>User</managed-bean-name>
<managed-bean-class>org.shuye100.vo.User</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
</navigation-rule>
<navigation-rule>
<from-view-id>/welcome.jsp</from-view-id>
</navigation-rule>
<navigation-rule>
<from-view-id>/index.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/welcome.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case></navigation-rule>
<converter>
<converter-id>LocaleConverter</converter-id>
<converter-class>
org.shuye100.vo.LocaleConverter
</converter-class>
</converter></faces-config>

我还定义了2个Bean
package org.shuye100.vo;

import java.util.Locale;

public class User {
private String userName;
private String password;
private Locale locale=new Locale("en"); //默认为英文
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
public String verfy(){ //验证功能,如果页面输入shuye100,则转到success.jsp页面
if(this.userName.equals("shuye100")){
return "success";
}else{
return "failure";
}
}

public Locale getLocale() {
return locale;
}

public void setLocale(Locale locale) {
this.locale = locale;
}
}

和(实现中英文切换功能)
package org.shuye100.vo;

import java.util.Locale;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

public class LocaleConverter implements Converter {

public Object getAsObject(FacesContext arg0, UIComponent arg1, String localeStr)
throws ConverterException {
Locale locale=new Locale("en");
if(localeStr.equals("en")){
return locale;
}else if(localeStr.equals("zh_CN")){
locale=new Locale("zh_CN");
return locale;
}
return locale;
}

public String getAsString(FacesContext arg0, UIComponent arg1, Object locale)
throws ConverterException {
if(locale.toString().equals("en")){
return "en";

}else if(locale.toString().equals("zh_CN")){
return "zh_CN";
}// TODO Auto-generated method stub
return "en";


}

}

实在是运行不成功,这的不知道哪里错了,大侠们帮帮忙吧!
...全文
91 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
JadoNet 2008-01-17
  • 打赏
  • 举报
回复
将LocaleConverter配置成User的属性,不然总是默认en,因为你根本没有改变Uers.locale

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧