Spring Boot Context Path for WebJars in Tomcat - java

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"

Related

How to use Favicon in Maven based Java web application?

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.

Call ExtJS application from a JSP file placed outside my Ext Application's root folder

I am trying to call a ExtJS application from a JSP file placed outside my Ext Application's root folder:
folder structure
How I am trying to do it is:
<script type="text/javascript" src="../UI-INF/LoginApp/CustomManifest.js"></script>
<script id="microloader" data-app="c320a89b-22a4" type="text/javascript" src="../UI-INF/LoginApp/bootstrap.js"></script>
<script type="text/javascript" src="../UI-INF/ext651/build/ext-all-debug.js"></script>
<script type="text/javascript" src="../UI-INF/LoginApp/app.js"></script>
Manifest is unable to properly fetch classic.json due to which I am getting this error:
Unable to load classic.json properly into manifest
What is the proper way to do it?
Note: We cannot change the JSP location or our application location due to server constraints.

Couldn't access JS file from WEB-INF

I have following project structure
In exportPage.jsp JSP I have included JS files as
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<script type="text/javascript" src="<c:url value="/js/jquery-1.11.2.min.js"/>"></script>
<script type="text/javascript" src="<c:url value="/js/dataTables.tableTools.js"/>"></script>
But when I run app to access the page I am getting 404 error for both URLs. URL generated is as follow:
http://localhost:8090/springmvc/js/jquery-1.11.2.min.js
What I have to configure in STS to fixed this issue. Is this build path issue. By the way this is Maven type Spring Project in STS.
The problem is that:User does not have access to WEB-INF.So,your js files are in WEB-INF.When page load js files download to user PC and user has no access to this folder and you get error.You must create js folder outside WEB-INF.You can create your views in your WEB-INF because user has no access directly to this folder.

How to serve static files in my web application on Tomcat

I have this servlet based web application project in eclipse and want to append some html tags like <script src="Chart.js">.
The folder structure is:
C:/apache-tomcat-7.0.53/
my workspace is in D:/../../workplace/CpdApplication/src/cpd/MyServlet.java
cpd contains: MyServlet.java, Chart.js and other files.
CpdApplication/WebContent/META-INF/web.xml
I have some path problems, and I can't resolve them, I searched over and over again and still not working, I get
a 404 (Not Found) for http://localhost:8080/CpdApplication/Chart.js.
The problem is when I want to append <script src='Chart.js'></script>, Tomcat cannot resolve the Chart.js static file.
I have some path problems, and I can't resolve them, I searched over
and over again and still not working, I get a 404 (Not Found) for
.../CpdApplication/Chart.js
Indeed, when writing <script src="/Chart.js"/> you are telling the browser to make its own, separate HTTP request to get the JavaScript file. For this to work:
The servlet container needs to be able to serve static files
To this end, you need to have a servlet-mapping inside your web.xml to serve static files (ie. the default servlet).
This should do:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
</servlet-mapping>
Then place your Chart.js in the following folder: WebContent/js/ and it should work.
EDIT: Of course, you'll need to update the <script> tag in your HTML. Also, make sure that you redeploy your web app to update web.xml on your servlet container (Tomcat I presume).
This is works for me. Thanks 沖原ハーベスト
welcome.jsp
<head>
<script src="resources/js/jsx/browser.min.js"></script>
<script src="resources/js/react/react.min.js"></script>
<script src="resources/js/react/react-dom.min.js"></script>
<script src="resources/js/main.js"></script>
<link rel="stylesheet" type="text/css" href="resources/css/style.css">
</head>
File hierarchy tree
Web.xml
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
It seems to me like Chart.js is stored in the same folder with your servlet source.
Usually you should have a structure like this one (I have simplified it):
/css/your-css.css
/js/your-script.js
/src/your-package/YourServlet.java
Whenever you run your application, your compiler creates a set of files that will be copied to your web container. The copied files do not include your src folder, but instead a copy of your built (compiled) classes. All other files (with some exceptions that we should not care about right now) must be outside your src folder in order to get copied.
Try moving your JS inside a js directory outside your src directory. Then, link it like this:
<script src='/Your-Context-Path/js/Chart.js'>
There must be a function to get your context path automatically, I think it is
ServletContext.getContextPath()
You can read about it here.
That should make the trick.
As #Andy replied, you need to set all your resource (JS, CSS, images, etc.) in a folder in your application, then access to them using <context_path>/folder/to/your/resource/<resource_name>.<ext>. Here's a sample of how to do it:
Create a folder inside the root folder (usually named web or WebContent) of your web application with name res.
Inside res, create a folder called js to put all the JavaScript files there. You may create a css folder to handle all your CSS stylesheets, img for images, and on.
In your view, this mean, your JSP, you should access to your resources via context path.
This is how your application folders should look:
- WebContent
- res
- js
Chart.js
In your JSP:
<script src="${request.contextPath}/res/js/Chart.js"></script>
Since you're creating the view content from your Servlet (a shoot on the foot, by the way), use request#getContextPath() to attach it:
"script src=\"" + request#getContextPath() + "/res/js/Chart.js\"></script>";
Rightly said by #VH-NZZ but in case you are using Spring 4x or above, you need to add static resources into ResourceHandlerRegistry. You can do this into your "AppConfig"(your application configuration file), like this :
#Configuration
#EnableWebMvc
#EnableScheduling
#ComponentScan(basePackages = "com.packagename")
public class AppConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/build/**").addResourceLocations("/build/");
registry.addResourceHandler("/dist/**").addResourceLocations("/dist/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
registry.addResourceHandler("/assets/**").addResourceLocations("/assets/");
registry.addResourceHandler("/images/**").addResourceLocations("/images/");
}
Again as rightly said above, you should not keep your java files and static content together at the same location. These should always be available at a separate location. You can put your static content directly under your webapp folder.

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

Categories