Need to display data on a jsp page using struts 1.3 - java

I am trying to read data from a table and display it to the user .
Can anybody tell how to do it using struts 1.3 ?

Write an class extending Struts' Action class. This class pulls the data from Database as a List. Pass this data as request attribute, request.setAttribute("myList", list). Return "success".
In your struts-config.xml, map this Action class to a JSP on "success". The request will be forwarded to the JSP.
In the JSP, get the list from request by request.getAttribute("myList"). Iterate through the list and print the List.
You need to study this: http://struts.apache.org/1.x/userGuide/index.html

(Edit: Just noticed this is a 2 year old question)
Don't use the struts tags unless you NEED to. This can be accomplished with jstl/el.
So on your Action class you would have something like this:
List<Map<?, ?>> listOfHashMaps = new ArrayList<Map<?, ?>>();
request.setAttribute("listOfHashMaps", listOfHashMaps);
In your jsp:
<c:forEach var="hashMap" items="listOfHashMaps">
${hashMap[someInteger]} <%-- To get the value associated with 'key' --%>
</c:forEach>
You can also access the keys/values with:
${hashMap.key}
${hashMap.value}
Respectively.

Related

Sending data to another JSP file from action class

The main page has link to create new record and one to show all the existing records.
On the create_new_record page I am writing all the data to a file in an action class method called saveRecords and populating a List<Records> in retriveRecords methods.
My action class code:
public class MyRecordes{
List<RecoredInfo> recoreds= new ArrayList<RecoredInfo>();
}
I have getters and setters for the same records in my action class(I am using Struts 2),
but on the main page when I click to show all records (which shows a different JSP page), nothing is displayed. Do I have to use servlets and/or doGet, etc. methods?
EDIT:
Adding code for showList.jsp:
<table>
<s:iterator value="arrayList" status="status">
<tr>
<td><s:property value="firstName"/> <s:property value="lastName"/>
</td>
</tr>
</s:iterator>
</table>
My action class has an arraylist named arrayList and I am using getters/setters to set the value.
The different action will populate the list and return a result of the different JSP page. In the different JSP you can show records using
iterator
Iterator will iterate over a value. An iterable value can be any of:
java.util.Collection, java.util.Iterator, java.util.Enumeration,
java.util.Map, or an array.
Whatever you show on the JSP the data should be bound to the beans properties that you can retrieve via OGNL expression and written to the JSP output.
OGNL
OGNL is the Object Graph Navigation Language (see
commons-ognl for the full
documentation of OGNL). Here, we will cover a few examples of OGNL
features that co-exist with the framework. To review basic concepts,
refer to OGNL Basics.
If you are using Struts2 you don't need servlets and/or doGet etc. methods. Struts2 framework implements MVC pattern that you can follow while writing your web application. If you are new to the framework, then better get started from the Tutorials.
Tutorials
The framework documentation is written for active web developers and
assumes a working knowledge about how Java web applications are built.
For more about the underlying nuts and bolts, see the Key
Technologies Primer.

Can not get select list back in struts Action class in java

I have a list i take it from DB and populate to the page using this code
DynamicComboCM allPartners = new DynamicComboCM();
allPartners.setAllRecommendedList(daoClass.getAllPartners(ds));
request.setAttribute("allPartners", allPartners);
Here DynamicComboCM is bean that contains the property allRecommendedList and i populate this list from DB using daoClass which returns an ArrayList then i set it to request and get it on jsp page like this.
<html:select styleId="partnerListForRecommended" name="Bean2"
property="bean2Property" size="15" multiple="true">
<html:optionsCollection name="allPartners"
property="allRecommendedList" label="label" value="value" />
</html:select>
i get the list on jsp as expected, on jsp user moves some records from this list to another list and in this way this list reduced by size as some recoreds had move to other list. now when i press save button i m not getting this list,
can any one please help me how to get this list again in jsp, i tried it using request.getAttribute("allPartners"); but not working.
Thanks in advance.
One of solution is to put list allPartners into session.
to set
request.getSession().setAttribute("allPartners", allPartners);
to get when needed
request.getSession().getAttribute("allPartners");
Another solution (using request scope) is to get allPartners from database again (better from the cache).

