setLocale with value from bean-function
[nobr]Hello,
hopefully my english is good enough for my problem.
I'm playing around with jsp's for a couple of weeks now, and so I am not really good now. Most of the problems I found out by myself, but this time I am over my head.
index.jsp:
...
<%@taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<fmt:setLocale value="#{account.fetchLanguage}"/>
<fmt:setBundle basename="messages"/>
...
<fmt:message key="start.welcome"/><br/>
...
Inside the java file for my bean I declared
private String language ="de";
and created a function
public String fetchLanguage(){
return language;
}
It compilies without any problems but on my browser I always see ?start.welcome?
If i replace the function-call with <fmt:setLocale value="de"/> it works fine.
What Iam doing wrong?
Thanks alot,
Hauke
Message was edited by:
HaukeG[/nobr]
[1686 byte] By [
HaukeGa] at [2007-11-27 9:00:27]

# 1
You are mistaken about how to use properties. Make the following changes:
<fmt:setLocale value="#{account.language}"/>
and
public String getLanguage(){
return language;
}
edit: uuuh, just realized this is JSF. But I suspect it is exactly the same as other java bean methods.
# 2
[nobr]Hello,
thanks for your answer, but the result is the same. Now I have this :
PS.: Its JSF 1.2
...
private String language = "de";
public String getLanguage(){
return language;
}
...
and my jsp
<%@taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<f:view>
<fmt:setLocale value="#{account.language}"/>
<fmt:setBundle basename="messages"/>
</f:view>
...
<fmt:message key="start.welcome"/><br/>
Does anyone else has another idea how to fix my problem? Just in case, as soon as I put ... value="de"/> it works fine.
Thanks for your help again.
Hauke
Message was edited by:
HaukeG[/nobr]
# 3
Do you have a bean called "account" in a relevant scope?
Try
<%= pageContext.findAttribute("account") %> to make sure it is not null.
You also seem to be intermixing JSTL with your JSF. With JSTL1.2 that shouldn't be a problem. But the URIs you are using for the jstl libraries are for version 1.0
Try the following imports:
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Note the subtle difference with the addition of /jsp into the JSTL tag libraries.
Also make sure your web.xml file is fully up to date, and defined as 2.5
I presume you are using Tomcat 6 or equivalent?
I'm not up on the latest of the "unified expression language" and all of its repercussions, but I know that JSF had equivalent tag libraries to the JSTL for printing out resources, in fact it may even be a bit more flexible.
You would have to change your account bean to return a locale instead of just a string ie
private String language = "de";
private Locale locale = new Locale(language);
public String getLanguage(){
return language;
}
public Locale getLocale(){
return locale;
}
...
And then in your jsp:
<f:view locale="#{account.locale}">
<f:loadBundle basename="messages" var="msgs"/>
<h:outputText value="#{msgs['start.welcome']}"/>
</f:view>
Hope this helps (and doesn't confuse you too much)
evnafets
# 4
Hello,
I trashed everything, cause nothing realy helped :-( I hope it will not be to much to post here, but I try the complete structure again. I am not using JSF anymore.
New.JSP
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<jsp:useBean id="account" scope="session" class="entities.Account"/>
<fmt:setLocale value="#{account.getLocale}"/>
<fmt:setBundle basename="messages"/>
<html>
<head>
<title>New Account</title>
</head>
<body>
<h1><fmt:message key="create.account.title"/></h1>
...
the bean entities.account is kind of big, but I try to compress it
package entities;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.sql.*;
import java.security.MessageDigest;
import java.util.Locale;
public class Account implements Serializable {
private String lang = "de";
private Locale locale = new Locale(lang);
public Account() {
}
public String getLanguage(){
return lang;
}
public Locale getLocale(){
return locale;
}
And again, it workes fine if I replace #{account.getLocale} by "de". I also tried account.getLanguage but both without any luck.
Its really frustrating cause I try it since 5 days already. Thanks a lot
Hauke
# 5
Hi Hauke,Can you try replacing "<fmt:setLocale value="#{account.getLocale}"/>"as<fmt:setLocale value="${account.locale}"/>I am not an expert in this area. Let's try to at least ease up the frustration. Some one will help you mean time.
# 6
Hi.Thanks for the fast answerI am getting this error in the browser:org.apache.jasper.JasperException: /New.jsp(8,0) According to TLD or attribute directive in tag file, attribute value does not accept any expressionHauke
# 7
Hello - it's me again :-(
Maybe somebody still reads this.
I found out, that
<jsp:useBean id="account" scope="session" class="entities.Account"/>
<fmt:setLocale value="#{account.geguage}"/>
<fmt:setBundle basename="messages"/>
doesn't result in an error at all. There is no variable or method inside account with the name geguage. Shouldn't java say something about that?
Hauke
# 8
Hauke,This was not the original JSP you posted earlier. You didn't have "geguage" in your earlier JSP. Anyway, Is it working for you?