Spring #RequestMapping("/favicon.ico") does not work when requested from index.html - spring

I used to get mapping not found for favicon.ico so decided to deal with it.
Easiest thing for me was to add an action to a controller method,#RequestMapping("/favicon.ico").
I no longer get these complaints ( although I don't request favicon.ico myself in an html file, I guess teh browser does this automatically ).
When I visit http://localhost:8080/favicon.ico the action gets hit!
I add the following to my html file:
<link href="/favicon.ico" rel="icon" type="image/x-icon" />
But the action never gets hit.
I've also tried
<link href="http://localhost:8080/favicon.ico" rel="icon" type="image/x-icon" />
but action does not get hit.
I suspect this has to do with get/post request something, and when I request it from the browser manually, a get request is made. When from an html file something else, and Spring won't recognize this.
Please do not recommend me doing:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
as I like to do it from my controller as I have some logic there.
Could there be some cache involved?
EDIT:
I should also mention that i keep getting a tomcat favicon instead. Can't see any info on a favicon.ico ever being requested.
Is tomcat supplying it by default and ignoring to hit/forward it to my action?

I ran into the same issue.
When spring boot starts you can see this in the console:
[...] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
You can disable the spring mvc favicon handler from the application.properties:
spring.mvc.favicon.enabled=false
Source: Spring Boot: Overriding favicon

Related

(WAR) Spring Boot Admin custom view not found

Once deployed as a WAR into Tomcat, my customized SBA dashboard fails at showing a custom view that was first doing fine into a JAR (but it also fails now, btw)
This is where is located the extension's directory into the WAR:
/WEB-INF/classes/META-INF/spring-boot-admin-server-ui/extensions/customz/...
REM: I've also customized the login page and my picture is located at /WEB-INF/classes/META-INF/spring-boot-admin-server-ui/assets/img/ so I guess that the classpath isn't the issue.
Still, I've got an error into the web browser's console, though:
GET http://xx.xx.xx.xx:8080/extensions/customz/css/custom.fb3a4f29.css net::ERR_ABORTED 404
REM: according to my context path, the correct path should probably be that one:
http://xx.xx.xx.xx:8080/myapp/dashboard/extensions/customz/css/custom.fb3a4f29.css
server.servlet.context-path=/myapp
spring.boot.admin.context-path=/dashboard
...
<packaging>war</packaging>
<build>
<finalName>myapp</finalName>
...
</build>
But I couldn't figure out how to change the base path for my views in this case. I should just have to prefix somehow the system with my "customz/dashboard" context path (?)
Does anybody, please, know how to get out of this trap?
NB: Spring Boot 2.2.8, Spring Cloud Hoxton.SR5, SBA 2.2.3, Tomcat 9.0.36
AdminServerUiAutoConfiguration declares resource handlers for the extensions (mapping context-path/extensions/** to the above classpath, as figured out when I tried to visualize custom JS and CSS earlier.
Remember that spring.boot.admin.ui.extension-resource-locations default is classpath:/META-INF/spring-boot-admin-server-ui/extensions/ which seems fine in my case. That confirms that custom views are correctly exposed.
So that leads us to spring-boot-admin-server-ui/src/main/frontend/index.html where all paths appear to be ... absolute!
<th:block th:each="cssExtension : ${cssExtensions}">
<link rel="preload" th:href="'/extensions/' + ${cssExtension.resourcePath}" as="style">
</th:block>
<th:block th:each="jsExtension : ${jsExtensions}">
<link rel="preload" th:href="'/extensions/' + ${jsExtension.resourcePath}" as="script">
</th:block>
<th:block th:each="cssExtension : ${cssExtensions}">
<link th:href="'/extensions/' + ${cssExtension.resourcePath}" rel="stylesheet">
</th:block>
<link rel="shortcut icon" th:href="${uiSettings.favicon}" type="image/png">
<title th:text="${uiSettings.title}">Spring Boot Admin</title>
...
<script lang="javascript" src="sba-settings.js"></script>
<th:block th:each="jsExtension : ${jsExtensions}">
<script lang="javascript" th:src="'/extensions/' + ${jsExtension.resourcePath}"></script>
</th:block>
I guess these are two points where both servlet and admin ui context paths should be added in order for extensions to be held. May be tehe quickest way would be to URls relative in index.html
So I did exactly that... git cloning SBA on tag 2.2.3, doing the changes and Maven installing it, changing my server's parent to SNAPSHOT, then rebuilding the WAR into Tomcat. Et voilà.

Fail to serve .jsp and .html file via default servlet handler using spring mvc <mvc:default-servlet-handler />

The title should explain the biggest part :)
I should be able for example to access http://www.someurl.com:8080/index.jsp but instead I get HTTP Status 404 - /index.jsp
Now why do I assume I should be able to serve static content (ie not be redirected to custom controller but to default servlet handler in stead.)?
Because I have added the following element to my mvc dispatcher servlet config:
<mvc:default-servlet-handler />
I have read that in some case the name of the default server cannot be guessed and I have looked it up in the file: ̣*~/tomcat7/conf/web.xml .*
The default servlet is specified by a name "default". So I tried adding:
<mvc:default-servlet-handler default-servlet-name="default"/>
But to no avail...
I have one spring dispachter servlet mapped to '/'.
I have two controllers mapped to, one controller is mapped to '/' and one is mapped to '/todo'
The controllers work fine.
I thought the controller mapped to '/' could be the culprit so I removed that controller but to no avail.
Anybody an idea? And is it possible to have a controller mapped to '/' and still serve a page like /index.jsp??
Arf, I had put my static resources under the webapp/WEB-INF folder instead of the webapp folder. Now it seems te be working fine ... :)

Spring MVC: how do I configure a web app to call a controller by default?

I'd like my Spring application to call a controller whenever someone opens the application at the root, like this:
http://server.com/myapp
or
http://server.com/myapp/
Ideally, I'd like the user to automatically be taken to /home.do, as in:
http://server.com/myapp/home.do
When I updated web.xml to include /home.do in the <welcome-file-list>, Tomcat returns an error page displaying:
The requested resource (/myapp/) is not available.
When I change <welcome-file-list> to instead take the user to the JSP behind home.do, the page loads, but the JSTL tags in the page cannot access a properties file that is loaded by my Spring configuration.
Any help would be much appreciated!
Create an html page or jsp add it to <welcome-file-list> and add this to the code of your page
<meta http-equiv="refresh" content="0; url=http://server.com/myapp/home.do" />
File list makes the page your default page and redirect will send to your controller without user notice

PageNotFound - No mapping found for HTTP request with URI

I am getting this error :
o.s.web.servlet.PageNotFound - No mapping found for HTTP request with URI
when I try to render a view from an controller/action
I've read about it being that servlet mapping being:
Spring MVC Dispatcher Servlet
/webspring/*
and the star hijacks even the jsp renderings. How can I overcome this ( as simple as possible ) and hopefully without having to resort to .do endings so it becomes controller/action.do
Thanks !
===============EDIT=================
It appears that this happens if your jsp is not in the WEB-INF folder, but outside but in WebContent... any idea why? The jsp's cant be outside? Or shouldnt?
But now I still have problems with linking to a css file... which usually are outside of the web-inf directory, right?
A link:
<link rel="stylesheet" href="webspring/public/main.css" type="text/css"/>
where the directory structure is:
WebContent
WEB-INF
webspring
public
main.css
... is being resolved to
http://localhost:8080/XXXWeb/webspring/auth/webspring/public/main.css
when the style tag is added on page with url:
http://localhost:8080/XXXWeb/webspring/auth/loginForm
auth is the controller.
What is going on here?
I got the same problem with you. So I resolve like this
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/webspring/auth/webspring/public/*</url-pattern>
</servlet-mapping>

Spring3 - WebFlow - JSF -- Can't get mapping of '/' to work properly

Sorry if this is a Newbie question, but I am trying to teach myself Spring MVC/WebFlow with JSF/Primefaces, and I've run into a snag setting it up...
If in web.xml, I set the MVC dispatcher to a catch all '/', then register #RequestMapping(value = "/{catchall}", method = RequestMethod.GET), in my controller. The page is served, but the resources files all have the {catchall} name prepended to the start of the name e.g.
If I use //127.0.0.1:8080/testpage
<link type="text/css" rel="stylesheet" href="/testpage/javax.faces.resource/jquery/ui/jquery-ui.css?ln=primefaces&v=2.2" />
This results in every resource being NOT FOUND, and returning a 404 error?
If instead of a 'catch-all', I set the MVC dispatcher to '/a/*', the perform the same test, e.g.
//127.0.0.1:8080/a/testpage, it works fine with the resources being shown as:
<link type="text/css" rel="stylesheet" href="/a/javax.faces.resource/jquery/ui/jquery-ui.css?ln=primefaces&v=2.2" />
I am trying to setup a system where the page is served dynamically from the datastore, and want the page to be - www.whatever.com/{pagename} - without any prefixed structure, or postfixed identifier (e.g. .jsp, .jsf, .xhtml, etc.)
I can post configs if required, but am sure I'm just missing something stupid!!!!
Please help.
Last time I tried I found that the Sun Mojarra library assumes your servlet mapping is either a prefix mapping or a servlet mapping by extension (but not the default servlet mapping "/"). Your best bet to use URLs without a servlet prefix might be to use URL rewriting techniques such as Tuckey UrlRewriteFilter or in JSF PrettyFaces is quite popular.

Resources