suppose I have a header (which is common for all pages regarding one type of object).
<%# page session="false" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%# taglib prefix="header" uri="headerDir" %>
<%# taglib prefix="tabs" uri="tabDir" %>
<div id="content" class="inside>
<header:myHeader headerData="${myModel}">
<tabs:myTabs argument="${someArg}"
....
so In views that are shared for different objects now I am doing
<c:choose>
<c:when test="${myModel.type ==FIRST_TYPE}>
<header:myHeader headerData="${myModel}">
</c:when>
<c:otherwise>
<header:secondHeader headerData="${myModel}">
</c:otherwise>
<c:choose>
but I would like to avoid this choose, I am able to send this
as a parameter as I am passing arguments? so can I do something like the following
<%# page session="false" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%# taglib prefix="header" uri="headerDir" %>
<%# taglib prefix="tabs" uri="tabDir" %>
<div id="content" class="inside>
<header:myHeader headerData="${myModel}">
<tabs:myTabs argument="${someArg}" headerToUse="${myHeader}" //in some way pass the header?
....
Related
I would like to display formatted java.time.LocalDate in my JSP. Do you know any taglib to use for this?
For java.util.Date we were using <%# taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>. Does something similar for java.time.LocalDate exist?
Afsun's hints inspired me to create a quick solution.
Under /WEB-INF create directory tags.
Create tag file localDate.tag inside the tags directory.
Put bellow code into this tag file:
<%# tag body-content="empty" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# attribute name="date" required="true" type="java.time.LocalDate" %>
<%# attribute name="pattern" required="false" type="java.lang.String" %>
<c:if test="${empty pattern}">
<c:set var="pattern" value="MM/dd/yyyy"/>
</c:if>
<fmt:parseDate value="${date}" pattern="yyyy-MM-dd" var="parsedDate" type="date"/>
<fmt:formatDate value="${parsedDate}" type="date" pattern="${pattern}"/>
Go to the JSP file in which you want display the java.time.LocalDate.
4.1. Add taglib directive <%# taglib prefix="tags" tagdir="/WEB-INF/tags" %> at the top of the file.
4.2. Use the localDate tag as follows:
<tags:localDate date="${yourDateToPrint}"/>
<tags:localDate date="${yourDateToPrint}" pattern="${yourPatternFormat}"/>
You can do this by fmt:parseDate.Try following:
<fmt:parseDate value="${dateForParsing}" pattern="yyyy-MM-dd" var="parsedDate" type="date" />
<fmt:formatDate value="${parsedDate}" var="newParsedDate" type="date" pattern="dd.MM.yyyy" />
I hope this help you.More information
One solution would be to use the annotation #XmlJavaTypeAdapter(LocalDateAdapter.class) on your javabean:
#XmlJavaTypeAdapter(LocalDateAdapter.class)
public LocalDate getLoanDate() {
return loanDate;
}
I just ran into the following code:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<form:form method="post" commandName="credentials" htmlEscape="true">
<label for="username"><spring:message code="screen.welcome.label.netid"/></label>
<spring:message code="screen.welcome.label.netid.accesskey" var="userNameAccessKey"/>
<form:input cssClass="required" cssErrorClass="error" id="username" tabindex="1" accesskey="${userNameAccessKey}" path="username" autocomplete="false" htmlEscape="true"/>
<label for="password" class="fl-label"><spring:message code="screen.welcome.label.password"/></label>
<spring:message code="screen.welcome.label.password.accesskey" var="passwordAccessKey"/>
<form:password cssClass="required" cssErrorClass="error" id="password" tabindex="2" path="password" accesskey="${passwordAccessKey}" htmlEscape="true" autocomplete="off"/>
</form:form>
And noticed that no action attribute was provided. This is a JSP app that uses Spring Web Flow. What does JSP do with form posts under this scenario? How could I find the section of code that actually handles the HTTP POST (form submission)?
Spring's <form:form> tag posts back to the flow path if it is not explicitly specified. Here's an example of generated HTML source from one of my flows:
<form id="model-bean" action="/context-root/flow-id?execution=eXsY" method="post">
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?
When I write <h:outputText value="Login Name"/> tag in my JSP, I get the following exception message:
Cannot find FacesContext
Without that my JSP works fine. Here is my JSP:
<%# page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<body>
Login Name <input type="text" value=""/><br>
<h:outputText value="Login Name"/>
Password<input type="password" value=""/><br>
<input type="submit" value="Login">
</body>
</html>
There are two flaws in your code:
The root cause of this exception is that you forgot to pass the request through the url-pattern of the FacesServlet as definied in web.xml. If the JSP page is for example named page.jsp and the url-pattern of the FacesServlet is for example *.jsf, then you need to invoke it by http://example.com/context/page.jsf instead of .jsp. This way the FacesServlet will be invoked and create the FacesContext. Otherwise the JSF components in the page will complain that the FacesContext cannot be found and you will face this particular exception.
The <f:view> is missing in the page. Wrap the entire <html> in it. E.g.
<%# page pageEncoding="UTF-8" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!doctype ... >
<f:view>
<html>
...
</html>
</f:view>
By the way, that import attribute in the <%#page> is completely superfluous. Get rid of it.
I have problem with I18N in JSP, specifically, with forms.
When I enter some Czech characters (e.g., "ěščřžýá...") into my page one form, into the field "fieldOne", and then show text from that field on page two, instead of Czech characters I see this as "ÄÄ". (Note, the second page gets the Czech characters with "request.getProperty("fieldOne")")
Here is the source code:
Page one:
<%#page contentType="text/html"%>
<%#page pageEncoding="UTF-8"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<html>
<head></head>
<body>
<form action="druha.jsp" method="post">
<input type="textarea" name="fieldOne">
<input type="submit">
</form>
</body>
</html>
Page two:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<html>
<head></head>
<body>
<h1>The text: </h1> <%=request.getProperty("fieldOne")%>
</body>
</html>
Thanks for help...
Which container are you using? This information is important for this kind of problems.
Anyway, try calling
request.setCharacterEncoding("UTF-8");
before reading the parameter. Sometimes setting the page encoding in the header directive isn't enough. You definitely need to do this in Tomcat and servlets, I assume that this could be also the case for JSPs.