Spark Java isn't recognizing static files and serving them - java

This was working before.
I have a request for 2 things.
<link rel="icon" href="/favicon.png">
and a style,
background-image: background.jpg;
but it doesn't recognize and returns with 404 not found.
I have set
Spark.staticFileLocation("/public/");
and made sure that it is correct
src/main/resources/public/*.jpg
How can I fix?

Try to remove terminating slash
staticFileLocation("/public");

Related

"Absolute paths not recommended in JSPs" ? - Is this also apply to the #RequestMapping()?

I got warnings from IntelliJ that "Absolute paths not recommended in JSPs"
I understand that, for internal files locations, like "styles.css or script.js", we should use relative path, something like this -
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/styles.css">
But how about the #RequestMapping(), #GetMapping, #PostMapping, ... , and etc
For example, I have a form here:
<form method="post" action="/file/upload-submitted" enctype="multipart/form-data">
...
</form>
and I also have:
#PostMapping(value = "/file/upload-submitted")
IntelliJ also give me warning "Absolute paths not recommended in JSPs" at "/file/upload"
Is it the correct warning?
***The reason that I put the absolute path there because there are multiple paths to get to the form.
#GetMapping(value = {"/file", "/file/upload"})

Thymeleaf 3 URL resolving not working with Spring Boot 4 + Spring Security

After having configured Thymeleaf 3 in Spring Boot 4 using the Gradle configuration
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.thymeleaf:thymeleaf:3.0.3.RELEASE')
and
ext['thymeleaf.version'] = '3.0.3.RELEASE'
ext['thymeleaf-layout-dialect.version'] = '2.1.2'
URL resolvers do not properly resolve the URLs in Thymeleaf views:
<link rel="stylesheet" href="/css/login.css" data-th-href="#{~/css/login.css}">
simply becomes
<link rel="stylesheet" href="/css/login.css">
I have done some debugging and first of all, during a request I noticed that SaveToSessionResponseWrapper (sub of SaveContextOnUpdateOrErrorWrapper) gets initialized like this:
public SaveContextOnUpdateOrErrorResponseWrapper(HttpServletResponse response,
boolean disableUrlRewriting) {
super(response);
this.disableUrlRewriting = disableUrlRewriting;
}
The arguments passed are a FireWalledResponse and false. The latter results in the following method completely disabling the forwarding of URLs:
#Override
public final String encodeURL(String url) {
if (this.disableUrlRewriting) {
return url;
}
return super.encodeURL(url);
}
Now, if I put a breakpoint in the constructor and force disableUrlRewriting to be true, it eventually executes HttpServletResponseImpl.isEncodeable which then fails here:
} else if(hreq.isRequestedSessionIdFromCookie()) {
return false;
At this point I'm not sure what is wrong. I'm unable to find anyone with this error and it works with neither starter-tomcat nor starter-undertow but I haven't done as thorough debugging in Tomcat yet.
#{~/css/login.css} is a server-relative URL in Thymeleaf.
If you want a context-relative URL, omit the tilde (~) character:
<link rel="stylesheet" href="/css/login.css" data-th-href="#{/css/login.css}">
See Standard URL Syntax article for details.
You should use
<link rel="stylesheet" href="/css/login.css" th:href="#{/css/login.css}">
Without data-th-href

JSP exception, "quote symbol expected"

<%# page import="java.util.*" %>
<html>
<body>
<h1 align="center">blablalblalblab</h1>
<p>
<%
List styles = (List)request.getAttribute("styles");
Iterator it = styles.iterator();
while(it.hasNext()) {
out.print("<br>try: " + it.next());
}
%>
</p>
</body>
</html>
after executing my servlet request i'm getting error
org.apache.jasper.JasperException: /result.jsp (line: 1, column: 18) quote symbol expected
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
can't find any quotes that are not on right place.
Make sure all your quotes are straight quotes, not curvy ones.
Don't use Java in JSPs, please. That's what the standard tag library is for.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h1 align="center">blablalblalblab</h1>
<p>
<c:forEach items="${styles}" var="style">
<br>try: ${style}
</c:forEach>
</p>
</body>
</html>
In more detail:
Embedding Java code in a JSP makes the page difficult to read (JSP is a tag-oriented language), difficult to maintain, and difficult to debug.
The standard tag libraries are already debugged, have plentiful documentation and examples, and probably already Do What You Want To Do.
If you truly have some logic that needs to be performed in Java and no pre-existing tags exist, you can either a) put the logic in a bean and call it via JSTL or b) write your own tag using tagfiles.
Why is Java code better in a bean or tag library than in a JSP?
Testing is a big factor: beans and tag libraries can be tested
outside of a running servlet environment with ease.
Tag libraries are reusable and significantly cleaner than JSP includes.
I guess you have copy pasted it from somewhere, make sure the double quotes are proper. I had the same issue when I copied it from a PDF, it was resolved once I corrected my double quotes.
Your JSP works just fine with Tomcat 6. So, it's probably either some include-related issue or some previously compiled classes are not getting recompiled.
Try to clean up your Tomcat work directory and try again.
Check " if you copied it from somewhere. I had the same error because of ".
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%#page isELIgnored=”false” %>
double quotes around the false are the wrong ones. Change it to " and it will work.
While I agree with Scott A's admonition to use JSTL instead of putting Java directly into the JSP the question deserves being explored a little further. I just encountered this myself for the first time and had to dig a bit ot answer it.
Technically the error means what it says. You're missing some quotes somewhere. The simplest would be something like in your h1 tag if it read:
<h1 align=center>
instead of what you have.
<h1 align="center">
Obviously there is nothing in the code snippet that you pasted which is missing quotes so I would explore a couple of things.
First, what does the output of your it.next() look like? Since you're pulling in a list called styles I wonder if something in there is making jasper think it's a style tag instead of text you are trying to render.
Second, I would explore Pradeep's answer and see what if there is some subtle issue pasting that was resolved when you pasted it here on stackoverflow. Specifically I would look for 'smart quotes' IE many text editors (including outlook and most of office) like to use different quotes on the front and back of quoted text.
IE
'this is quoted text'
becomes
`this is quoted text'
which can be difficult to notice.

How to write trivial JSP that just returns static XML page

I'm trying to write a trivial JSP that returns the contents of a static xml file. I need to run this in tomcat. Eventually, this will be more dynamic, but at first, I just want to return an xml file. Can anyone point me to a demo for such a trivial beast, I'm trying to learn what are the minimum chunks I need to create the web app and install in tomcat.
Mucho appreciato,
pawpaw17
Following this document is always a good start.
But you may have issues.
First, it's basically trivial to do something like:
http://example.com/app/mydynamicxml.jsp
that returns an XML blob. Just paste the XML in to that file.
But it won't have an XML content type. You can fix that by adding directives to the JSP:
<%#page contentType="application/xml" %>
However, that brings on MORE problems.
Specifically, an XML file CAN NOT start with white space. It MUST start with <?.
That directive will very likely insert a blank line in to you XML file.
So, what you really want is:
<%#page contentType="application/xml" %><?xml version...
Finally, there IS a JSPX version of JSP, which uses an XML syntax, and avoids all of those white space issues. There's also a directive to Tomcat that can eliminate the white space issue. But, out the gate, this is the fastest, "obvious" tact to take.
The main thing would be to specify the content type as <%# page contentType="text/xml" %>
<%-- Set the content type
--%><%# page contentType="text/xml" %><%--
--%><?xml version="1.0" encoding="UTF-8"?>
<root><entry key="key1" value="value1" /><entry key="key2" /></root>
Check out the article on Sun site
Tried adding a trimDirectiveWhitespaces="true" page directive, but that wasn't supported on my server.
The solution was simply to remove any newlines after any page directives.

Spring and dynamic inclusion of JSP files

I'm starting building web apps in Spring 3 (and in J2EE) in general.
Looking at the petclinic example I've seen that the programmer creates many JSP pieces, like header, includes, footer and then stitches them together using static inclusion. Anyway what I'd like is that I may have a base page, like Base.jsp and be able to include things like this:
<body>
<jsp:include page="${subpage}"></jsp:include>
</body>
the reason is that I'd like a main page, then being able to put in the ModelAndView returned by the controller which parts of the pages display in each situation (with the data attached to it). This works, but it gives no errors in case ${subpage} is not found, the jsp name is wrong or missing. I'd like more error checking...
Is this the best and recommended way to do this? And if this seems a good idea for what I've in mind, what's the correct way of doing it?
You might want to use Apache Tiles 2 integration for managing your JSP files. Spring has good integration support Apache Tiles. It also shows if there's an error in your page. I've put an example of it at http://krams915.blogspot.com/2010/12/spring-mvc-3-tiles-2-integration.html
It appears you have additional quotes in your subpage. Get rid of them. For example:
<c:set var="subpage" value="/jsp/index.jsp" />
If you have to set it in a controller or servlet - just use request.setAttribute("subpage", "/jsp/index.jsp")
For error checking you can use:
<c:catch var="myException">
<c:import url="${subpage}" />
</c:catch>
and later you can check it with:
<c:if test="${myException != null}">
...
</c:if>
Take a look at Sitemesh (http://www.opensymphony.com/sitemesh). It is a servlet filter-based page layout system that is easy to use. I have done a number of projects using it with Spring MVC and it worked very well.

Categories