in my application I need to prepare a path for XML file inside JSP page. I'm doing someting like this:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<!DOCTYPE html>
<c:set var="abs_path" value='<%= getServletContext().getRealPath("").replace(" ", "%20").replace("\\", "/") %>' />
But there is a problem, I get the followind exception:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 6 in the jsp file: /users.jsp
String literal is not properly closed by a double-quote
3: <%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4: <%# taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
5: <!DOCTYPE html>
6: <c:set var="abs_path" value='<%= getServletContext().getRealPath("").replace(" ", "%20").replace("\\", "/") %>' />
Apparently it's about this part: .replace("\\", "/")
When I delete it, I don't get this exception.
What's that about? I will be very grateful for any clue.
EDIT:
I use this variable in the following way:
<c:import url="file:/${abs_path}/MyProject/xml/users.xml" var="inputDoc" charEncoding="UTF-8" />
<c:import url="xsl/users_list.xsl"
var="stylesheet" charEncoding="UTF-8" />
<x:transform
xml = "${inputDoc}"
xslt = "${stylesheet}">
</x:transform>
It is not about "\\" being a regular expression ... because that wouldn't result in a JSP compilation error. (And besides, the argument of String.replace(String) isn't interpreted as a regex.)
However, the compilation error does seem to be saying that you need double escaping, and I think that the reason is that the JSP syntax is "consuming" one level of escaping itself ... in this context.
This is from the JSP 2.1 spec:
JSP.1.6 Quoting and Escape Conventions
...
Quoting in Attributes
Quotation is done consistently regardless of whether the attribute value is a
literal or a request-time attribute expression. Quoting can be used in attribute
values regardless of whether they are delimited using single or double quotes. It is
only required as described below.
A ‘ is quoted as \’. This is required within a single quote-delimited attribute
value.
A “ is quoted as \”. This is required within a double quote-delimited attribute
value.
A \ is quoted as \\
Only when the EL is enabled for a page (see Section JSP.3.3.2, “Deactivating
EL Evaluation”), a literal $ can be quoted by \$. Similarly, a literal # can be
quoted by \#. This is not required but is useful for quoting EL expressions.
A %> is quoted as %\>
A <% is quoted as <\%
The entities ' and " are available to describe single and double
quotes.
Anyway, try writing the offending code fragment as replace("\\\\", "/").
Related
I have a problem to display German special characters.
My Java Bean tests some user input and fills an error message string if there is an error. In the jsp I access the Bean's error message if there is one using JSTL Core like so:
<c:if test="${MyBean.errorMsg != ''}">
<c:out value="${MyBean.errorMsg}" />
</c:if>
The error message is: "Alle Felder müssen ausgefüllt werden.";
It is displayed in Chrome as follows: "Alle Felder müssen ausgefüllt werden."
I tried including the following in the JSP:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
which worked for all text I output directly in JSP but not via the
prefix c.
I tried to set the JSTL character encoding to UTF-8 via FTM:
<%# taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>
<fmt:requestEncoding value = "UTF-8" />
Which did not solve it.
I put the following in the Controller Servlet that handles the request:
request.setCharacterEncoding("UTF-8");
This did not have any effect.
I wrote a simple JSP file:
<%# page import="java.util.*" session=”true” isErrorPage=”false”%>
<HTML>
<BODY>
<h4>Welcome to the world of JSP</h4>
This JSP uses the page directive
</BODY>
</HTML>
I got error for the first line, inside Eclipse:
Multiple annotations found at this line:
- Invalid location of text (>) in tag (<jsp:directive.page>).
- Start tag (<jsp:directive.page>) not closed properly, expected
'>'.
Regarding the first message in the error, the first line <%# page import="java.util.*" session=”true” isErrorPage=”false”%> doesn't seem to have a > with invalid location. What does the error message mean?
Regarding the second message in the error, isn't the <%# page import="java.util.*" session=”true” isErrorPage=”false”%> ended with >? Why does it say > is expected?
Thanks.
In your tag
<%# page import="java.util.*" session=”true” isErrorPage=”false”%>
these are not the usual double quotes, change that with
<%# page import="java.util.*" session="true" isErrorPage="false"%>
i am learning JSTL and from tutorials point in this link
when i tried to execute that example in the page which is
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:url> Tag Example</title>
</head>
<body>
TEST
</body>
</html>
i am getting following error.
but i could not understand why and what is the solution for that?
The validator is probably confused by the nested double quotes:
TEST
You can make the code cleaner by doing:
<c:url value="/jsp/index.htm" var="myUrl" />
TEST
which assigns the value of the processed URL to a var named 'myUrl' and then uses JSP Expression language to output the URL.
Change this line:
<title><c:url> Tag Example</title>
By:
<title><c:url> Tag Example</title>
Otherwise, the JSP engine will interpret this (<c:url>) as the beginning of a tag to be processed.
You need to escape your nested quotes as
TEST
Otherwise the HTML parser perceives "<c:url value=" as the href value and complains about the space in between. Alternatively, you can make use of single and double quotes as
<a href='<c:url value="/jsp/index.htm"/>'>TEST</a>
Or,
TEST
This is what I am trying but it seems not to work:
(myStringHere.split(".")[(myStringHere.split(".").length)-1]).concat(text[myStringHere])
The string I have will be something like this:
com.foo.bar.zar.gar.ThePartIWant
ThePartIWant is what I want to show in the page only.
I am using Expression Language 2.2
If you are doing it in JSP then try with JSP JSTL function tag library that provide lost of methods as defined here in JavaDoc
Read more here on Oacle The Java EE 5 Tutorial - JSTL Functions
Here is the code to get the last value based on split on dot.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
...
<c:set var="string1" value="This.is.first.String." />
<c:set var="string2" value="${fn:split(string1, '.')}" />
<c:set var="lastString" value="${string2[fn:length(string2)-1]}" />
<c:out value="${lastString }"></c:out>
output:
String
Here is one more example
You want to catch only the last part of your string ? Try this :
string[] s_tab = myStringHere.split(".");
string result = s_tab[s_tab.length - 1];
If you want it in one line :
string result = myStringHere.split(".")[StringUtils.countMatches(myStringHere, ".")];
Try this :
String s = "com.foo.bar.zar.gar.ThePartIWant";
System.out.println(s.split("\\.")[(s.split("\\.").length)-1]);
"." is a special character. You have to escape it because period means any character in regex.
Source : How to split a string in Java
i have the following JSP:
<%# page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%# page isELIgnored="false"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><c:out value="${it.title}"/></title>
</head>
<body>
<c:forEach var="speaker" items="${it.speakers}" varStatus="stat">
<ul>
<li><c:out value="${speaker.person.firstName}" /> <c:out value="${speaker.person.lastName}" />, <c:out value="${speaker.person.address.city.zip}" /> <c:out value="${speaker.person.address.city.name}" /></li>
</ul>
</c:forEach>
</body>
</html>
Eclipse warns me about every instance of EL Expressions in my code:
Warning [line 10]: "value" does not support runtime expressions
Warning [line 13]: "items" does not support runtime expressions
...
this is however not true, EL gets evaluated correctly by the server.
Can anyone hint me in the right direction why eclipse is warning me about those EL expressions?
Your taglib directive imports a JSTL 1.0 taglib. It should be JSTL 1.1 instead (note the difference in URI):
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Possible solution (found here):
Twin Libraries
The JSTL tag libraries come in two
versions which differ only in the way
they support the use of runtime
expressions for attribute values.
In the JSTL-RT tag library,
expressions are specified in the
page's scripting language. This is
exactly how things currently work in
current tag libraries.
In the JSTL-EL tag library,
expressions are specified in the JSTL
expression language. An expression is
a String literal in the syntax of the
EL.
When using the EL tag library you
cannot pass a scripting language
expression for the value of an
attribute. This rule makes it possible
to validate the syntax of an
expression at translation time.
So maybe your eclipse and the server use different tag libraries.
try this:
change this:
<%#taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
to yes:
<%#taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
hope it works for you. I got this from www.csdn.net.