I have this code in my jsp file:
<body>
<c:if test="${!isActivityVP && isBuy && isOwner}">
Hello
</c:if>
</body>
isActivityVP and isBuy and isOwner are all boolean values
when I visit the page, however, I got this in the html:
<body>
<c:if test="false">
Hello
</c:if>
</body>
What's wrong here?
You need to include jstlxxx.jar into WEB-INF/lib and add <%# taglib %> directive in JSP page.
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<body>
<c:if test="${!isActivityVP && isBuy && isOwner}">
Hello
</c:if>
</body>
Related
I have this code in JSP:
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%# taglib prefix="security" uri="http://www.springframework.org/security/tags" %> <%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: PC Date: 02.05.2022 Time: 15:20 To change this template use File | Settings | File Templates.
--%> <%# page contentType="text/html;charset=UTF-8" language="java" %>
<html> <head>
<title>Title</title> </head> <body> <h1>Dashboard</h1> <br>
<table>
<tr>
<th>Banners</th>
<th>Localization</th>
</tr>
<%--#elvariable id="banners" type="java.util.List<pl.coderslab.entity.Banners>"--%>
<c:forEach var="banner" items="${banners}">
<tr>
<td>Banner</td>
<td>${banner.street}</td>
<td> <a href="localhost:8080/dashboard/info/${banner.id}" >Wiecej informacji </a></td>
<td> <a href="localhost:8080/dashboard/edit/${banner.id}" >Edytuj </a></td>
<td> <a href="localhost:8080/dashboard/delete/${banner.id}" >Usun </a></td>
</tr>
</c:forEach>
</table>
</body> </html>
and also have controller:
#RequestMapping(value = "/dashboard/info/{id}", method = RequestMethod.GET)
public String showInfo(Model model, #PathVariable("id") int id){
List<Banners> banners = bannerDao.getBannerById(id);
model.addAttribute("banner", banners);
return "showInfo";
}
#RequestMapping(value = "/dashboard/info/{id}", method = RequestMethod.POST)
public String info(){
return "showInfo";
}
The problem is that these href's aren't redirecting. When i click on them nothing happens, as if they were simple buttons. I don't have an idea, what to do here?
1st edit:
Okay so answer from Halil Ibrahim Binol worked but now it shows me 404 error, even if I have #RequestMapping to this address
Did you try adding http prefix?
<a href="http://localhost:8080/dashboard/info/${banner.id}" >Wiecej informacji</a>
Or you can use;
Wiecej informacji
This will be use current page protocol. When you are in https page it will convert to https://localhost:8080/...
Edit:
Alternatively, when your code is running in same server. You can use;
Wiecej informacji
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<!-- jQuery -->
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<c:if test="${error}">
<script>
alert("You must have something...");
</script>
</c:if>
</body>
</html>
jsp file has
<c:if test="${error}">
<script>
alert("You must have something...");
</script>
</c:if>
spring controller is
#RequestMapping(value = "/sample/board.do", method = RequestMethod.POST)
public ModelAndView updateBoard(#ModelAttribute HistoryBoard historyBoard) throws Exception {
ModelAndView mv = new ModelAndView("redirect");
mv.addObject("error", true);
return mv;
}
and it make url http://localhost:8080/test/board/board.do?error=true
but c:if tag doesn't work
<c:if test="${not empty error}">
<h1>TEST</h1>
</c:if>
c:if not empty doesn't work too.
how to fix this tag?
The error is a parameter, not a scope variable.
<c:if test="${not empty param.error}">
<h1>TEST</h1>
</c:if>
I am trying to deploy and run the Hello World application using IntelliJIDEA 14.1.4
on Tomcat 8.0.14(Tomcat Server -> Local). The project was created using maven-archetype-webapp and later imported
to the IDE by specifying path to pom.xml.
Everything works fine as documented in the servlets tag info, but when I remove the following line:
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
and add it in my include.jsp, I encounter an:
org.apache.jasper.JasperException: /WEB-INF/jsp/hello.jsp (line: 18, column: 42) The attribute prefix [fn] does not correspond to any imported tag library
Project structure:
include.jsp
<%-- 'header' file that will be included in every JSP page ensuring the same definitions are included in all our JSPs. --%>
<%# page session="false"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
index.jsp:
<%# include file="/WEB-INF/jsp/include.jsp" %>
<c:redirect url="/WEB-INF/jsp/hello.jsp"/>
hello.jsp:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Application Home</title>
<style>.error {
color: red;
}
.success {
color: green;
}</style>
</head>
<body>
<form action="hello" method="post">
<h1>Hello</h1>
<p>
<label for="name">What's your name?</label>
<input id="name" name="name" value="${fn:escapeXml(param.name)}">
<span class="error">${messages.name}</span>
</p>
<p>
<label for="age">What's your age?</label>
<input id="age" name="age" value="${fn:escapeXml(param.age)}">
<span class="error">${messages.age}</span>
</p>
<p>
<input type="submit">
<span class="success">${messages.success}</span>
</p>
</form>
</body>
</html>
Question:
Is it a bad idea to add the line only once(include.jsp) so that I can use it where required rather than adding it in every JSP or is the error cause because of something else which I am missing?
Silly, as it may seem, I missed the include statement in hello.jsp which was causing the exception. Posting the answer because I want to keep this information here for future reference.
Missing statement inside hello.jsp
<%# include file="/WEB-INF/jsp/include.jsp" %>
hello.jsp:
<%# include file="/WEB-INF/jsp/include.jsp" %>
<!DOCTYPE html>
....
</body>
</html>
I have this structure in my JSP (template.jsp):
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="language" value="es" scope="session"/>
<fmt:setBundle basename="com.myweb.i18n.text"/>
<html>
<body>
<fmt:message key='login.label.username'/>
<jsp:include page='/home/header.jsp' flush='true'/>
<jsp:include page='/home/body.jsp' flush='true'/>
<jsp:include page='/home/footer.jsp' flush='true'/>
</body>
</html>
The fmt:message line works well (print a text label). But if I write this line in header.jsp or any other jsp included does not work.
I must write all tag lib lines in all jsp files?
There any way to include without repeat the firsts 4 lines in all my jsp?
Thanks!
Edit: example header.jsp
<div>
<fmt:message key='login.label.username'/>
<jsp:include page='/home/header-left.jsp' flush='true'/>
<jsp:include page='/home/header-center.jsp' flush='true'/>
<jsp:include page='/home/footer-right.jsp' flush='true'/>
</div>
put the lines in a jsp & include it in the current jsp i;e template.jsp as
<#include file="path/jspcontaininglibs.jsp"/>
<html>
<body>
<fmt:message key='login.label.username'/>
<jsp:include page='/home/header.jsp' flush='true'/>
<jsp:include page='/home/body.jsp' flush='true'/>
<jsp:include page='/home/footer.jsp' flush='true'/>
</body>
</html>
When I try to add jstl tags into my jsp I get this:
HTTP Status 500 - org.apache.jasper.JasperException: Unable to load class for JSP
JSP may look like this:
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<body>
<c:forEach items="${list}" var="post" >
<tr>
<td>${post.author}</td>
</tr>
</c:forEach>
</body>
</html>
I also tried to write
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
the result is the same.
Why does it happen?