• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I use jstl <c:set> in <c:forEach> loop of bean objects in a JSP?  RSS feed

 
Nick Richard
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm STUCK and could use really your help. I need to use a JSTL c:set tag or JavaScript to post user's input back to the server. I have a servlet called StarTeamServer which has an ArrayList of UserAccessDetailObject beans. The JSP displays the ArrayList table results fine, but I have no idea how to code the c:set on the forEach indexed loop. I hope you can please provide some examples to help..





 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

First thing, be sure to UseCodeTags when posting code. I've gone ahead and added them to your post. See how much easier it is to see the code?

Secondly, why are you using a useBean action? It's obsolete and not needed when using the JSTL and EL.

Is your page controller not placing the bean in request scope? If not, you are missing a fundamental concept.

Finally,
Nick Richard wrote:but I have no idea how to code the c:set on the forEach indexed loop.

Why do you feel you need a <c:set> at all?

Other:
  • Why a multi-part form?
  • <form> not <Form>
  • Your form action, if it even works, is fragile. Don't use page-relative URLs. Use server-relative URLs that start with the context path.
  •  
    Nick Richard
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the suggestions and for making the code much easier to read. I'm not sure how you did that. I am a novice at web development and piggy-backing off another JSP that we have in use currently. The issue is that when I update the data in my tables' input fields and submit the form the values do not appear to be updated in the array back on the servlet. I fetch the StarTeamServer bean from the request.

    HttpSession session = request.getSession(false);
    StarTeamServer stServer = (StarTeamServer) session.getAttribute("STServerBean");

    But, when I view the contents of stServer.getRequestedUsersSecurityAccessDetailObjectList() the details are original and not updated. I was reading that I needed to use a c:set tag. Where am I going wrong?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nick Richard wrote:Thank you for the suggestions and for making the code much easier to read. I'm not sure how you did that.

    There's link in my post that explains how to use code tags. There's also a "code" button on the topic form.

    The issue is that when I update the data in my tables' input fields and submit the form the values do not appear to be updated in the array back on the servlet. I fetch the StarTeamServer bean from the request.


    What gets submitted back to the form action servlet is independent of anything that you do in the JSP. The JSP is executed when the page is rendered, not when the form is submitted.

    HttpSession session = request.getSession(false);
    StarTeamServer stServer = (StarTeamServer) session.getAttribute("STServerBean");

    Attributes have nothing at all to do with form submission and its parameters. To get the values of submitted form inputs, you need use the getParameter() class of methods on the request. You shouldn't need to be dealing with the session at all.

    I was reading that I needed to use a c:set tag. Where am I going wrong?

    <c:set> has nothing at all to do with form submission. Nothing. At all.

    Again, JSP is a technology for generating the HTML page that's sent to the browser. Once sent, the JSP is done. Gone. Kaput-ski. A form submission generates a new response to its action and has nothing whatsoever to do with the generating JSP.

    Sounds like you need some info on what JSP actually is, and how it works. You're in luck! Read this article!
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And then when you're done with that, read this article which details how Java web apps should be organized. It's a bit tougher going, but thought it out and come back to it for details later.
     
    Nick Richard
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for your reply. I feel that maybe I have stated my question and replies unclear. I understand that the c:set has nothing to do with form submission. However, I believe some activity must occur in the JSP for the objects to be updated. I believe there needs to be setter type action.

    An example of what I mean is a jsp:setProperty is used to update an input that is defined with jsp:getProperty.

    Or, a scriplet like

    <%
    String myEmail = "ABCDE@zzzz.com";
    %>
    <jsp:setProperty property="email" name="CurrentUserBean" value="<%=myEmail %>"/>

    Back in the servlet I can use CurrentUserBean currentUserBean= (CurrentUserBean) session.getAttribute("CurrentUserBean") and currentUserBean.getEmail() will return "ABCDE@zzzz.com";

    I cannot use request.getParameter("zzz") in my servlet because it returns a single String value of a single request parameter. I believe I can loop threw all the request objects with request.getParameterValues and conditional test each one until I locate what I need. But, I have a servlet class 'StarTeamServer' that has a class level attribute 'ArrayList<UserAccessDetailObject> getRequestedUsersSecurityAccessDetailObjectList()' where the Array contains numerous entries of a bean class 'UserAccessDetailObject'. That is the reason I was using StarTeamServer stServer = (StarTeamServer) session.getAttribute("STServerBean") to return an object type and not a single String value. Once I have a StarTeamServer stServer from session.getAttribute("STServerBean") I can stServer.getRequestedUsersSecurityAccessDetailObjectList() and loop through each UserAccessDetailObject for the updated user entries.

    I can loop the contents of the ArrayList using a forEach loop or a Java script loop in my JSP. I can write the <td>data with c:out or jsp:getProperty . But, somewhere I need a setter type code in my JSP for each <td> attribute in the loop of the Array.

    Can you assist me with the specific code required as a setter in a forEach? Or, javascript that I can use such as my jsp:setProperty above?

    Thanks for your time.



     
    Paul Clapham
    Sheriff
    Posts: 22185
    38
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nick Richard wrote:However, I believe some activity must occur in the JSP for the objects to be updated.


    Well, now you've gone and confused the issue. Do you want to post data back to the server -- as you claimed in your original post -- or do you want to "update some objects"?

    Remember that whatever activity occurs in the JSP always occurs on the server, before the HTML it generates is even sent to the browser. So you can certainly update some objects (on the server) in the JSP, although it's bad form to do that. You should do it in your controller servlet before it forwards to the JSP.

    On the other hand if you want data to be sent from the browser, from the generated HTML, then you should probably have that HTML contain a <form> element from which the user can enter data and submit it to another servlet. You could involve JavaScript here as well but it's unnecessary unless you need to submit the data in a fancier way for some reason.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nick Richard wrote:Thank you for your reply. I feel that maybe I have stated my question and replies unclear. I understand that the c:set has nothing to do with form submission. However, I believe some activity must occur in the JSP for the objects to be updated. I believe there needs to be setter type action.

    Why do you feel this? It's highly unusual to need to update items in a JSP that's generating a page to send to the browser, an therefore, it's a huge red flag.

    An example of what I mean is a jsp:setProperty is used to update an input that is defined with jsp:getProperty.

    Agin, for what purpose?. Why would this value not already be at the correct setting before the JSP is invoked?
    Or, a scriplet like
    <%
    String myEmail = "ABCDE@zzzz.com";
    %>
    <jsp:setProperty property="email" name="CurrentUserBean" value="<%=myEmail %>"/>

    Scriptlets are obsolete. They shouldn't even be thought of a solution of any type.

    I cannot use request.getParameter("zzz") in my servlet because it returns a single String value of a single request parameter. I believe I can loop threw all the request objects with request.getParameterValues and conditional test each one until I locate what I need. But, I have a servlet class 'StarTeamServer' that has a class level attribute 'ArrayList<UserAccessDetailObject> getRequestedUsersSecurityAccessDetailObjectList()' where the Array contains numerous entries of a bean class 'UserAccessDetailObject'. That is the reason I was using StarTeamServer stServer = (StarTeamServer) session.getAttribute("STServerBean") to return an object type and not a single String value. Once I have a StarTeamServer stServer from session.getAttribute("STServerBean") I can stServer.getRequestedUsersSecurityAccessDetailObjectList() and loop through each UserAccessDetailObject for the updated user entries.

    This is the part the is no being explained well. I have no idea what all this is for and why it even exists. Please explains.

    I can loop the contents of the ArrayList using a forEach loop or a Java script loop in my JSP. I can write the <td>data with c:out or jsp:getProperty . But, somewhere I need a setter type code in my JSP for each <td> attribute in the loop of the Array.

    Again, why?

    You're giving us a lot of details on how to do things that sound odd and very unconventional (hence our skepticism) but not a whole lot of information on what you are actually trying to accomplish.

    Whatever that is is likely to a lot simpler than what you are laying out here.
     
    Nick Richard
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am not an expert on MVC web development so my post communications are probably lacking the correct verbiage or syntax. I do have a form element that communicates data back and forth between the JSP and the action class. The JSP form needs to have the ability to allow the user to update an input <td> in a forEach loop that displays an HTML table. The form submits and sends data back to the action servlet where I grab the object from the session, parse the Array and update the database.

    <jsp:useBean id="STServerBean" type="com.bis.alm.model.StarTeamServer" class="com.bis.alm.model.StarTeamServer" scope="session" />

    <Form method="post" name="formToSubmit" id="formToSubmit" action="ControllerSubmitRequest">

    if (doSubmit == false)
    {
    alert("Please enter the information for the Red highlighted fields!");

    return false;
    }
    else
    {
    document.formToSubmit.submit();
    }

    I am not a javascript expert either. Can you assist with the syntax for, javascript, JSTL, jsp:setProperty or which ever, to function as a setter method in the JSP within the forEach loop? It needs to update an indexed loop that is in a server class.

    <c:forEach var="userAccessDetailObject" items="${STServerBean.requestedUsersSecurityAccessDetailObjectList}" varStatus="theCount">
    <tr class="authorizationTable">
    <td><input type="text" name="serverNameID_${theCount.count}" value="${userAccessDetailObject.serverName}" /></td>
    <td><input type="text" name="serverURLID_${theCount.count}" value="${userAccessDetailObject.serverURL}" /></td>
    <td><input type="text" name="serverPortID_${theCount.count}" value="${userAccessDetailObject.serverPort}" /></td>
    <td><input type="text" name="serverProjectNameID_${theCount.count}" value="${userAccessDetailObject.serverProjectName}" /></td>
    <td><input type="text" name="serverGroupNameID_${theCount.count}" value="${userAccessDetailObject.serverGroupName}" /></td>
    <td><c:out value="${theCount.count}"></c:out></td>
    </tr>
    </c:forEach>

    To the best of my limited knowledge this loop will only display the data. If the user updates the input field I need a setter type method structure to update the iteration in the loop to submit back to the action class.

    Again, any suggestions would be a huge benefit and I thank you for your time.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nick Richard wrote:
    To the best of my limited knowledge this loop will only display the data.

    This is true, and that is what you want.

    If the user updates the input field I need a setter type method structure to update the iteration in the loop to submit back to the action class.

    Here is the disconnect. Once the page has been sent to the browser and the user is able to interact with it, the JSP is done. It's dead. Gone. And there are no zombies to take its place.

    To get the input that a user types into a form field, you do not try to use JSP set actions (you can't, the JSP is as dead as yesterday) you submit the form to the servlet and use the getParameter() class of methods to get the values.
     
    Dave Tolls
    Ranch Hand
    Posts: 2504
    27
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nick Richard wrote:But, I have a servlet class 'StarTeamServer' that has a class level attribute 'ArrayList<UserAccessDetailObject> getRequestedUsersSecurityAccessDetailObjectList()' where the Array contains numerous entries of a bean class 'UserAccessDetailObject'. That is the reason I was using StarTeamServer stServer = (StarTeamServer) session.getAttribute("STServerBean") to return an object type and not a single String value. Once I have a StarTeamServer stServer from session.getAttribute("STServerBean") I can stServer.getRequestedUsersSecurityAccessDetailObjectList() and loop through each UserAccessDetailObject for the updated user entries.


    Is StarTeamServer actually a Servlet, as implied by the first sentence here?
    If so, does it have any member variables declared, whether static or not, because that's what it sounds like with that List<>?
    If that's the case then that needs to be sorted out.
    Servlets shouldn't have attributes.

    That's part 1.

    Part 2 is, are you actually setting this servlet in the session, as implied by the second sentence?
    That would need fixing as well, though it might get sorted out when you sort out the flow from your HTML form to the JSP.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!