• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help translate to JSTL and EL

 
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Could anyone help me translate this part to jstl and el?


thanks in advance
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Three tips to get your started:

1) To loop through the top level array:
<c:forEach var="row" items="vec">
</c:forEach>
2) The syntax for accessing an array in JSTL is ${row[index]}.
3) There is no need to call Integer.parseInt() only to immediately display the value (logically as a String.)

See how much you cna get done with these three tips and post where you get stuck.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne,
Thanks for your reply. I will soon give you an update on my progression.
Regards
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kudos for making progress in doing things the appropriate and modern way. I'd also recommend that you always indent your code properly. Unindented code is really hard to inspect.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne,
This is what i have so far:

<c:set var="i" value="0" />
<c:set var="nrows" value="5" />
<%
//I don't know how to declare an array
//and the while loop in jstl
String[][] vec=new String[3][5];
while(i<nrows){
%>

<c:if test="${i==0}">

<tr>
<td ><b>id_article</b></td>
<td ><b><font color="#FF0033">nbr_stock</font></b></td>
<td ><b>critical_nbr</b></td>
<td ><b><font color="#FF0033">date_first_stock</font></b></td>
<td ><b>date_last_maj</b></td>
<td ><b>heure_last_maj</b></td>
</tr>


</c:if>
<!-- I don't know how to translate the ?: operator below -->
<tr>
<td >
<%= (Integer.parseInt(vec[i][1])<=Integer.parseInt(vec[i][2]))
?"bgcolor=\"FF0000\"":""%>><b><c:out ${vec[i][0]}/></b>
</td>

<td >
<font color="#FF0033"><c:out value="${vec[i][1]}"/></font>
</td>
<td >
<c:out value="${vec[i][2]}"/>
</td>
<td >
<font color="#FF0033"><c:out value="${vec[i][3]} ${vec[i][7]}"/></font>
</td>
<td >
<c:out value="${vec[i][4]}"/>
</td>
<td >
<c:out value="${vec[i][5]}"/>
</td>
</tr>



${i}=${i}+1;
</c:forEach>



 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • You don't create the array in the JSP. The data gets created in the page controller servlet and set into a request-scoped variable (via setAttribute()).
  • There are no while loops. You iterate with <c:foreach> as Jeanne already stated.
  • The font tag is long deprecated. Use CSS instead of obsolete markup.
  • An array of arrays is a very poor choice to model the data. Much more commonis a list of beans. Why is the data in arrays rather than a better choice?
  •  
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    You don't create the array in the JSP. The data gets created in the page controller servlet and set into a request-scoped variable (via setAttribute()).



    Bear,
    I have a bean that creates the data but i don't know how to call it from the jsp using jstl and el. The bean is in a package name mg. It returns a multidimensional array.
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Again, the data should be created in the controller. If you don't have a controller, then you are not ready to use the JSTL and EL.

    Please read this article to understand appropriate web application structure.

    And again, why a multi-dimensional array? Sometimes that might be the most appropriate structure, but most often it is not. What data is it modeling?
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    If you don't have a controller, then you are not ready to use the JSTL and EL.


    Bear,
    Can you give me directions on how to create the controller?
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Did you read the article? If not, then posting is premature.
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    See how much you cna get done with these three tips and post where you get stuck.


    Hello Jeanne,
    I am waiting to hear from you. This is my new jsp page:
     
    Marshal
    Posts: 28193
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    You don't declare beans in JSTL. What you do is to create a bean in your controller -- the servlet which is going to forward to the JSP, usually -- and add it to one of the standard scopes. Often you will add the bean to the request scope, but if you want the bean to remain available for longer than that, you could use session scope. The JSP will then simply access the bean from that scope. No declaration is necessary.

    And that scriptlet you have there would also be moved to the controller, rather than putting it in the JSP. By the way using arrays is not the best idea either; it's more common to apply object orientation principles and use a Java Bean of some particular class.
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    And that scriptlet you have there would also be moved to the controller


    Hi Paul,
    Thanks for your reply. I have moved the code to the controller but i cannot translate the remaining scriptlets to jstl. I need a step by step guidance. My jsp page looks like this now:
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are ignoring most of the advice you have been given in this topic and not answered questions of the people who are trying to help you. How can you expect help if you ignore it?
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Bear,
    I think you are compounding the problem by asking me more questions and giving no indications as to how to approach them. What i have is a jsp page containing sciptlets that i want to translate to jstl and el. Jeanne and Paul gave me indications which i followed but i am yet to hear from them.
    Kind regards
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You cannot answer the question of what the data you are modeling is, and why you are using arrays? :confused:

    In any case, at your request I will bow out of this topic. Good luck with your question.
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    In any case, at your request I will bow out of this topic.


    Please, do not leave. You have brought up new terms and i will be very grateful if you could elaborate on them. What do you mean by data modeling? I have some tables that record statistics about some items which i want to display. I use classes that have methods that return multidimensional arrays and it would be very tedious and time consuming to edit all of them. Note that English is not my mother tongue.
    Regards
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    data modeling is simply the structures you use to represent (model) your data. In this case, you have chosen arrays to model your data. This is frequently not the best choice and the data would be better modeled some other way. But we don't know that until we understand the nature of your data, which you have not disclosed.

    The manner in which data is modeled is very very important to the JSPs. So much so, that even if the backend is returning a poor model (and the backend cannot be changed to return a better model), it is worth the effort and processing to reformat the data in the controller to a model better suited to consumption by the JSP.

    For example, let's say that some DB table is being returned as a 2D array. That's a horrible model. It's almost always better to use a List of beans for such data; where one bean represents one row of the table with properties that (usually) represent the columns.

     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    It's almost always better to use a List of beans for such data; where one bean represents one row of the table with properties that (usually) represent the columns.


    Bear, a 1000 thanks for your explanation. I was thinking of a class with a method that takes the 2D array and transforms it into the list you suggested. But how to go about it in code?
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Bear,
    This is what i found from the net for converting a 2d array to a list.

    But i don't know how to reference elements in the list. Any idea? I wish this would be using scriptlet first and then jstl.
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Making a List of lists is no better than a 2D array.

    I will ask one last time: what is the data representing? What does one row of the array/table represent?
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    I will ask one last time: what is the data representing? What does one row of the array/table represent?


    One row in array contains the following information:
    id_article,nbr_stock,critical_nbr,
    date_first_stock,date_last_maj,heure_last_maj,jour_mil,heure_first_stock

    where
    id_article | bigint(20)
    | nbr_stock | int(11)
    | date_first_stock | varchar(11)
    | heure_first_stock | varchar(11)
    | date_last_maj | varchar(11)
    | heure_last_maj | varchar(11)
    | id_mag | int(11)
    | critical_nbr | varchar(10)
    | jour_mil | double

    The array contains strings. Hope this is what you are asking for.
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Ngom wrote:
    One row in array contains the following information:



    Does it not make sense to capture that information in a bean with properties, rather than an array of values?

    What makes the code more clear, accessing the "first stock hour" as:

    someArray[4]

    or

    someBean.getFirstStockHour()

    ?

    Never, ever, ever use arrays to store anything that isn't really an array of similar values. A row of an array is a horrible, horrible way to represent a variety of properties for one item.

    This is why a red flag went up as soon as I heard "2D array". This isn't a spreadsheet, it represents a list of items. So the data modeling should be a list of items.

    Your data layer should not be creating and returning a 2D array. For each row in the table, it should create one bean that represents the item. Multiple rows in the table should be represented by a List of these beans.

    This is a basic concept of dealing with data, and it's one that you must confront and understand before attempting to show the data on a JSP page.

    P.S. Even if you were still using scriptlets, your data should have been a list of beans to being with. It's just that when using JSTL, it's even more important to be using data structures that make sense, and correctly model the data.
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Bear,
    Can you give me a starting point for the bean. How will it look like? I started it like this but got stuck:
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Not a bad start, but a few things to start with:
  • Indent your code! You have been asked numerous times to properly indent your code. Indent your code. Indent your code. I will not read any more code that is not properly indented.
  • Follow proper Java naming conventions. Variables should be in camel case with no underscores. So not heure_first_stock, but something like firstStockHeure. Note that i have changed the words to make it more readable. The class name should be ItemData, not Itemdata.
  • Dates should be stored as dates, not strings. (Unless they are already in display format and you will perform nbo actions at all on them except display.)
  • No non-readable abbreviations! So whatever "nbr" means in "critical_nbr" needs to be spelled out. For example, if it means "Non-breaking Ruble", the property name sjould be cricticlNonBrakngRuble, not cricticlNbr. "nbr" is meaningless.
  • Remeber, whatever the tbale c olumn names are had no bearing on the vairable and property names. Use good names, not silly abbreviations that someone used for database columns.
  • That also applies to silliness like "setHeure_first_stock(String hfs)". "hfs"? Not acceptable. What's wrong with "firstStockHeure"? Or something simple like "value" (I frequently just use the parameter name value in setters.)


  • Start with that and see where it takes you.
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Indent your code! You have been asked numerous times to properly indent your code. Indent your code. Indent your code. I will not read any more code that is not properly indented.


    Bear,
    About your first remark, can you tell me if this edited code is better indented?
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Paul Ngom wrote:About your first remark, can you tell me if this edited code is better indented?


    Yes, much better! Indenting reveals the structure of the code and makes it much much easier to inspect.

    Now on to fixing your names...

    After that, you'd change the code that creates the array from the database values (I assume you are using JDBC rather than an ORM like JPA?) to create a bean for each row, and add the beans to a List. That will be much easier to deal with at the UI layer (and elsewhere!), and be a much better way to model the data.
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Follow proper Java naming conventions. Variables should be in camel case with no underscores. So not heure_first_stock, but something like firstStockHeure. Note that i have changed the words to make it more readable. The class name should be ItemData, not Itemdata.


    Bear,
    about you second remark, did i do the proper corrections as follow?
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Maybe it's a language barrier (it doesn't like all the term are in English), but what's the "Maj" in dateLastMaj? And what does "jourMil" stand for? If they are real words in your language, fine. If they are abbreviations, not so much. Spell words out completely. There is no need to abbreviate variable names.

    Also, you are initializing all your member variables. Because you are going to set the members from the database row when the bean is created, there is no need to the initialization.

    Otherwise, looking pretty good.

    Can you put it to use?
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    If they are real words in your language, fine.


    Those words are meaningful in my language. I modified ItemData to look as follows:


     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'd also wonder if a generic class name like "ItemData" is the best choice. It could mean anything. What is one of these beans/rows representing?

    Next, create your list of beans in place of the 2D array...

     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Next, create your list of beans in place of the 2D array


    Good morning Bear,
    I am sorry i could not say bye bye to you before i went offline yesterday. I created the list of beans class to look like this:
     
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I note that in the getListOfItemData method you're only creating a single ItemData object, instead of one for each DB record. This will not work.

    And I agree that "ItemData" and "ListOfItemData" are not particularly meaningful class names.
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    I note that in the getListOfItemData method you're only creating a single ItemData object, instead of one for each DB record. This will not work.


    Hello Ulf,
    I am glad for your interest. Can you direct me exactly to the place in getListOfItemData where i need to effect a modification?


    And I agree that "ItemData" and "ListOfItemData" are not particularly meaningful class names.


    I will be happy if you could suggest some names.
    Kind regards
     
    Ulf Dittmer
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Can you direct me exactly to the place in getListOfItemData where i need to effect a modification?


    Think about it for a minute - if instead of creating one object you wanted to create one object for each DB record - what kind of change would that entail?

    As to the names of classes, it doesn't make sense for us to suggest names - they should reflect what kind of data they hold (which you know, but we don't).

     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ulf,
    I have moved this inside the while loop of getListOfItemData.
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    I'd also wonder if a generic class name like "ItemData" is the best choice. It could mean anything. What is one of these beans/rows representing?


    Bear,
    A bean represents the stock information about an item. Can i change ItemData to ItemStockData?
    Regards
     
    Ulf Dittmer
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    A bean represents the stock information about an item. Can i change ItemData to ItemStockData?


    That's a question for you to answer, is it not? Why are you so attached to "Item" and "Data"? Meaningful names help, generic ones - not so much. Think about the next person to read through your code, without you sitting next to him: Does the class, method or field name "X" help that person understand the code? That's what you should aim at. Also note that "the next person" could be you yourself, 2 years down the road when you have all forgotten about this project: is "X" a name you would like to see?
     
    Paul Ngom
    Ranch Hand
    Posts: 355
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Also note that "the next person" could be you yourself, 2 years down the road when you have all forgotten about this project: is "X" a name you would like to see?


    You are right Ulf. "X" is not a meaningful name. To be frank, i am not good at namings. But as the names stand now, i can manage. Can we proceed with the jsp page that displays the list? That is if my list contains the right information.
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Also, you do not need to create a list class. You'd just use ArrayList to collect the individual beans,
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic