About classpath:/META-INF/resources in spring - java

I am using below content to configure the swagger in springmvc.xml.
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**"
location="classpath:/META-INF/resources/webjars/" />
But I does not understand the internal. Why can i visit http://localhost:3333/swagger-ui.html?
There are so many jar files in my project. Why the path swagger-ui.html will point to the content under springfox-swagger-ui-2.6.1.jar!\META-INF\resources\webjars\springfox-swagger-ui. Why doesn't it look for the web content under other jar file? How does it work? Thanks.

Related

404 error even after mapping /resource/** to /resource/ in Spring

I have mapped my resources locust.js and locust.css files to the folder, resources, as shown in the image:
Directory Structure
The mvc-config.xml file has the following configuration settings:
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
I am getting 404 error when I try to load the locust.js and locust.css files. The jsp files are rendering properly.
Any clue where am I wrong here?
Spring was unable load the locust.js and locust.css files because my root ("/") folder was pointing to "webapp" folder and so spring was unable to resolve the path as the resources were outside the 'webapp' folder.
Thanks.

Image not getting displayed in Spring MVC

I know this question has been asked countless times.I also found many solutions but the image is not displaying.Getting error resource not found.I have done a fairly basic example.
Please help me.
i am new to spring
spring-servlet.xml
<context:annotation-config />
<context:component-scan base-package="com.app" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/pages/" p:suffix=".jsp">
</bean>
In display.jsp i have written the following code
<img src="<c:url value="/resources/img/a.jpg" />" />
I have created the resouces/img folder directly under WebContent.
The error get in browser console
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:7070/SpringStaticDemo/resources/img/a.jpg
Thank u for the help in advance and sorry for asking such a basic question.
Don't know what was wrong.But I restarted Eclipse and now everything is working fine.It is showing the image. Don't know what happened.

I cannot access Robots.txt in Spring-MVC

I am trying to give access to robots.txt in Spring-MVC. To test the code, I put robots.txt in WebContent,Root and WEB-INF but I cannot access to any of them.
I've already applied answers of these questions 1,2,3 to no avail.
MyCode
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/robots.txt" location="/robots.txt" order="0" />
<mvc:annotation-driven />
This works for me:
Put robots.txt directly under webapp
In mvc-dispatcher-servlet.xml have:
<mvc:default-servlet-handler/>
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />
Use maven to build your war

Image issue in Spring MVC

This is my project folder structure.
I have resources folder under webapp.
Source
_ main
-webapp
- resources
adminlogin.jpeg
I have one jsp page adminlogin.jsp which contain 1 image.But When ever I try to access this page I get page but not image.
This is my SpringDispatcher-servlet.xml
<i><context:component-scan base-package="ShoppingOnline" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:annotation-config />
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</i>
This is my jsp page
<html>
<body>
<img src="/adminlogin.jpeg" alt="ADMIN LOGIN">
</body>
</html>
I have seen many example in your site very one has given one soultion.
but no has explain how its work in folder structure with example.I have made project in eclipse with the help of maven and In my folder structure the resources file is inside the web-inf folder.I haven't seen a single good example in this sites.
Please if u explain,explain with a good example how it is work if the resource is inside the web inf folder and when the resources is outside the web inf folder.
Please also let me know my mistake.
Thanks for helping.
Add your static files to webapps > resources
servlet.xml
<mvc:resources mapping="/resources/**" location="/resources/" />
then static files can be accessible from the page.
in Jsp:
<img src="/resources/images/yourimage.jpg" />

css file is not working in Spring Theme module. properties files are loaded properly but css are not working

I configured my confugration file ie. dispatcher-servlet.xml file for Themes using following beans
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="theme-" />
</bean>
<!-- Theme Change Interceptor and Resolver definition -->
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value="theme" />
</bean>
<bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="default" />
</bean>
I have added 3 properties
as theme-black.properties,theme-blue.properties,theme-default.properties under the source directory.
in each properties file I added key-value pair as following
style=style/blue.css
style=style/black.css
style=style/default.css
i put style folder under Web-Content .
problem is this ResourceBundleThemeSource loaded properties file successfully but could not able to load css file.
In JSP file I have added follwing code
<link rel="stylesheet" href="<spring:theme code='style'/>"
type="text/css" />
for changing
<span style="float: right;"> <a href="?theme=default">
default</a> | blue | black
</span>
Please help me out if any issue is there........ please please please > Thanks in Advance
I don't have experience with Spring's theme support, however I spot a little mistake in your URL.
First your URLs are relative. You should always have absolute URLs (starting with /).
Second, when using any URL, you should use <c:url> or <spring:url> so that correct context prefix is used:
<spring:url var="cssUrl">
<jsp:attribute name="value"><spring:theme code="style"/></jsp:attribute>
</spring:url>
It is as simple as specifying the location of your static content (in you application-config.xml), and the path from which to access them:
<mvc:resources mapping="/resources/**" location="/resources/"/>
And next you edit the themes properties file with :
style=resources/style/blue.css
And put blue.css file into: webapp/resources/themes
Now you can load css file using:
"resources/themes/blue.css" in href
Or using style in code of spring:theme tag.
This works for me.
I don't know whether you have put the bean themeChangeInterceptor ref under bean DefaultAnnotationHandlerMapping . If you have done that , then please remove that from there and put that inside the <mvc:interceptors>tag like below
<mvc:interceptors>
<ref bean="themeChangeInterceptor" />
</mvc:interceptors>

Categories

Resources