I use eclipse, springmvc and maven. I created a folder under
Web-Content --> WEB-INF --> resources
I added the following into my servlet:
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />
<mvc:annotation-driven />
Then I try to include a picture into my index.jsp file.(Web-Content->index.jsp)
<img src = "/resources/images/Logo.png" />
But it's not loading my image. I tryed many tutorials but it doesn't work. I guess I'm something basic wrong. Can you tell me whats wrong?
EDIT:
my serlvet got this
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
In my jsp file I did this:
<img src="<c:url value="/resources/logo.png" />"/>
I got resource folders with the logo.png file in the following places:
main->resources
main->webapp->resources
main->webapp->WEB-INF->resources
It still doesn't work
I also tried :
<mvc:resources mapping="/resources/**"
location="/, classpath:/WEB-INF/public-resources/"
cache-period="10000" />
and include the picture with
/resources/images/logo.png
and the picture located in
src/main/webapp/images/logo.png
doesnt't work for me
EDIT:
I guess it this:
<mvc:resources mapping="/resources/**" location="/resources/" />
got no effect to the program. I can always acces the url http://localhost:8080/test/resources/logo.png
doenst matter if I got this mvc line in my serlvet or not. If I change
<mvc:resources mapping="/resources/**" location="/resources/" />
to
<mvc:resources mapping="/resources1/**" location="/resources/" />
I can't acces to the file with http://localhost:8080/test/resources1/logo.png but with http://localhost:8080/test/resources/logo.png
Greets Sam
As an aside, while it is probably not best to make an Eclipse Dynamic Web Project and convert it to a Maven project through Eclipse, it will work, because Eclipse will put an entry into your POM that points to the WebContent directory. Have a look you will probably find it there. There is nothing wrong with making a Dynamic Web Project per se, but I typically change the initial directory configuration to match maven's expectations in the Wizard and convert to a maven project right away. Much easy to work on the POM than copy jars to the lib directory.
Otherwise, the resources directory you referring to should be in the root of your war file. The best way to get it there is to put resources at the root of your WebContent directory, not under WEB-INF. Try moving it there, open the .war file and inspect the contents to be sure it is in the root, and you should be good to go.
Related
I have a war file which not include the lib jars. I create share folder under the lib folder, put all jar files in it, and update the catalina.properties file. It's working on my Mac, but not the centos7 server. I check the centos7 tomcat log, it shows the log4j cannot be found, but the jar is in the share folder. What's other possible place to check?
thanks
catalina.properties
shared.loader="${catalina.base}/lib/share","${catalina.base}/lib/share/*.jar","${catalina.home}/lib/share","${catalina.home}/lib/share/*.jar"
tomcat 8.5.8
This my own mistake, add to the ../conf/context.xml file, the antiResourceLocking="false" privileged="true" should be remove!
correct sample:
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
</Context>
I have a Spring Boot Application. I am trying to load to my HTML file a CSS file, but probably I am doing something wrong. In Spring MVC Project I've done something like this:
In HTML file I added the following line:
<link href="<c:url value="/resources/css/style.css" />" rel="stylesheet">
And in mvc-dispatcher-servlet.xml I added following code:
<mvc:resources mapping="/resources/**" location="/resources/"
cache-period="31556926"/>
<mvc:annotation-driven />
And everything worked perfect.
Now I'm using thymeleaf templating language and load my CSS file like that:
<link rel="stylesheet" type="text/css" th:href="#{/assets/css/style.css}" />
My project files hierarchy is following:
In current project I don't have WEB-INF directory so I don't have such a files like mvc-dispatcher-servlet.xml or web.xml so I don't know if I should or where I should paste that code with resources mapping. Im completely new to Spring so I apologise for my lack of knowledge.
Any tips and solutions will be much appreciated.
I am utilising the Bootstrap front end framework, although I am having some trouble getting the icons to load. I have implemented Spring security, I am not sure whether this may be affecting them not to load. I am calling my .css and .js files like this:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/style/bootstrap.css" />
<script src="${pageContext.request.contextPath}/static/js/jquery.js"></script>
This is the configuration that I have in my Dispatcher servlet config:
<mvc:resources location="/resources/" mapping="/static/**" />
This is really irritating, can someone please help. Thanks
In your spring security config, have you excluded your resources directory containing your css, js, images from the secured zone ?
<http use-expressions="true" security="none" pattern="/resources/**">
</http>
If the problem is only related to the twitter bootstrap icons, have you included the files glyphicons-halflings.png glyphicons-halflingswhite.png in a resources/img folder ?
I'm getting a 404 in Jetty if the mvc:resources tag in my web-context.xml is not mapped to the same filename:
<mvc:resources mapping="/some-file" location="/WEB-INF/js/some-file.js" />
Where as the following works fine:
<mvc:resources mapping="/some-file.js" location="/WEB-INF/js/some-file.js" />
How can I map files to a different file-name in the URL space?
I recommend you don't map single files but an entire directory.
<mvc:resources mapping="/js/**" location="/js/" />
As you can see the mapping attribute takes an ant path to match multiple files and directories.
For more information see http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config-static-resources
This is a "simple" problem and I am seeking both a how-to and/or a you're-dumb-don't-do-that. I am open to both.
I am building a war file and want the structure to be:
WEB-INF/
properties/
<my properties files>
classes/
...
spring/
<my spring files>
Is that dumb? I know that I can access the properties files though the property-placeholder but I'd rather not nest the properties in the classes section - it doesn't make sense to me.
So the Spring file looks like this:
<context:property-placeholder location="classpath:properties/*.properties" />
if I want to access them in the classes area. I thought that
<context:property-placeholder location="properties/*.properties" />
would let me just put the directory under WEB-INF directly...am I wrong (ps I think I am :) ).
Any advice?
This should work
<context:property-placeholder location="WEB-INF/properties/*.properties" />
WEB-INF is not the root of the of the web-app, so you need to add WEB-INF to the path.
spring-context-3.1.xsd
<xsd:attribute name="location" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The location of the properties file to resolve placeholders against, as a Spring
resource location: a URL, a "classpath:" pseudo URL, or a relative file path.
Multiple locations may be specified, separated by commas. If neither location nor properties-ref is
specified, placeholders will be resolved against system properties.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
You can't do it the way you want since the classpath for the Classloader will be the /classes directory and any jars in the /lib directory. This is the standard configuration for a war file.
Wars and ears have specific configurations which you have to follow for the files to be valid. If you think about it, it would make it difficult to have different vendors provide web containers that could deploy the same war file if there was no standard format. There is a pretty informative page here.
To achieve something similar to what you want, you can simply have directories of /classes/properties and /classes/spring and look them up appropriately from your classpath ("classpath:properties/myfile.properties).
I am not sure what you want to achieve. Here the method I use to inject the properties from a basic properties file to a bean:
In the spring files (XML bean definitions), I add the reference to my property file (myfile.properties):
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:myfile.properties" />
</bean>
and then I add my references to the properties (db.url is the URL address for my database connection, I kept only the bean properties referenced in my property file).
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"><value>${db.url}</value></property>
<property name="username"><value>${db.login}</value></property>
<property name="password"><value>${db.password}</value></property>
</bean>
By default, if the property is not defined in the property file, Spring uses the System Properties (this behaviour can be changed).