1

On my JSP page I import a java class as

<%@page import="org.model.MyConstants"%>

In the MyConstants class I have a public function

public static String getBookTypeName(int bookTypeId){
    //lots of if's based on id
    String bookName = 'Fantasy';
    return bookName;
}

Now from my JSP page I am trying to call this function passing in a request bean value as the parameter.

Essentially one property of my bean is an integer which I used to display, now I want to display the String that goes along with it but want to do so in a way where I dont need to modify the bean and can just use my MyConstants method to get the string.

I have been outputting my bean value before using

<bean:write name="BookNotifyForm" property="bookTypeId" />

I have been able to use scriplets in the past but have never had to pass a bean value into it.

<c:out value="<%=MyConstants.SOME_STRING_OUTPUT %>" />

I think I have the idea down but the syntax is what is killing me. I believe I should be able to use c:set to store the bean variable, and then use c:out calling the method passing in the variable I stored. Something along the lines of....

<c:set var="bookTypeId" value="${BookNotifyForm.bookTypeId}" />

//help with syntax, trying
<c:out value="<%=MyConstants.getBookTypeName(bookTypeId) %>" /> 
<c:out value="<%=MyConstants.getBookTypeName(${bookTypeId}) %>" />

It must be possible to do something like this !?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user3013612
  • 61
  • 1
  • 4
  • 1
    This is a sample about why you should [**stop** use scriptlets](http://stackoverflow.com/q/3177733/1065197) – Luiggi Mendoza Feb 10 '14 at 19:26
  • Note: the link in my comment covers how to work with `public static` methods as well. – Luiggi Mendoza Feb 10 '14 at 19:27
  • I was just using this, but I figured there had to be a better way. Fantasy Horror Action Is this an acceptable way of doing this? – user3013612 Feb 10 '14 at 19:32
  • @user3013612 Not if you want to read or maintain it, no. There should already be a map of book type IDs to titles on the back end; there is zero reason to do this in the view layer. – Dave Newton Feb 11 '14 at 12:50

0 Answers0