Problem: When I open page view source this data is shown on browser
Hello there jsp
<c:forEach var="student" items="2abcnullmca">
<c:out value= ""/>
</c:forEach>
<c:out value= "abc" />
And on browser it does not show c:out value in spring I have used model.addAttribute() to show data
Actually I forgot to include jstl tag thanks for looking into it but.
Here is the Tag -
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
Related
I am trying to create an empty in my jstl page is it possible to create an empty array and assign some value to that array in the jstl page itself .?
Your question is quite unusual. You want to do scripting without a scriplet. The only way that I could think of is to use the useBean tag to create an ArrayList. Its add method returns a boolean. That is why I used empty if tags to insert the elements. The forEach tag is not necessary. I used it to display the elements.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="myList" class="java.util.ArrayList" />
<c:if test = '${myList.add("My first element")}'>
</c:if>
<c:if test = '${myList.add("My second element")}'>
</c:if>
<c:forEach var="element" items="${myList}">
${element}
</c:forEach>
Output:
My first element My second element
You can create an array with some values using c:set:
<c:set var="array" value="${['item a','item b','item c']}" />
<c:forEach var="item" items="${array}">
<c:out value="${item}" /><br />
</c:forEach>
I recently migrated an ANT project to Maven. After doing all the dependecnies and getting the project error free. I see this warning on in Eclipse:
The tag handler class for "c:if" (org.apache.taglibs.standard.tag.rt.core.IfTag) was not found on the Java Build Path
I see the same warnings for c:import, c:out c:set as well.
I do have the tag:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
added in the JSP. And I have the JSTL 1.2 dependency added in the pom file. Can you tell me how to get rid of these warnings?
I had the same error. To fix it, I just added and saved the following in my pom.xml.
<!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.1</version>
</dependency>
In some older Eclipse versions, you may need to modify the "<c:if..." line of code, remove a character or more, save it then put it back as it was and save the file again. This in some case get rid of the warning.
My code used <c:set and <c:if as the following example. No warning displays. Perhaps, you may need to update your Eclipse. My Eclipse is IDE for Enterprise Java Developers Version: 2019-09 R (4.13.0).
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<%# attribute name="normalPrice" fragment="true" %>
<%# attribute name="onSale" fragment="true" %>
<%# variable name-given="name" %>
<%# variable name-given="price" %>
<%# variable name-given="origPrice" %>
<%# variable name-given="salePrice" %>
<head>
<title>Tag Example</title>
</head>
<table border="1">
<tr>
<td>
<c:set var="name" value="Hand-held Color PDA"/>
<c:set var="price" value="$298.86"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="4-Pack 150 Watt Light Bulbs"/>
<c:set var="origPrice" value="$2.98"/>
<c:set var="salePrice" value="$2.32"/>
<jsp:invoke fragment="onSale"/>
</td>
<td>
<c:set var="name" value="Digital Cellular Phone"/>
<c:set var="price" value="$68.74"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="Baby Grand Piano"/>
<c:set var="price" value="$10,800.00"/>
<jsp:invoke fragment="normalPrice"/>
</td>
<td>
<c:set var="name" value="Luxury Car w/ Leather Seats"/>
<c:set var="origPrice" value="$23,980.00"/>
<c:set var="salePrice" value="$21,070.00"/>
<jsp:invoke fragment="onSale"/>
</td>
</tr>
</table>
<body>
<c:set var = "salary" scope = "session" value = "${2000*2}"/>
<c:if test = "${salary > 2000}">
<p>My salary is: <c:out value = "${salary}"/><p>
</c:if>
</body>
If the tag works actually fine, it is just a false negative from Eclipse. In that case you could force a new check editing the tag name and then setting it back to the right name.
So, for instance, click on your "c:if" tag, remove a single character and then re-enter it (exactly as it was before). Save it. In my case that was enough to get rid of the warning.
i was familiar with JSTL before and thymeleaf is something new to me.
What i want is to check if the current URL is the index page (/index), if so then make visible a div.
Here is a JSTL equivalent example
<c:set var="url" value="${ pageContext.request.requestURI }" />
<c:if test="${url=='/example/WEB-INF/views/inbox.jsp'}">
...
</c:if>
Try this:
<div th:if="${#httpServletRequest.requestURI == '/example/WEB-INF/views/inbox.jsp'}">
some content
</div>
I use dynamic include on a page:
<div class="top">
<jsp:include page="Header.jsp"/>
</div>
This is the important part in the Header.jsp:
<H4>
JSTL TAG Test: </br>
<c:if test="${sessionScope.username != null}" >
<c:out value="Hello, ${sessionScope.username}"/>
</c:if>
</h4>
The result on my main page source is:
<H4>
JSTL TAG Test: </br>
<c:if test=Swank != null >
<c:out value="Hello, Swank"/>
</c:if>
</h4>
I use this: <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> in the main page.
Does anybody know how can I use JSTL if I import it from another page?
Thanks, Zoltán
Add the same declaration
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
into your Header.jsp file.
I agree with the given answer, just with a slight modification:
<#include file="Header.jsp" %>
Notice the '%' to close the directive.
You can solve the problem by using JSP include directive
<%# include file="Header.jsp" %>
Because the content of the file given in the include directive is pasted as it is, in the place where the JSP include directive is used.
But in include action <jsp:include> At runtime, the included file will be ‘executed’ and the result content will be included with the soure JSP page.
Updated:
In JSTL condition should be written inside double quotes.
<c:if test="${sessionScope.username} != null"></c:if>
Add double quotes in your condition.
I have a jsp page with a List<Object> as the #ModelAttribute. However, there are no <form:form> tags in the page. All I'm doing is print the contents of the List.
In my Controller.java, I'm binding an error by doing:
result.rejectValue("", "NOT_LOGGED_IN", "You should Login first") ;
But since I dont have a form in my jsp, I'm not able to access the error with:
<form:errors path="" /> <br/>
Please tell me how to access the error (or what I'm doing wrong).
In your controller:
model.addAttribute("errors", result.getAllErrors());
In your JSP:
<c:forEach items="${errors}" var="error">
<%-- do want you want with ${error} --%>
<c:out value="${error.defaultMessage}" />
</c:forEach>
Associate global errors this way:
result.reject("NOT_LOGGED_IN", "You should Login first") ;
You can show the global errors in the jsp :
<form:errors cssClass="error" delimiter="<p/>" />
for any specific error
set in your code like-
model.addObject("errorMsg","username/password failed");
And show this error on jsp in this way:
<c:out value="${errorMsg}"/>
This way you would get your error on jsp.
there's a way to reference the error like
instead of
<form:form commandName="object">
<form:errors path="field"/>
</form:form>
best
Check if Expression Language evaluation is enabled in JSP(By default it is disabled).If not add below code to enable it.
<%# page isELIgnored="false" %>