0

I initially wrote a .jsp connected by Struts 1.1 to pull some data from a database using scriplets:

<%  Map<String, myObject> map = MyClass.returnMap();

    for(Map.Entry<String, myObject> entry : map.entrySet()) {
        myObject element = entry.getValue();
        out.println("<tr>");
        out.println("<td><input type=\"checkbox\" name=\"objects[]\" value=\"" + entry.getKey() + "\"/>" + entry.getKey() + "</td>");
        out.println("<td>" + element.property1() + "</td>");
        out.println("<td>" + element.property2() + "</td>");
     }
%>

So I end up with a table with a checkbox, object name, and two of the element's properties as the columns, with a row for each entry in the Map returned by returnMap().

Now, I want to be able to check however many checkboxes inside the generated table, and then click a button to send a list of all the checked checkboxes to a servlet to perform some server-side calculations depending on the checkboxes selected.

One problem is that the said "submit" button is outside the table, in a separate div (used for a fixed position header). Could I just wrap a form around the entirety of the div containing the button, and the table?

I am starting out in web development, and have done some tutorials on Servlets and I understand the basic concepts. I have seen that it's generally bad practice to use scriptlets for business logic in the jsp, so I am considering generating the table through my Servlet instead. However, I also want to be able to use the elements generated by the Servlet in another Servlet method (if that makes sense).

My thought process was:

1) .JSP loads through Struts
2) .JSP receives table from Servlet
3) When submit button is clicked, sends list of checked checkboxes back to the Servlet
4) Servlet uses list of checkboxes and performs some business logic
5) .JSP refreshes with updated table

Is this a viable process? Or is there a better way to do this?

I have to access the .jsp (through Struts), not the servlet url most of the tutorials use

Song Gao
  • 666
  • 4
  • 14
  • You say you are using Struts. First is it Struts1 or Struts2 ? They are really different frameworks only sharing the name. And the way of passing objects from the controller to the JSP is not exactly the same. – Serge Ballesta Jul 24 '14 at 16:45
  • Sorry, updated the question. I am using Struts 1 – Song Gao Jul 24 '14 at 16:46

2 Answers2

0

I suggest to avoid Scriplet instead use JSP Standard Tag Library and Expression language that is easy to user and less error prone.

Map.Entry contains getKey() and getValue() methods to access key and value from Map Entry.

Simply set the return value as request attribute in Servlet and then you can access it in JSP using JSTL.

Sample code:

Servlet:

request.setAttribute("map",MyClass.returnMap());
// forward the request to the jsp

JSP:

<c:forEach var="entry" items="${map}">
    <c:out value="${entry.key}"/>:<core:out value="${entry.value}"/>
</c:forEach>

read more...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Thanks, I'll look into JSTL. As for my other question about the "submit" button outside my generated table, should I just wrap a form around the button and the table? – Song Gao Jul 24 '14 at 16:20
  • yes you can wrap it. add form tag outside the table tag. – Braj Jul 24 '14 at 16:21
0

Struts1 is rather outdated now and is officially no longer supported by Apache foundation. From Apache Struts site : The Apache Struts Project Team would like to inform you that the Struts 1.x web framework has reached its end of life and is no longer officially supported. So if you are beginning with it, you should considere using another framework such as Struts2 or Spring MVC.

But the resources are still present and you should use Struts 1 User Guide.

So you should have one Action for displaying the JSP. In the JSP, the form must include all the input fields **and* the submit one (possibly across many divs). You should have another Action to treat the inputs and an ActionForm to carry the data between first Action and the JSP at render time, and to give the input values to the second one at submit time.

Do not forget that Struts1 comes with a taglib that could help you to avoid as much scriptlet as possible from you JSP, but IMHO, you should use JSTL when possible instead of Struts specific tags when they do same thing : migration will be easier if you later want to use another framework.

Normally it is the job of second action to call business methods to do intelligent things with input values, and it is recommended to do a redirect after a submit (POST) to avoid submitting again if user refreshes its browser or clicks on back arrow button of browser.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252