Declaring interceptors in a spring 4 application - java

I have tried to declare interceptors in my spring application within my spring.xml configuration file. I am getting the error that:
Line 18 in XML document from ServletContext resource [/WEB-INF/spring/spring.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 18; columnNumber: 16; cvc-complex-type.2.4.a: Invalid content was found starting with element 'interceptors'.
It also doesn't like my annotation-driven tag
<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"
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">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<context:component-scan base-package="com.app" />
<interceptors>
<bean class="com.app.interceptors.LoginLogoutURLInterceptor" />
<interceptor>
<bean class="com.app.interceptors.AccountServletInterceptor" />
<mapping path="/account/**" />
</interceptor>
<interceptor>
<bean class="com.app.interceptors.AdminServletInterceptor" />
<mapping path="/admin/**" />
</interceptor>
<interceptor>
<bean class="com.app.interceptors.HomePageInterceptor" />
<mapping path="/" />
</interceptor>
<interceptor>
<bean class="com.app.interceptors.RegistrationServletInterceptor" />
<mapping path="/register/**" />
</interceptor>
</interceptors>
<!-- 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/" />
</beans>
Am I declaring my interceptors in the correct configuration file?

You have to declare the namespace for Spring MVC XML elements in the header of your XML file:
<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">
And then use the mvc namespace for the interceptors element:
<mvc:interceptors>
<mvc:interceptor>
<!-- ... -->
</mvc:interceptor>
</mvc:interceptors>
See 17.16 Configuring Spring MVC in the Spring reference documentation for more information.

Related

Spring Controller not getting Scan

Controller and project structure:
I am new to Spring Development. I am working on a web application. There are some jsp pages where I need to show some images so I made a resource entry in spring-servlet.xml like this:
<mvc:resources mapping="/schedules/**" location="/schedules/" />
But as I am adding this entry after that my controller classes stop scan and non of my action detecting in says 404. but if am removing above entry from xml than my controllers detected properly. but after removing images urls are not found what am missing.
<context:component-scan base-package="com.spring" />
My recommendation is use latest spring version.
add <mvc:annotation-driven /> in your config files
check the resources folder line <mvc:resources mapping="/schedules/**" location="/schedules/" /> is everything under this folder.
Check the mvc XML namespace is at xmlns:mvc="http://www.springframework.org/schema/mvc" in your Spring servlet-context xml configuration.
The <resources> tag was introduced in Spring 3.0.4, so you need at least that version of Spring and of the xsd.
Thanks To all For your Kind Support. issue is resolved. actually there is a level of hierarchy in which xml tag should declare.
Below code works for me
<context:component-scan base-package="com.spring" />
<mvc:resources mapping="/schedules/**" location="/schedules/" />
<mvc:default-servlet-handler />
<mvc:annotation-driven/>
now my xml is looks like this
<?xml version="1.0" encoding="UTF-8"?>
<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-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-3.0.xsd">
<context:component-scan base-package="com.spring" />
<mvc:resources mapping="/schedules/**" location="/schedules/" />
<mvc:default-servlet-handler />
<mvc:annotation-driven/>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<!-- Register the welcome.properties -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="ApplicationResources" />
</bean>
<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>

NullPointer exception on managed property (service)

I am facing a null pointer exception on my app , I am annotating Dao with #Repository , the servive by #Service , controller with #Controller and service inside it with #ManagedProperty, I am suspecting my application context is not well configured so here there is:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xml:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- Enable Spring Annotation Configuration -->
<context:annotation-config/>
<!-- Scan for all of Spring components such as Spring Service -->
<context:component-scan base-package="com.domain.nameOfapp.*" />
<!-- Necessary to get the entity manager injected into the factory bean -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- Define Hibernate JPA Vendor Adapter -->
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
</bean>
<!-- Entity Manager Factory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jpa-persistence" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="packagesToScan">
<list>
<value>com.domain.nameOfapp.*</value>
</list>
</property>
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Detect #Transactional -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
Any help would be great! thanks
Your component scan needs to be as below,
<context:component-scan base-package="com.domain.nameOfapp" />
This will scan all the classes under this package including any sub-packages.
Also using #ManagedProperty, are you trying to autowire the spring service bean? I believe you should be using #Autowired.

spring mvc 4 + thymeleaf

thymeleaf are new and trying to integrate it in spring mvc 4.
By following this tutorial I ran into this problem:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.thymeleaf.templateresolver.ServletContextTemplateResolver]for bean with name 'templateResolver' defined in ServletContext resource [/WEB-INF/spring-mvc-config.xml];
Now I'm sure you have all the necessary jars, could depend on what?
attached the configuration file.
spring-mvc-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: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-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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="it.shakesoft.common.controller" />
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
</beans>
the problem was the lack of jar of thymeleaf, I had loaded only to spring mvc 4.

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'bean' - Spring config problems

I'm trying to set up Hibernate 4 with Spring 4, and I have run into some problems configuring it. Specifically, in my servlet-context.xml file, I followed a guide and added the following code blocks to this file:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>/WEB-INF/hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Both of these display the following error: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'bean'
The session factory bean also displays this error: Cannot locate BeanDefintionParser for element [bean]
Here is my entire `servlet-context.xml' file (excluding those two code blocks for brevity's sake, though please notice that they are actually in the file):
<?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:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx-4.1.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="prefix" value="/WEB-INF/views/" />-->
<!-- <beans:property name="suffix" value=".jsp" /> -->
</beans:bean>
<context:component-scan base-package="pear.pear.pear" />
</beans:beans>
How can I resolve these issues?
You set the default namespace for that XML file to mvc, so you'll need to prefix your element (beans:bean) or change the default namespace to beans (the usual approach).

Spring resources mvc tag issue

Iam working with spring maven hibernate . I have error in adding resources mvc tag in dispatcher servlet file.For adding css and images files i have to add my folders using resources tag for that i have added xmlns mvc and its schema location. But its not working. Below am adding my code. When i remove mvc and resources from it , its working.
Please help.
<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-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">
<!-- Enable annotation driven controllers, validation etc... -->
<import resource="classpath:ApplicationContext.xml"/>
<context:component-scan base-package="com.org.rolltickets" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"><value>/views/</value></property>
<property name="suffix"><value>.jsp</value></property>
</bean>
<bean id="appUserDao" class="com.org.rolltickets.publicapp.dao.impl.AppUserDaoImpl" >
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
</beans>
My images folder is inside webapp/images and css webapp/css. The folders are not inside resources folder of maven. How can i add these external folders as resources?

Categories

Resources