How to use Favicon in Maven based Java web application? - java

I have a web application developed in Java, and it is in Maven structure. I added the favicon.png to src/main/resources location and used the following tagline in JSP.
<head>
<link rel="icon" type="image/png" href="favicon.png"/>
</head>
But unfortunately the favicon is not visible. How can I fix this issue?

The favicon should be in your context root.
If you have src\main\webapp place it there, also if you want to load from classpath place it in src\main\resources\META-INF\resources
NOTE: If your application have context other than root (i.e. \) then you have to append it to the href.

Related

The disadvantage of editing source that build in react.js

I'm initializing web project follow this tool
Backend : java by using Spring MVC framework
frontend : react.js by using next.js framework
Frontend source is inside backend and when I build frontend i placed it inside webapp so the structure of project look like this.
root
react
frontend
src
main
java
controller
resources
web app
build (react build will go here!)
From backend can make a right call to frontend. But there are a static resources which must be used and page cannot find them.
So I changed src path in html file (I changed it in folder "build/") then page can find resources. for example
before in index.html
<link rel="preload" href="/_next/static/chunks/commons.a979bd194c5739a7c69d.js" as="script" />
<link rel="preload" href="/_next/static/runtime/main-eb82511d0817e1061fd2.js" as="script" />
after in index.html
<link rel="preload" href="/myWeb/build/_next/static/chunks/commons.a979bd194c5739a7c69d.js" as="script" />
<link rel="preload" href="/myWeb/build/_next/static/runtime/main-eb82511d0817e1061fd2.js" as="script" />
My Question is: Is there a real disadvantage from what I did?

Spring Boot Context Path for WebJars in Tomcat

If i build a war file and deploy it on tomcat, it cannot find the webjars which i have written in index.html as follows;
<script type="text/javascript" src="/webjars/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/webjars/angularjs/angular.min.js"></script>
so, had to manually change the src="{my_application_name}/webjars/jquery/jquery.min.js"
Is there a way i can do this dynamically?
When you point any path preceding with / using src tag, it will take absolute path and your request will be redirected to localhost:[port]/whatever_path_is.
So I suggest you to use relative path without / like below :
src="path_to_your_js_files"

Cannot access CSS files defined under WEB-INF from JSP

I have created Struts2 application using Maven. In this application I have created two namespaces, first is tc and the second one is cmpui. From the JSP page, I am trying to access .css files, but it is giving me 404 error.
Location of JSP page is :
webapp\tc\layout\stylesheets.jsp
Location of CSS file is :
WEB-INF\css\default.css
Code on JSP page is
<link rel="stylesheet" type="text/css" href="../WEB-INF/css/default.css">
Any suggestion please.
You can not access a resources that are under WEB-INF folder. Move your static resources to another place accessible by Struts2 (for example, web root). And use s:url tag to build the URL.
<link rel="stylesheet" type="text/css" href="<s:url value='/css/default.css'/>">

Java/Play - Proper way to link to CSS/JS?

I'm creating a Play! 2.1.1 application which I have packaged into a war file using Play2War. This required me to add a context, application.context=/theApp/, in the application.conf file.
I deployed the WAR file to a tomcat7 server which resulted in the url localhost:8080/theApp/.
CSS/JS files load when the url is for example http://localhost:8080/theApp/thisseemstowork, but once the url is http://localhost:8080/theApp/thisoughttowork/180 none of the CSS/JS files are loaded. I simply get
GET http://localhost:8080/theApp/thisoughttowork/public/javascripts/vendor/bootstrap.min.js 404 (Not Found)
This is how I link to the js file in the views:
<script src="public/javascripts/vendor/bootstrap.min.js"></script>
This is in my routes file
GET /public/*file controllers.Assets.at(path="/public", file)
Anyone have an idea what I'm doing wrong? Let me know if you need any more info.
You need to use the Assets controller and reverse-routing.
<script src="#routes.Assets.at("javascripts/vendor/bootstrap.min.js")"></script>
This will generate the correct path no matter where you are in the app. As you can see from the routes file snippet you posted the path is relative to the /public folder, so this will work for any stylesheets, images, etc you put in there.
<link rel="stylesheet" href="#Assets.at("stylesheets/bootstrap.min.css")">
<link rel="stylesheet" href="#Assets.at("other/folder/styles.css")">
Here's Play's documentation:
http://www.playframework.com/documentation/2.1.1/Assets

favicon.ico in Java WAR file

Can anybody provide some instruction on how to setup a war file to show a favicon.ico in the browser address bar?
You can also use the following HTML markup in your HTML:
<link rel="icon" type="image/gif" href="/img/image.gif">
Most newer browsers should support it and I think it's generally a more clean way since you can use any image type/name/location you want.
This might be different in different application servers. For tomcat, the favicon comes from the directory your root context is mapped to. So if your application is mapped to the root context [/], just place the favicon.ico file in the top level folder in your war file.

Categories