is it possible to add values to an ArrayList instead of using a HashMap
something like:
<jsp:useBean id="animalList" class="java.util.ArrayList" />
<c:set target="${animalList}" value="Sylvester"/>
<c:set target="${animalList}" value="Goofy"/>
<c:set target="${animalList}" value="Mickey"/>
<c:forEach items="${animalList}" var="animal">
${animal}<br>
</c:forEach>
now getting the error:
javax.servlet.jsp.JspTagException: Invalid property in <set>: "null"
thx
JSTL is not designed to do this kind of stuff. This really belongs in the business logic which is (in)directly to be controlled by a servlet class.
Create a servlet which does like:
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
List<String> animals = new ArrayList<String>();
animals.add("Sylvester");
animals.add("Goofy");
animals.add("Mickey");
request.setAttribute("animals", animals);
request.getRequestDispatcher("/WEB-INF/animals.jsp").forward(request, response);
}
Map this on an url-pattern of /animals.
Now create a JSP file in /WEB-INF/animals.jsp (place it in WEB-INF to prevent direct access):
<c:forEach items="${animals}" var="animal">
${animal}<br>
</c:forEach>
No need for jsp:useBean as servlet has already set it.
Now call the servlet+JSP by http://example.com/context/animals.
To do add() to a List or others methods from Map, Set, etc... You have to use a unusable variable.
<jsp:useBean id="list" class="java.util.ArrayList"/>
<c:set var="noUse" value="${list.add('YourThing')}"/>
<c:out value="${list}"/>
The above code is not working.
Following are the lines of code that has to be placed in file animals.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:forEach var="animal" items="${animals}">
<c:set var="animalName" value="${animal}"/>
<c:out value="${animalName}"/>
</c:forEach>
Related
enter image description hereI have a table of users data which have been retrived below using JdbcTemplate of Spring:
List<UserDetailsBean> userdetails = UserDetailsDaoObj.getallUserDataDetails(u.getId());
these details needs to be displyed in the jsp so im setting the object userdetails as below:
modelAndView.addObject("userdetails",userdetails);
I'm not able to retrieve the details in JSP the code used in JSP is below:
<c:forEach var="user" items="${userdetails}"><tr><td>${user.getId()}</td><td>${user.getAddress()}</td><td>${user.getCity()}</td><td>${user.getCountry()}/td></tr></c:forEach>
You just need to access properties of UserDetails like this
<c:out value="${user.id}" />
<c:out value="${user.address}" />
assuming that you have standard accessors method present in your class
You need to do something like this in JSP:
<c:forEach items="${userdetails}" var="user">
<tr>
<td>User ID: <c:out value="${user.id}"/></td>
<td>User address: <c:out value="${user.address}"/></td>
</tr>
</c:forEach>
You need to use JSTL tags to fetch values, In your case it is <c:out>.
Also try accessing elements using field name and not getters()
<c:forEach var="user" items="${userdetails}">
<tr>
<td><c:out value="${user.id}"/></td>
<td><c:out value="${user.country}"/></td>
</tr>
</c:forEach>
Well from what you posted its not really clear what your issue might be , but an obvious error , is what the others posted in their answers.
Note that * in order to operate properly , you have to place this line on top of your JSP.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Also the same for the jstl library. It must exist in your classpath.
link
I'm working on a legacy Tomcat server that is no longer supported by the software developer that provided it. web.xml doesn't tell me what version of JSP I'm working with, but all the .class files are Java 1.5.
I have a customer who is upset, because the system has html boxes that are auto-populated by Java, and it fills it straight the the (unordered) results of an XML database query. I'm trying to sneak a bit of code into the JSP for the Selector to sort the list of fields before they are populated, but it doesn't work and I don't understand why.
The (abridged) relevant code:
<%# page import="src.explorer.ObjectStateFactory"%>
<%# page language="java" %>
<%# taglib uri="/WEB-INF/c.tld" prefix="c" %>
<jsp:useBean id="ExplorerViewContext" scope="session" type="src.explorer.ExplorerViewContext"/>
...
<c:forEach var="nc" items="${ExplorerViewContext.networkControllers}">
<c:choose>
<c:when test="${nc.name == ExplorerViewContext.networkControllerSelection.name}">
<option value="<c:out value="${selectAction}${nc.objectKey}"/>" selected><c:out value="${nc.name}"/></option>
</c:when>
<c:otherwise>
<option value="<c:out value="${selectAction}${nc.objectKey}"/>"><c:out value="${nc.name}"/></option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
The relevant code with my additions:
<%# page import="src.explorer.ObjectStateFactory"%>
<%# page import="java.util.*"%>
<%# page language="java" %>
<%# taglib uri="/WEB-INF/c.tld" prefix="c" %>
<jsp:useBean id="ExplorerViewContext" scope="session" type="src.explorer.ExplorerViewContext"/>
...
<%
final Comparator<src.explorer.XmldbObjectState> NC_ORDER = new Comparator<src.explorer.XmldbObjectState>(){
public int compare(src.explorer.XmldbObjectState nc1, src.explorer.XmldbObjectState nc2){
return nc1.getName().compareTo(nc2.getName());
}
};
List myList = ExplorerViewContext.getNetworkControllers();
java.util.Collections.sort(myList,NC_ORDER);
%>
<c:forEach var="nc" items="${myList}">
<c:choose>
<c:when test="${nc.name == ExplorerViewContext.networkControllerSelection.name}">
<option value="<c:out value="${selectAction}${nc.objectKey}"/>" selected><c:out value="${nc.name}"/></option>
</c:when>
<c:otherwise>
<option value="<c:out value="${selectAction}${nc.objectKey}"/>"><c:out value="${nc.name}"/></option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
Basically, I'm trying to grab the list and sort it before it gets sent to the HTML. The problem is, myList always comes up empty, and I don't understand why. I'm guessing that ExplorerViewContext.networkControllers in the original code is calling the getNetwrokControllers() method on an instance of ExplorerViewContext, yes? Why can't I do the same thing in a scriptlet and reformat the output a little before serving it?
You should not add scriptlet code to code that is already using JSTL only. The better thing to do would be to edit the bean class src.explorer.ExplorerViewContext to sort the insides for you automatically.
In any case, the reason ${myList} does nothing in the JSTL is that variables created in scriptlets (i.e. between <% and %>) do not exist for JSTL. To get a variable to exist in JSTL you have to create it in JSTL, or set it in the page context, or it has to be in the session or the request. Generally you put it in the session or request in a servlet.
In this case, your list is in the bean, so it would be better to just edit the bean class to sort the list. But you could set the variable into the page context here so that JSTL can use it:
<%
...
List myList = ExplorerViewContext.getNetworkControllers();
java.util.Collections.sort(myList,NC_ORDER);
pageContext.setAttribute("myList", myList); //set in pageContext so JSTL can see it
%>
<c:forEach var="nc" items="${myList}">
As far as the code being "legacy" adding scriptlets to it would make it even more legacy. The fact is, this code is more modern than the modifications you were trying to add. But only slightly so, since using <jsp:useBean> is an obsolete way of using beans.
I want to create an arraylist of objects in JSP.
And after that, want to loop through the list objects.
can some one please help me in creating it.
Create the ArrayList at servlet set it as attribute, and iterate it on JSP using <c:forEach>
Servlet
List<Foo> list = new ArrayList<Foo>();
list.add(foo1);
list.add(foo2);
list.add(foo3);
request.setAttaribute("fooList", list);
request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
hello.jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach items="${list}" var="foo">
<tr>
<td><c:out value="${foo.name}" /></td>
<td><c:out value="${foo.age}" /></td>
</tr>
</c:forEach>
Note: name and age are two properties of Foo with proper accessor methods
I'm writing a web app that uses a servlet to maintain an ArrayList of VideoData objects (these just contain basic information about movies like the title, type of movie, etc).
The servlet puts this List in the request's scope and forwards both the request and response to a jsp (only part of the servlet code is shown here):
public class VideoServlet extends HttpServlet {
private ArrayList<VideoData> library = new ArrayList<VideoData>();
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
try {
// put ArrayList in Request's scope
request.setAttribute("the_table", library);
request.getRequestDispatcher("/listvideos.jsp").forward(request,
response);
...
The listvideos.jsp is shown below. I'm getting a Tomcat error stating that the uri for the JSTL cannot be resolved. I've used EL in other parts of my jsp code without having to have any special import line like this, and I'm not sure if JSTL is still the preferred way to solve this type of problem while still trying to adhere to MVC2 and keeping all the Java code in the Servlet. Can anyone point me in the right direction here? Ideally I'd like a pure EL solution, if that's possible.
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
'http://www.w3.org/TR/html4/loose.dtd'>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Cattle Drive Assignment Servlets-4: Videos</title>
</head>
<body>
<h1>Cattle Drive Assignment Servlets-4: Videos</h1>
<form method='post' action='/videos/VideoServlet'>
Add a video
<br>
<br>
<table border="1">
<tr>
<th>Title</th>
<th>Star</th>
<th>Type</th>
<th>VHS</th>
<th>DVD</th>
<th>Description</th>
</tr>
<c:forEach items="${the_table}" var="movie">
<tr>
<td>${movie.getTitle()}</td>
<td>${movie.getStar()}</td>
<td>${movie.getType()}</td>
<td>${movie.inVHS()}</td>
<td>${movie.inDVD()}</td>
<td>${movie.getDesc()}</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
Your code looks basically correct. Looks like the error you're seeing indicates that the JSTL taglibs cannot be found in the classpath. Please make sure that jstl.jar and standard.jar are in your war's WEB-INF/lib folder.
I have been looking how to implement a thing in struts2 jstl but it is impossible for me to find the way.
When I load the jsp page from the action, I have a list of String lists.
I want to create as divs as elements have the list, but inside every div, I want to create as links as the third element of the sub-list.
So I use the s:iterator tag to parse the list. But I don't know how to iterate "${item[2]}" times inside the first iterator.
The code would be something like this:
<s:iterator value="functions" var="item" status="stat">
<span class="operation">${item[1]}</span>
<div id="${item[0]}">
<s:for var $i=0;$i<${item[2]};$i++>
Link $i
</s:for>
</div>
</s:iterator>
Where I have put the s:for tag is where I would like to iterate "${item[2]}" times...
Anyone can help me?
Thank you very much in advance,
Aleix
Make sure you've got the JSTL core library in scope in your JSP page:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
And simply use <c:forEach>. Something like this:
<c:forEach var="i" begin="0" end="${item[2] - 1}">
Link ${i}
</c:forEach>
You should use List of Map if appropriate, example :
Action class
// List of raw type Map
private List<Map> functions = Lists.newArrayList(); // with getter
#Override
public String execute() {
// loops {
Map map = Maps.newHashMap();
map.put("id", id);
map.put("operation", operation);
map.put("count", count); // count is int/Integer
functions.add(map);
// }
return SUCCESS;
}
.jsp
<s:iterator value="functions">
<span class="operation">${operation}</span>
<div id="${id}">
<s:iterator begin="0" end="count - 1" var="link">
Link ${link}
</s:iterator>
</div>
</s:iterator>
or with <s:a /> (example)
<s:a action="action_name" id="%{link}" anchor="%{link}">Link ${link}</s:a>
output
<a id="[id]" href="/namespace/action#[anchor]">Link [link]</a>
See also
Struts2 Guides -> Tag Reference -> s:iterator