Serving static content with Spring - 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..

Related

Spring does not find messages.properties

I'm working on a maven project using Spring framework.
Instead of writing raw text in JSPs I prefer to use <spring:message .../> tag and register my messages in a .properties file.
I get this warning when requesting a page :
ResourceBundle [messages] not found for MessageSource: Can't
find bundle for base name messages, locale fr
Which then brings an exception about the message not being found (obviously).
Here's my project's hierarchy :
my project's hierarchy
Here's my springapp-servlet.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="app.core" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/">
</property>
<property name="suffix" value=".jsp">
</property>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="fr" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
<mvc:resources mapping="/public/**" location="/WEB-INF/resources/"
cache-period="31556926"/>
<mvc:annotation-driven />
</beans>
From the documentation:
The basenames follow ResourceBundle conventions: essentially, a fully-qualified classpath location. If it doesn't contain a package qualifier (such as org.mypackage), it will be resolved from the classpath root.
(emphasis mine)
So it should be under src/main/resources.

Size and Valid annotation not working in spring mvc project

I use #Size and #valid annotation in my spring mvc project but it did not work and did not display the message which i want to display.
Download the zip file from the link : http://hibernate.org/validator/downloads/
Extraxt the zip file
Copy all jars of folder hibernate-validator-x.x.x/dist/lib/required
( If hibernate-validator-x.x.x.Final.jar file is not present in the folder then download it and also copy this jar. )
Paste jars into /WEB-INF/lib folder in the spring mvc project.
Add requiered code into the spring-servlet.xml
<?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: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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.parth.hellocontroller"></context:component-scan>
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/studentmessages"></property>
</bean>
Go to properties of your project
Go to build path and add external jar from downloaded file
Right click on your #size and #valid annotation and import those things
done

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.

How does spring detects current persistenceUnitName?

I'm working on a spring and JPA project. I had configured my JPA Persistence Unit in the Persistence.xml and here's my spring configuration file.
My application works fine, but I didn't understand how does spring framework detects the Persistence Unit defined in my Persistence.xml file and injects it without being defined in my spring bean configuration file .
Can anybody answer me please ?
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="ma.professionalpartners.fireAppBusiness.dao"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fireApp-Domain" />
</bean>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="jpaTransactionManager" />
</beans>
You have provided the name for the persistence unit, when configuring the entityManagerFactory bean:
<property name="persistenceUnitName" value="fireApp-Domain" />
The persistence.xml file MUST be on certain paths, so that Spring simply searched in those locations. After finding the file, it parses the XML content, and if there is a single PersistenceUnit, that is made the default one. Of course, if you specify a name (as you did), then it looks exactly for that PersistenceUnit.

Spring MVC url-mapping

I made a simple web application by Spring mvc.
I want to use these URL
/user
/user/{id}
/user/create
/user/edit/{id}
in web.xml
first case
<servlet-mapping>
<servlet-name>SpringMVC1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
It works well.
but I can not read
http://localhost:8080/res/images/image.png - 404 error
in {my project path}/WebContent/res/images/logo.png
second case
<servlet-mapping>
<servlet-name>SpringMVC1</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
I can see image on http://localhost:8080/res/images/image.png
but http://localhost:8080/user/create - 404 error
What's wrong??
You need something like this in your XML:
<mvc:resources mapping="/res/**" location="/path/to/your/resources"/>
See 16.14.5. Configuring Serving of Resources
more detail explain..
in my spring configuration xml file
i append
<mvc:resources mapping="/res/**" location="/path/to/your/resources"/>
it have to append next..
append to root node - beans
xmlns:mvc="http://www.springframework.org/schema/mvc"
and append to xsi:schemaLocation
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
and append mvc:annotation-driven node.
<mvc:annotation-driven />
It is my spring configuration xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.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="com.test" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:annotation-driven />
<mvc:resources mapping="/res/**" location="/res/" />
</beans>
It works well.
Thanks Sean Patrick Floyd.

Resources