How to display images in a jsp file in Spring MVC - spring

I am very new to spring. I am using spring 3.29 version, tomcat 7. I need to display an image in .jsp file. I searched a lot. There are plenty of post about this problem. But still i am unable to solve this problem. Please help.
The following is my application structure
The following is my spring-servlet.xml file code
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.wipro.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
I have a page footer.jsp. I need to add an image in this file
The following is code
<!-- Here I need to add an image -->
<hr/>
<p>Copyright 2010-2014 javatpoint.com.</p>

I got the image in my page. #Pankaj Saboo comments helped a lot. Thanks for all of you.
I have added the below code in spring-servlet.xml
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="WEB-INF/resources/images/" />
And in footer.jsp I have added the below code
<img src="<c:url value="/images/wipLogo.png" />"/>

Add following line in your spring-servlet.xml
<resources mapping="/images/**" location="/images/" />
and on jsp
<link rel="icon"
href="http://<hostname>/projectname/images/imageName"/>

Try adding the following resources declaration to your Spring configuration:
<!-- Handles HTTP GET requests for /images/** by efficiently serving up static resources in the ${webappRoot}/images directory -->
<resources mapping="/images/**" location="/images/" />
Alternatively, and more common, is to have a resources folder which contains all your resources (images, css, js, etc...), broken out by sub-directories.
Your configuration would then look like:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
And your resources would be referenced as follows:
<link rel="stylesheet" type="text/css" href="<c:url value="/resources/css/screen.css" />" />
<script type="text/javascript" src="<c:url value="/resources/js/jquery-1.6.4.min.js" />"></script>
<img src="<c:url value="/resources/images/left_arrow.png" />"

Related

My bootstrap style is not rendering prperly in my spring mvc application

Iam developing a E-musicstore application using spring mvc and hibernate.When I add header.jsp file to my ViewproductList.Jsp file The page is not rendering my bootstrap style properly.
my dispatcher servlet configuration
this is how its displayed in browser
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Interceptor -->
<context:component-scan base-package="com.mymusic" />
</beans:beans>
Try this... if you face same issue, please attatch your brower's NETWORK Tab which i can see HTTP information.

SpringData and MongoDB configuration error : MongoTemplate

I am currently attempting an integration of mongodb to my spring application.
I have the initial mongodb environment setup on my machine, service startup, etc.
However on the application configuration in spring there is an error related to the mongoTemplate construction-arg
"wildcard is strict but no declaration found for element"
I've actually used the same xml outlay as described in the documents here, the same as some recent tutorials online, and as far as I can tell I have addded the correct namespace urls. So i am a bit lost at the moment as to what I am missing. Any help on this is most appreciated.
Here is a look at the xml configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- scanning comment root context of all components package directory-->
<context:component-scan base-package="com.demo" />
<!--Dispatcher servlet-->
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/view/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
<!-- mongo db config -->
<mongo:mongo host="localhost" port="27017" id="mongo" />
<mongo:repositories base-package="com.demo.football.repository" />
<beans:bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo"/>
<constructor-arg name="databaseName" value="FootballManager"/>
</beans:bean>
</beans:beans>
Qualify the xsd names to match the spring version you're using.
Considering you're using Spring 4, the xshema name space will change to following.
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd">

Serving static content with Spring

I've been following a couple of tutorials on serving static files (CSS, JS etc.) using Spring and from what I can see I should be able to see my static files but I can't.
My spring-web-servlet.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="brass.ducks" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/styles/**" location="views" />
<mvc:annotation-driven/>
</beans>
The last line suggests that any URL that follows styles/whatever.css should look in my views folder and serve from there. For example my page has a link to a stylesheet as /styles/css/bootstrap.min.css (which would complete to http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css) but this does not resolve to my style sheet.
My folder hierarchy looks like this:
Am I missing something?
EDIT
As suggested I extracted my .war file and found the folder hierarchy looks like this:
I've modified my web.xml to look like this:
<mvc:resources mapping="/styles/**" location="/WEB-INF/views/" />
<mvc:annotation-driven/>
But the URL http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css still does not resolve a CSS file.
Move your styles directory to /webapp/resourses path. So you will have /webapp/resourses/styles. Change xml config with <mvc:resources mapping="/styles/**" location="/resources/styles/" and try to get from http://localhost:8080/Brass-Ducks/styles/css/bootstrap.min.css
Change your mapping config to:
<mvc:resources mapping="/styles/**" location="/WEB-INF/views/styles/" />
Yes, you are missing the /, it should look like this:
<mvc:resources mapping="/styles/**" location="/views/" />
and XML is not really a good idea to configure spring, try java config..

How to Set “exclude-mapping” in Spring MVC Interceptor

Here is my XML configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
...
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
... ">
<resources mapping="/resources/**" location="/resources/" />
<resources mapping="/style/**" location="/resources/style/" />
<resources mapping="/script/**" location="/resources/script/" />
<resources mapping="/images/**" location="/resources/images/" />
<resources mapping="/uploaded/**" location="/resources/uploaded/" />
<interceptors>
<interceptor>
<mapping path="/**" />
<exclude-mapping path="/style/**" />
<exclude-mapping path="/script/**" />
<exclude-mapping path="/images/**" />
<exclude-mapping path="/uploaded/**" />
<exclude-mapping path="/resources/**" />
<beans:bean class="com.example.MyInterceptor" />
</interceptor>
</interceptors>
</beans:beans>
My issue is when I browse a file in /style or other path in exclude mappins, such as /style/style.css, the interceptor code still run. Even if I change the config to <exclude-mapping path="/style/*" />, the issue still happen.
How do I set the config file to let <exclude-mapping> work?
I found the solution.
As what gamerkore said in About mvc:intercepter,how to set excluded path said, spring add this feature in version 3.2 (my project had used ver. 3.1.1.RELEASE). It can work after updating the version of spring (I updated it to ver. 4.2.0.RELEASE)

xml spring social xmlns:facebook

I want to build a login with spring social, and I have implemented a *xml whit the next configuration, but the url http://www.springframework.org/schema/social/facebook is wrong?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:facebook="http://www.springframework.org/schema/social/facebook"
xmlns:twitter="http://www.springframework.org/schema/social/twitter"
xmlns:social="http://www.springframework.org/schema/social"
xmlns:linkedin="http://www.springframework.org/schema/social/linkedin"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/social/facebook http://www.springframework.org/schema/social/spring-social-facebook.xsd
http://www.springframework.org/schema/social/linkedin http://www.springframework.org/schema/social/spring-social-linkedin.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/social/twitter http://www.springframework.org/schema/social/spring-social-twitter.xsd
http://www.springframework.org/schema/social http://www.springframework.org/schema/social/spring-social.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:/itrippingWeb/src/main/resources/application.properties" />
<facebook:config app-id="${facebook.clientId}" app-secret="${facebook.clientSecret}" app-namespace="socialshowcase" />
<social:jdbc-connection-repository/>
<bean id="userIdSource" class="org.springframework.social.security.AuthenticationNameUserIdSource" />
<bean id="connectController" class="org.springframework.social.connect.web.ConnectController" autowire="constructor">
<property name="connectInterceptors">
<list>
<bean class="org.springframework.social.showcase.facebook.PostToWallAfterConnectInterceptor" />
<bean class="org.springframework.social.showcase.twitter.TweetAfterConnectInterceptor" />
</list>
</property>
</bean>
<bean id="psc" class="org.springframework.social.connect.web.ProviderSignInController" autowire="constructor" />
<bean id="signInAdapter" class="org.springframework.social.showcase.signin.SimpleSignInAdapter" autowire="constructor" />
<bean id="disconnectController" class="org.springframework.social.facebook.web.DisconnectController"
c:_0-ref="usersConnectionRepository" c:_1="${facebook.clientSecret}" />
</beans>
And this xml retrieve me the next exception:
Multiple annotations found at this line:
- cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element
'facebook:config'.
- schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/social/spring-
social-facebook.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element
of the document is not <xsd:schema>.
Anybody knows the problem?
thank you!!!!
I think this has to do with your pom missing some of the dependencies.
Is spring-social-security in your dependency?
<dependency>
<groupId>org.springframework.social</groupId>
<artifactId>spring-social-security</artifactId>
<version>1.1.0.RELEASE</version>
Here is a similar application with the same config as your spring xml above. Here is the pom it uses https://github.com/spring-projects/spring-social-samples/blob/master/attic/spring-social-showcase-xml/pom.xml
Here is a reference to a similar error resolved by fixing the pom dependency https://github.com/spring-projects/spring-social-facebook/issues/79
Actually the url "http://www.springframework.org/schema/social/facebook" links to nowhere.
I found another schema files location: http://docs.spring.io/autorepo/schema/
I used "http://docs.spring.io/autorepo/schema/spring-social/current/social/spring-social.xsd" and it works for me.

Resources