 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
rhassinger12@yahoo.com Guest
|
Posted: Tue Jun 21, 2005 4:39 am Post subject: locale-specific include for JSP? |
|
|
Is there a way to use a jsp:include for a locale? For example, if the
locale is US, then take homepage_us.html, if it's ES, use
homepage_es.html, etc.
This seems obvious to me (since I don't want to have all that stuff in
a .properties file) but I can't find anything about it, except for a
feature of BEA weblogic.
|
|
| Back to top |
|
 |
SMC Guest
|
Posted: Tue Jun 21, 2005 6:15 am Post subject: Re: locale-specific include for JSP? |
|
|
On Tue, 21 Jun 2005 14:39:06 +1000, rhassinger12 wrote:
| Quote: | Is there a way to use a jsp:include for a locale? For example, if the
locale is US, then take homepage_us.html, if it's ES, use
homepage_es.html, etc.
This seems obvious to me (since I don't want to have all that stuff in a
.properties file) but I can't find anything about it, except for a
feature of BEA weblogic.
|
You could of course do:
if (locale = US)
include homepage_us
else if (locale = ES)
include homepage_es
But more elegant might be to write a custom include tag perhaps? We wrote
our own to decide which JSP of the given name to include from a set of
rules. For your case, the tag could be
<o:include page="homepage"/>
The taglib body might be as simple as:
protected String page = "";
public String getPage(){ return page; }
public void setPage(String page){
this.page = page;
}
public int doStartTag() throws JspException{
try{
JspWriter out = pageContext.getOut();
if ("true".equals(flush) && !(out instanceof BodyContent))
out.flush();
ServletContext context = pageContext.getServletContext();
ServletRequest request = pageContext.getRequest();
ServletResponse response = pageContext.getResponse();
// Some locale code here
String localeExt; /* = something */
String resourcePath = path + localeExt + ".jsp";
RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
rd.include(request,new ServletResponseWrapperInclude(response,
out));
}catch(Exception e){
throw new JspTagException("IncludeTag ("+module+"):"+e.toString());
}
return SKIP_BODY;
}
Or something like that.
--
Sean
I don't suffer from insanity, I enjoy every minute of it.
|
|
| Back to top |
|
 |
rhassinger12@yahoo.com Guest
|
Posted: Tue Jun 21, 2005 10:36 pm Post subject: Re: locale-specific include for JSP? |
|
|
Thanks, I was thinking about this approach too. However the obvious
problem is that this doesn't fall back to a default page when someone
from an unknown country connects. On every include, we would have to
check the filesystem to see if a jsp exists, then fall back (to avoid
exceptions slowing us down). I was thinking that another possibility
would be to have the filename in the resources properties file itself,
but that means keeping things in two places. Not very maintainable. I
would have thought JSTL would have a feature such as this. BEA got the
right idea and implemented it.
SMC wrote:
| Quote: | On Tue, 21 Jun 2005 14:39:06 +1000, rhassinger12 wrote:
Is there a way to use a jsp:include for a locale? For example, if the
locale is US, then take homepage_us.html, if it's ES, use
homepage_es.html, etc.
This seems obvious to me (since I don't want to have all that stuff in a
.properties file) but I can't find anything about it, except for a
feature of BEA weblogic.
You could of course do:
if (locale = US)
include homepage_us
else if (locale = ES)
include homepage_es
But more elegant might be to write a custom include tag perhaps? We wrote
our own to decide which JSP of the given name to include from a set of
rules. For your case, the tag could be
o:include page="homepage"/
The taglib body might be as simple as:
protected String page = "";
public String getPage(){ return page; }
public void setPage(String page){
this.page = page;
}
public int doStartTag() throws JspException{
try{
JspWriter out = pageContext.getOut();
if ("true".equals(flush) && !(out instanceof BodyContent))
out.flush();
ServletContext context = pageContext.getServletContext();
ServletRequest request = pageContext.getRequest();
ServletResponse response = pageContext.getResponse();
// Some locale code here
String localeExt; /* = something */
String resourcePath = path + localeExt + ".jsp";
RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
rd.include(request,new ServletResponseWrapperInclude(response,
out));
}catch(Exception e){
throw new JspTagException("IncludeTag ("+module+"):"+e.toString());
}
return SKIP_BODY;
}
Or something like that.
--
Sean
I don't suffer from insanity, I enjoy every minute of it.
|
|
|
| Back to top |
|
 |
SMC Guest
|
Posted: Wed Jun 22, 2005 1:13 am Post subject: Re: locale-specific include for JSP? |
|
|
We had to check for the existence of a file like you say, but we kept a
(static) hash of the results so that we only had to do it once. Something
like:
protected static Hashtable resourceList = new Hashtable();
...
URL url = application.getResource(thisFile);
if (url != null){
// Mark that this file exists
resourceList.add(thisFile,new Boolean(true));
}else{ // ignore missing resources
// Mark that this file doesn't exist
resourceList.add(thisFile,new Boolean(false));
}
If a feature like this existed in the JSTL, it would have to address this
itself somehow anyway, as no doubt the BEA version would too. If the BEA
version were open source we could see how they did it :-)
On Wed, 22 Jun 2005 08:36:20 +1000, rhassinger12 wrote:
| Quote: | Thanks, I was thinking about this approach too. However the obvious
problem is that this doesn't fall back to a default page when someone
from an unknown country connects. On every include, we would have to
check the filesystem to see if a jsp exists, then fall back (to avoid
exceptions slowing us down). I was thinking that another possibility
would be to have the filename in the resources properties file itself,
but that means keeping things in two places. Not very maintainable. I
would have thought JSTL would have a feature such as this. BEA got the
right idea and implemented it.
SMC wrote:
On Tue, 21 Jun 2005 14:39:06 +1000, rhassinger12 wrote:
Is there a way to use a jsp:include for a locale? For example, if the
locale is US, then take homepage_us.html, if it's ES, use
homepage_es.html, etc.
This seems obvious to me (since I don't want to have all that stuff
in a .properties file) but I can't find anything about it, except for
a feature of BEA weblogic.
You could of course do:
if (locale = US)
include homepage_us
else if (locale = ES)
include homepage_es
But more elegant might be to write a custom include tag perhaps? We
wrote our own to decide which JSP of the given name to include from a
set of rules. For your case, the tag could be
o:include page="homepage"/
The taglib body might be as simple as:
protected String page = "";
public String getPage(){ return page; } public void setPage(String
page){
this.page = page;
}
public int doStartTag() throws JspException{
try{
JspWriter out = pageContext.getOut(); if
("true".equals(flush) && !(out instanceof BodyContent))
out.flush();
ServletContext context = pageContext.getServletContext();
ServletRequest request = pageContext.getRequest();
ServletResponse response = pageContext.getResponse();
// Some locale code here
String localeExt; /* = something */
String resourcePath = path + localeExt + ".jsp"; RequestDispatcher
rd = request.getRequestDispatcher(resourcePath);
rd.include(request,new ServletResponseWrapperInclude(response,
out));
}catch(Exception e){
throw new JspTagException("IncludeTag
("+module+"):"+e.toString());
}
return SKIP_BODY;
}
Or something like that.
--
Sean
I don't suffer from insanity, I enjoy every minute of it.
|
--
Sean
I've always found [the news media] quite accurate, except when they're
reporting on a topic I know something about. --usenet post
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|