how to add <div> tag data to arraylist in java

i want pass data /innerHTML to the another jsp page ,so how could i pass it ?is it possible to use arraylist or any other way to pass this large data to another page?
Is it possible add tag data to the arraylist in javascript?or how can we pass div tag innerhtml to the next page in jsp?
i want pass data /innerHTML to the another jsp page
This seems wrong approach to me,because the main adavantage of Jsp is actually writing HTML on demand,where ever you need it.
Pass the essesntial data which decides the html to the jsp using the session or request but passing html is a bad idea.
Instead of passing the HTML view data, just pass what needs to be displayed and let the target JSP decide how to render it. For example, if you wanted the target JSP to print a welcome message for the logged-in user; then instead of passing the HTML
target.jsp?msg=<div><p>Welcome <b>John</b></p></div>
You would pass a request attribute named user as
request.setAttribute("user", "John");
which can now be accessed in your target JSP (using EL) as
<c:if test="${not empty user}">
<div><p>Welcome <b>${user}</b></p></div>
</c:if>
In your case, your Cart along with its List<CartItems>, should be stored in HttpSession. This way the shopping data remains accessible to any JSP/Servlet that needs to update the cart.
Wrap your div tag div tag. and get the html tags from ouside wrapper div tag.
<div id="wrapper">
<div id="what_you_want">
some contents...
</div>
</div>
$("#wrapper").html() - It contains whole html code in wrapper div.
Try some Javascript. If you really insist on using kinda abnormal methods (I do too sometimes), you can create an input field of type hidden and put the cart info into that input as a delimited string.

How do I pass information from a servlet to a JSP page

Is it possible to have a servlet that contains an object (an ArrayList in this case) that then does the equivalent of displaying a jsp page and passing that object to the jsp page. In this case the ArrayList contains database results an I want to iterate through and display the results on the JSP page.
I am not using any MVC framework, is it possible to do this with the basic Servlet/JSP architecture.
Yes.
in the servlet call request.setAttribute("result", yourArrayList);
then forward to the jsp:
getServletContext().getRequestDispatcher("your.jsp")
.forward(request, response);
using JSTL, in the jsp:
<c:forEach items="${result}" var="item">
...
</c:forEach>
If you don't want to use JSTL (but I recommend using it), then you can get the value using request.getAttribute("result") in the JSP as well.
Alternatively, but not recommended, you can use request.getSession().setAttribute(..) instead, if you want to redirect() rather than forward().
You can pass objects to jsp's by embedding them within the Request.
request.setAttribute("object", object);
and within the jsp:
request.getAttribute("object");
You can put it using request.setAttribute("myobj", myObj); see javadoc
If you are trying to make some kind of "component" then it's better to convert the JSP page into a custom tag. Here is excellent article about that: http://onjava.com/pub/a/onjava/2004/05/12/jsp2part4.html

JSP - Saving a collection

[Warning] I'm new to JSP/Struts/JSTL. This is probably a newbie question :)
I have a form that contains a collection:
public class ServiceForm extends AbstractForm
{
private List<SrvDO> allSrv = new ArrayList<SrvDO> ();
}
I can see the object data correctly in my form using the JSP. The page displays 5 input box with the data from the database correctly:
<c:forEach items="${serviceForm.allSrv}" var="srv">
<html:text name="srv" property="nbDays"/>
</c:forEach>
<html:submit/>
But when I press the submit button, the form does not contains the updated data for "nbDays". I still see the same data as it was shown before the update. What am I missing that says to struts: for each srv, update the "nbDays" data?
Found the answer on the spring forum:
Your form:input tag doesn't and
shouldn't know anything about the fact
that it is used inside another tag.
That is why you need to include the
index.
So the solution is:
<html:text property="allSrv[${srvSta.index}].nbDays"/>

Categories

Resources