How to get an item - java

JSP tag code is:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# attribute name="items" required="true" %>
${items[0]}
JSP code is:
<%# taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:input items="${form.items}"></t:input>
Maybe I forgot type of the attribute or something else? Why is the way to access values different in JSP and JSP tag?

The default type of attributes is java.lang.String. If you expect something else, specify the expected type:
<%# attribute name="items" required="java.util.Collection" %>
or
<%# attribute name="items" required="java.lang.Object" %>
for example.

Related

JSP adds HTML to the view but doesn't execute it to the user

I wanted to insert html code to a jsp page so I used normal spring controller populated my model with html items, then once I start to render the data on the view ,it show the user a row html tags rather than an actual elements like:
<p> <strong> Description:</strong></p>
I wanted to show the user an actual strong text not the tag itself ,anyone knows how to achieve that?
my view is like that:
<%# page isELIgnored ="false" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<c:out value="${Description}" escapeXml="false" />
</body>
</html>
any Idea how to solve it?
Edit : part of the code where I send the html :
model.addAttribute("Description", jobpost.getDescription()
.replace("<", "<")
.replace(">", ">")
.replace("&", "&")
.replace(""", "\"")
.replace("&apos;", "\\"));
Edit 2 : it finally worked guys It was a problem with the above code I forgot to insert ; at the end of & lt;
You can try wrapping the field with <b> or <h2>tag
<%# page isELIgnored ="false" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<b><c:out value="${Description}" escapeXml="false" /></b> // like this
</body><
/html>

How can be a JSP ".tag" file documented?

There is a JSP Tag file like this:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# attribute name="attr" required="true" type="java.lang.String" rtexprvalue="true" description="FOOO" %>
<%# attribute name="var" required="true" type="java.lang.String" rtexprvalue="false" description="BAAR" %>
<%# variable name-from-attribute="var" variable-class="java.lang.String" alias="attrValue" scope="AT_BEGIN" %>
<c:set var="attrValue" value="${requestScope[attr]}" />
It seems to be possible to document the attributes. How to add documentation to the tag itself?
The proper place to describe a tag and its properties (including attributes) is the Tag Library Descriptor. The description attribute of the attribute directive is IMHO a left-over "from the dark past" and does not have any real use nowadays.

Difference between <html:html></html:html> and <html></html>

Good day all,
I saw <html:html></html:html> from a jsp page in a java project.
Would like to ask what is the difference between these html tags.
Kindly advise.
The example code is as follow:
<%# taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%# taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html:html>
<head>
</head>
<body>
<!-- some html code here -->
</body>
</html:html>
<html:html> uses the struts-html tag library, where <html></html> is just plain old html.
You can read all about the struts-html taglib here.
both are same.html:html is struts 1 tag which is equal to basic HTML's html tag.

How to get an item from the String[] attribute in JSTL/JSP tag

In plain JSP I can get first item by EL ${form.items[0]}, but in a JSP tag the same expression throws the following exception:
javax.el.PropertyNotFoundException: Could not find property 0 in class
java.lang.String
The value of ${form.items} is [Ljava.lang.String;#315e5b60.
JSP tag code is:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# attribute name="items" required="true" %>
${items[0]}
JSP code is:
<%# taglib prefix="t" tagdir="/WEB-INF/tags"%>
<t:input items="${form.items}"></t:input>
Maybe I forgot type of the attribute or something else? Why is the way to access values different in JSP and JSP tag?
You need to specify the expeded type of the custom tag attribute. By default, it's java.lang.String, and the JSP container coerces the attribute to a string before passing it to your tag. It thus calls toString on your String array.
<%# attribute name="items" required="true" type="java.lang.String[]" %>
or
<%# attribute name="items" required="true" type="[Ljava.lang.String" %>
should do the trick. If neither does, using
<%# attribute name="items" required="true" type="java.lang.Object" %>
should, but it's less clear.

Passing an ArrayList from a Servlet to JSP

my model returns an arraylist of strings to servlet in the form
ArrayList<String> currentCustomer = model.getAllCustomers();
i want to pass this arraylist from the servlet to the jsp page. how do i do this? below is what i tried
req.setAttribute("currentCustomer", currentCustomer);
and in the jsp page, i want to use JSTL to loop over each value and display it. how do i do that? its frustrating me to no end. ive scoured the web but to no avail. any help is greatly appreciated.
here is the jsp code
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<body>
<div>
<c:forEach var="customer" items="currentCustomer">
${customer}
</c:forEach>
</div>
</body>
Let's make it work :)
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:forEach var="customer" items="${currentCustomer}">
<c:out value="${customer.name}" />
<c:out value="${customer.age}" />
</c:forEach>
P.S. jsp:useBean is another way to go...
P.P.S. I also made a correction in the taglib import. That's one of these hard-visible mistakes when you can look on two different entries and think they are the same :)
its allrite guys, i solved the problem.. thanks for your help..
apparently the code i was using was outdated (thanks internet!) i was writing this on the header:
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
while it should have been
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
It will be smt like
<c:forEach var="currentCustomer" items="${customers}">
${currentCustomer.name}
${currentCustomer.age}
</c:forEach>

Categories