Problems accessing CSS from JSP Spring MVC - java

I am having some major problems trying to get my external css file to be read by my jsp files. My project structure at the moment is the following:
My spring.xml file:
<?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:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- Uncomment and your base-package here:
<context:component-scan base-package="org.springframework.samples.web"/> -->
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/"/>
<context:component-scan base-package="model" />
<context:component-scan base-package="controller" />
<context:component-scan base-package="service" />
<context:component-scan base-package="config" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
My JSP file:
<link href="<c:url value="resources/css/main.css" />" rel="stylesheet">
I have tried alot of different kinds of paths and configurations but nothing seems to work.
Is there anyone who has any ideas what the issue seem to be ?
Would really appreciate any help.

Related

Resource Versioning with configuration xml-based (Spring)

I'm trying to use the VersionResourceResolver in order to prevent old js and css to be cached. Currently I'm using XML-Based Configuration for my Spring. I just copied this code:
<mvc:resources mapping="/resources/**" location="/public-resources/">
<mvc:resource-chain>
<mvc:resource-cache/>
<mvc:resolvers>
<mvc:version-resolver>
<mvc:content-version-strategy patterns="/**"/>
</mvc:version-resolver>
</mvc:resolvers>
</mvc:resource-chain>
</mvc:resources>
and placed it inside my xml called by the DispatcherServlet in order to start my web application. After placing this code in my servlet.xml I have something like this:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
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">
<context:property-placeholder location="/WEB-INF/application.properties" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
<mvc:resources mapping="/resources/**" location="/resources/">
<mvc:resource-chain>
<mvc:resource-cache />
<mvc:resolvers>
<mvc:version-resolver>
<mvc:content-version-strategy patterns="/**"/>
</mvc:version-resolver>
</mvc:resolvers>
</mvc:resource-chain>
</mvc:resources>
<mvc:resources mapping="/sources/**" location="/sources/" />
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<property name="proxyTargetClass" value="true" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="pt_BR" />
</bean>
</beans>
Eclipse tells me that are 2 errors in my servlet.xml:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'mvc:resource-cache'. One of '{"http://www.springframework.org/schema/mvc":resolvers, "http://www.springframework.org/schema/mvc":transformers}' is expected.
cvc-complex-type.4: Attribute 'resource-cache' must appear on element 'mvc:resource-chain'.
I don't know what could be since I'm using the last version of the mvc.xsd.
If anyone can help to find my mistake I'm grateful.
Yep, so a college of mine already solved this one. The Spring documentation is wrong, the right way to use Resource Versioning with XML-Based Configuration is:
<mvc:resources mapping="/resources/**" location="/public-resources/">
<mvc:resource-chain resource-cache="true">
<mvc:resolvers>
<mvc:version-resolver>
<mvc:content-version-strategy patterns="/**"/>
</mvc:version-resolver>
</mvc:resolvers>
</mvc:resource-chain>
</mvc:resources>

Spring MVC is not recognizing mvc:resource in config file

Update :Now Working properly. Kindly follow the comments.
This is my Spring configuration file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<context:component-scan base-package="report.frontcontroller"/>
<mvc:resources mapping="/resources/**" location="/resources/" />
<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>
</beans>
All jars are included in this project, and the configuration file is shown above. I get errors for this part:
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
Errors: * Matching wildcard is strict but no declaration is found for mvc:resource and mvc:annotation
* 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 'mvc:resources'.
- schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/mvc/spring-
mvc-3.0.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document
is not .
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 'mvc:resources'.
- schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/mvc/spring-
mvc.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document
is not .
can you try below configuration that is copied from GIT - spring-mvc-showcase
<?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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<resources mapping="/resources/**" location="/resources/" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<task:annotation-driven />
</beans:beans>
change to spring-mvc.xsd to spring-mvc-3.0.xsd .Similarly change at all the remaining places too.
Refer here
Edit
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">
<context:component-scan base-package="report.frontcontroller" />
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

Cannot resolve property `paramName` in Spring MVC

I need to set Spring MVC interseptors to catch url parameter language and accordingly get data from .properties file. Getting error Cannot resolve propertyparamName when configuring context in servlet-config.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven/>
<mvc:resources mapping="/pdfs" location="pdfs"/>
<context:component-scan base-package="com.ttu.cs.controller"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<bean id="LocaleResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" p:defaultLocale="en"/>
<!-- Declare the Interceptor -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="paramName" value="language"/>
</bean>
</mvc:interceptors>
</beans>
I think you meant to use org.springframework.web.servlet.i18n.LocaleChangeInterceptor rather than SessionLocaleResolver.
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language"/>
</bean>
This class is a HandlerInterceptor and does have a paramName property.
SessionLocaleResolver, on the other hand, is not.
Not sure what you expect but SessionLocaleResolver doesn't have such a param. Maybe you meant locale?

Spring configuration beans static resources mapping and url's mapping?

I just started with spring framework and i have a problem with my spring beans configurations.
To load my static resources such as css, js, etc. i had to put the following line:
<mvc:resources mapping=”/resources/**” location=”/resources/” /> in my spring bean configuration:
My problem is that if i put this line, for some reason all url's of my application are not mapped when i deploy my application. When i comment this line all the url's application are working just fine, but of course my static resources are not mapped.
I need some tips on how should i tackle this problem. Thank you !.
Also this is all my spring-config.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-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.application" />
<!-- Mapping the static resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<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/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Unimportant for my problem -->
<!-- Bean used for login from userDao(repository) -->
<bean id="customUserDetailsService" class="com.application.service.CustomUserDetailsService"></bean>
<!-- Setup the userDao(repository) -->
<bean id="userDao" class="com.application.model.UserDAO" />
<!-- Declared datasource bean -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/licenta" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
</beans>
You need to add <mvc:annotation-driven /> to your spring-config.xml
Check out this similar problem

spring 4.0.2 schema error

I am trying to upgrade my spring 3.4 application to spring 4.0.2
I imported all the spring-4.0.2 jar files but am getting errors in the dispatcher-servlet.xml
if I use this code, I get Referenced file contains errors (http://www.springframework.org/schema/beans/spring-beans-4.0.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- automatic scan base folder to detect controllers -->
<context:component-scan base-package="com.shaw.csdss"/>
<mvc:annotation-driven />
<!-- This file will contain the view name mappings to the jsp -->
<bean id = "jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix"><value>/</value></property>
<property name="suffix"><value>.jsp</value></property>
<property name="order" value="1" />
</bean>
</beans>
I tried to remove the -4.0 from the xsd references but I get same error on spring-mvc-xsd now.
I even tried to discard everything and just copy the xml from the spring reference documentation as follow
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven />
<bean id = "jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix"><value>/</value></property>
<property name="suffix"><value>.jsp</value></property>
<property name="order" value="1" />
</bean>
</beans>
but with this simple file I get error on
cvc-complex-type.2.4.c: the matching wildcard is strict, but no declaration can be found of element 'mvcannotation-driven
Any example I take from spring4.0 reference document, gives me same error.
is this a problem with spring4.0.2 or am I missing something?
Thank you for your help

Categories