Thanks in advance for taking the time to read!
I'm working on a web application on the NetBeans IDE 7.1, with Struts 1.3.10, Apache Tomcat 7.0.22, PostgreSQL 9.1 on Windows 7.
My question is the following:
I have a class called "Reminder", it's properties are: startDate, endDate and id. I want to get all the start and end dates from all the reminders in my jsp so I can highlight this range of days on a calendar at a sidebar of my web application.
In my login action I have the following code:
Reminder reminder = new Reminder();
ArrayList<Reminder> queryResults =
dbms.queryReminders(reminder);
Here I've gathered all the reminders from the database (The model function simply does a SELECT * from reminders and returns that)
Then I finish with:
request.setAttribute("reminders",queryResults);
return mapping.findForward(User);
Here I've set the collected results to an attribute I decided to call "reminders", this should have all the reminders I want.
Afterwards I get the data from view like this:
<bean:write name="reminders" scope="request"/>
But all I get is something like: [classes.Reminder#1d1ce11] (In this case, I have only inserted one reminder in the database. Also I have a class called Reminder in my package called classes, so I think it's referring to an object of class Reminder (the one I inserted on the DB)).
But what I want to do is to be able to obtain the startDate and endDate from that object, not just the object like that. Does anybody know how to access these properties from the jsp?
I've done a lot of research but usually what tutorials show you is a way to get the properties of classes with these bean tags, but here I have an array of objects of a certain class and I want their properties, so things are different.
I would really appreciate some guidance. Thank you!
That's because you stored a List (of type ArrayList) to the request on the following code:
request.setAttribute("reminders",queryResults);
So, in order to get each Reminder from the List, you will need to iterate through the list (in the JSP).
To iterate through a collection, you will use <logic:iterate> tag:
<logic:iterate name="reminders" id="reminder" scope="request">
ID: <bean:write name="reminder" property="id" />
Start Date: <bean:write name="reminder" property="startDate" />
End Date: <bean:write name="reminder" property="endDate" />
</logic:iterate>
Related
I'm passing List<Object[]> to my view i.e jsp. And want to populate my <form:select> tag.
Suppose I have List<Object[]> states
what I tried is
<form:options items"${states}" itemValue="0" itemLabel="1">
also
<form:options items"${states}" itemValue="states[0]" itemLabel="states[1]">
But no Luck coz java.lang.Object does not have properties like 0/1 or states[0]/states[1].
One way is, in Controller convert it to Map.
Can I directly use this in my jsp?
I know just a little bit Spring MVC, so i’m not sure that i’ve understand completely your question, but the first thing that i can see is that you are getting the object teaking it from the ModelMap using his name and not using his key
I have been using JSTL to fetch values from my database and display it in my jsp page using a similar code as shown below.
<sql:setDataSource
var="myDS"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb"
user="root" password="secret"
/>
<sql:query var="list_users" dataSource="${myDS}">
SELECT * FROM users;
</sql:query>
<c:forEach var="user" items="${listUsers.rows}">
<c:out value="${user.name}" />
<c:out value="${user.email}" />
<c:out value="${user.profession}" />
</c:forEach>
My team leader advised me that it is not a good practice to put queries in the jsp page directly. Since there is a database name and username and password in this code I'm not sure if this code implements proper security. I would like to know your thoughts on the matter, and if there is any alternative to do this using JSTL itself.
Since JSTL is executed on server side, there's no security hole because the query cannot be seen by clients of the application. There are other problems with this approach:
The query is not reusable. If you need it in other pages, you will need to repeat the query. When you have simple queries like this one you cannot check the problem, but this arises with more complex statements that needs parameters as well.
You're creating a connection manually per page! This will slow your application. Use a proper datasource for a database connection pool configured as a resource or programatically (C3PO or BoneCP, for example)
Move all your database code into proper Data Access Layer classes. And use <sql> for JSTL for learning purposes only, not for real world applications.
More info:
How to use JSTL sql tag
Retrieve values from JDBC and use JSTL tags to call the methods
Should a database connection stay open all the time or only be opened when needed?
You should really do your query in your java class, not in the jsp, like your team leader advised.
On the security side it doesn't really matter, all the code is available on the server, jsp or java. The sql tag shouldn't output that information in the generated page.
But really the question is more about the right use of the technologies:
jsp is used as a template, it should take data and show them to the end user. Some basic operation can be done life looping on data list or formating data, but this should be only specific to the view you want to make
java controler is used to recuperate data and configure the view as needed like which jsp to use and which data to send in that jsp
Please correct me if I am wrong. I am developing a small web application for learning purpose. I have a jsp in which a list of top scorers in the the game are to be displayed in a table. For that I wrote a ServletContextListener and in the contextInitialized() method I have set an attribute(LinkedHashSet) in the ContextScope, which is the list of top 10 scorers in the game. I think it can be accessed using EL. But how can I update this collection?
You can use request.getSession().getServletContext().getAttribute("your_attribute_name_here") and can access the LinkedHashSet, once you get it you can add/remove/update values in it and again set it back to put updated values like request.getSession().getServletContext().setAttribute("your_attribute_name_here", "update_LinkedHashSet"); As best of my knowledge using EL you can access it but can not put back the updated value in attribute.
Note: while accessing the attribute you will need the explicit type casting.
You can update attribute like :
<%((Set<String>)pageContext.getServletContext().getAttribute("set")).add("Second"); %>
<% Set<String> set = (Set<String>) pageContext.getServletContext().getAttribute("set"); %>
from servlet context
<c:forEach items="${set}" var="s">
<c:out value="${s}"/>
</c:forEach>
I am new to struts. I want to load a list of data in the session to a select tag <s:select> which equals to pure html <select><option>values..</option></select> . The data might be loaded from database and put them to a list. I looked for Internet. But it all didn't work for me. Please any one let me know how to do this or provide any link with working example.( including the action class,struts.xml and jsp page. most needed codes are enough.)
As long as you have the list of values in a java.util.List on the stack, you should be fine with something like this:
<s:select label="Some label"
list="yourList"
name="somName" />
You can find a sample here: http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/
I am not sure why you want to place List in the session?
Struts2 provides a clean way to put your request/response data in Valuestack and its OGNL system provides a very clean way to access those data from the value stack.All you need to have a list in your action class with its getter and setters and at UI use build in struts2 tag to access those data.Here is a simple code to accomplish this
Action Class
public Class MyAction extends ActionSupport{
private List<String> myList;
//getter and setter for myList
public String execute() throws Exception{
myList=new ArrayList<String>();
// fill the list
return SUCCESS;
}
}
At UI level you need to use S2 select tag like
JSP
<s:select label="MyList"
name="myList"
headerKey="-1" headerValue="Select Value"
list="myList"
/>
This is all you need to do. For mapping this in struts.xml its quite straight forwards and all you need to configure your action name and its respected class.Hope this will help you.
For more details about S2, i suggest to refer official doc.
Still if you want to put the list in session in your java class and want to access it in jsp here is the JSP code
%{#session.MyList}
Struts 2.3.1 Documenation
Can anyone help me to understand the usage of TypedProperty in websphere commerce?
ie,How to pass values from one jsp to other using TypedProperty without a command class.I would prefer to handle it in my client side itself without invoking Command class..can anyone help me to sort out it?
Typed property is usually used to pass values from controller commands to JSPs. If you just want to pass values from one JSP to another, create a form in your first JSP and submit it to the second.
If this is a form submit, set the values you need to pass in element. In the results jsp you can get those values using ${WCParam.xxx} .
FYI - To list out all the values in WCParam object try to print the below in JSP :
${WCParamValues}
We use typedProperty when we need to send anything from the command. For example, you give an order ID from the first JSP and want to get the final amount to be passed the result JSP. Here in the command we use the orderID from the request object -> Then we use the OrderAccessBean to get the OrderTotal -> then we set this to a TypedProperty object -> we then set this TypedProperty object to request properties using setRequestProperties() OOB method in a controller command.
Hope this makes it clear !
TypedProperty is a class in Java which can be compared to Hashmap in Java for better understanding. It is a name value pair combination.
I just wanted to understand the problem before answering further.
Why do you want to use TypedProperty in Jsp to pass the value from one jsp to another?
Are you importing the second jsp or including the second jsp to which you have to pass the values to?
If you are importing, you can use c:param tag to pass the values to the second jsp.
For included jsps, the values are already available in the second JSP.
Please include code snippets to explain your problem so that it can be answered clearly.
You can pass parameters from one jsp to another by using the following code snippet:
<c:import url="child.jsp">
<c:param name="name1" value="value1" />
<c:param name="name2" value="value2" />
<c:param name="name3" value="value3" />
</c:import>
Within the child.jsp you can read the parameters by using:
<c:out value="${param.name1}" />
<c:out value="${param.name2}" />
<c:out value="${param.name3}" />
A TypedProperty is nothing but a Wrapper to HashMap. So that's nothing to do here with passing values from one JSP to another JSP. Without invoking a command, you can't pass a Java object to another JSP.
And that is the very basic of Command Framework. I would prefer to go with the first answer.