File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get rid of this scriptlet?

 
Thomas Kennedy
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm inheriting a JSP done some years ago. There is a lot of script stuff in it. A typical example:



To begin with I want to get rid of this outer Java if-else thing:

<% if (view.getUser().isSomeBoolean()) { %>

and replace it with <c:if>. So of course I have to add the jstl core declaration at the top. But after that I'm not too clear on the syntax, given that the boolean condition is not on the view bean but on an object returned by a public method on the view. Can someone clue me in here? How do I dig down to that boolean method in the context of <c:if>?

Sorry, I don't know the JSP version we're using. I'm also going to fix the getter methods -- the ones taking an argument -- because I know that's bad syntax.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to do a hard work of writting Servlets classes and refactoring the JSP.

It will be a hard work, but it will be worth of it.
 
Paul Clapham
Sheriff
Pie
Posts: 20163
24
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first line, the jsp:useBean, declares that "view" is the name of a request attribute, correct? The EL treats request attributes, and in fact everything it gets hold of, as if they were written using the Java Bean conventions. So a "getUser()" method corresponds to a "user" property, and an "isSomeBoolean()" method corresponds to a "someBoolean" property. Therefore your first line translates into

Although... there's an else clause so you would be better off with c:choose followed by c:when with that test and then c:otherwise.

As for the "getFromDos(i)" issue you identified, you're right, that is hard to address in EL. (Maybe even impossible, I don't know.) The easiest way to address it is to produce a "getFromDos()" method which returns the underlying array, or list, or whatever it is. You can then iterate through that array or list with <c:forEach>.

Oh... but then I see you've got "getProgram(i)" and "getSomeString(i)". Parallel arrays. So yeah, the data design would need to be strengthened a bit, so that you have something which returns a list of objects, each of which has a "getFromDos()" method and a "getToDos()" method and a "getProgram()" method and a "getSomeString()" method. Like Hebert said, there's some groundwork to be done there.

 
Thomas Kennedy
Ranch Hand
Posts: 137
  • 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting, thank you. I'm beginning to understand that if I get rid of those parallel arrays and replace them with an ArrayList of, say, MyResponseDataRow types, I could then for-each my way through that collection and drop all the indexing. A lot of work, indeed, but these parallel arrays are a nasty business.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic