Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Conversion to JSTL - Help

 
Hari Maha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell on how to convert this into JSTL

I am using JSP 2.0, JSTL 1.1 and tomcat. The "PropertyFileReader" used here reads a property file and returns a HashMap on calling getProperty().
The logic is just to change the CSS, where by user selects the theme in the previous page.

<%
String contextPath = request.getContextPath();
PropertyFileReader propertyFile = new PropertyFileReader("resources.Themes");

String themeId = "1";
themeId = request.getParameter("themeId");

if (themeId == null || "".equals(themeId))
themeId = "1";

String cssName = (String)(propertyFile.getProperty().get(themeId));

pageContext.setAttribute("cssName",cssName);
%>

<html>
<head>
<title>Theme</title>
<link rel="stylesheet" type="text/css" href="<%=contextPath%>/css/Homepage.css">
<link rel="stylesheet" type="text/css" href="<%=contextPath%>/css/${cssName}">
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Hariharan",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Property file access is handled by the fmt class of JSTL tags. In particular please see the <fmt:message> tag.
 
Hari Maha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, It worked. Thanks
So the changed code is as follows.

<%
String contextPath = request.getContextPath();
%>

<fmt:setBundle basename="resources.Themes" var="theme" scope="session"/>
<c:set var="themeId" value="1"/>

<c:choose>
<c:when test="${empty param.themeId}">
<c:set var="themeId" value="1"/>
</c:when>

<ctherwise>
<c:set var="themeId" value="${param.themeId}"/>
</ctherwise>
</c:choose>

<cut value="${param.themeId}"/>
<html>
<head>
<title>Theme</title>
<link rel="stylesheet" type="text/css" href="<%=contextPath%>/css/<fmt:message key="${themeId}" bundle="${theme}"/>">

And I think, I have changed my display name correctly?

And I also want to know, if i can also change
<% String contextPath = request.getContextPath(); %>
to JSTL

Thank You in advance
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I also want to know, if i can also change <% String contextPath = request.getContextPath(); %> to JSTL

${pageContext.request.contextPath}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic