I used Netbeans to create a Spring MVC 3.0 app. I have a simple controller and JSP view. The JSP view appears correctly except for an image that doesn\'t render. My direc
I believe it can only reference images/css in the WEB-INF folder. Moving your Images
folder to WEB-INF should fix your problem.
anything under Web-Inf folder will be marked as private and can not be access by the browser. You suppose to move the image folder outside Web-Inf and under webapp.
Alter your web.xml file:
<servlet>
<servlet-name>spring-servlet</servlet-name>
<servlet-class>com.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring-servlet</servlet-name>
<url-pattern>/<url-pattern>
</servlet-mapping>
and add below configuration after it:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
If you have a better solution please share with me.
For set image in jsp file in Spring MVC framework :
You can simply set your image by following steps:
Step 1. Place images folder in resources
Step 2. write image path like : src="${pageContext.request.contextPath}/resources/images/logo.jpg"
A bit old, but let me add my bit here. It is generally preferred to hide the .css/ .js files from direct public access by placing them into the main/java/webapp folder instead of the WEB-INF.
The reasons are explained very well in the posts below:
I think you can put all the static data such as image, css, javascripts etc out side the WEB-INF and then call it the normal way :)