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>
Related
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.
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.
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?
I've created Spring 3 MVC project via Spring Tool Suite template and I have integrated Spring security there.. Everything works except accessing the static content.
When I create only MVC app and have my static content in /src/webapp/WEB-INF/resources/and put <resources mapping="/resources/**" location="src/main/webapp/WEB-INF/resources" /> to my applicationContext.xml, it works well...But I can't add this code to my applicationContext.xml with security...the code doesn't even compile..Any idea what to write to my web.xml to make this work?
My applicationContext.xml file 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:security="http://www.springframework.org/schema/security"
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/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="cz.cvut.fit.genepi.controllers" />
<import resource="classpath:applicationContext-security.xml" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
It's weird that when using code above, mapping view-controller works allrigh, but when I'm using this, I get this error The prefix mvc:resources is not bound
<?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:security="http://www.springframework.org/schema/security"
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/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="cz.cvut.fit.genepi.controllers" />
<import resource="classpath:applicationContext-security.xml" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans>
structure of the solution:
The code should be in your app context definition(applicationContext.xml), and the location is relative to deployment root :
<mvc:resources location="/resources/" mapping="/resources/**"/>
you'll need this at top of config file
xmlns:mvc="http://www.springframework.org/schema/mvc"
and then
xsi:schemaLocation = http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
I think there may be someo confusion, the normal wdirectory structure is something like this:
src/main/java
src/main/resources
src/main/webapp
src/main/webapp/WEB-INF
src/main/webapp/WEB-INF/jsps
src/main/webapp/css
The css directory is just an example, I acually have javascipt, and image directries as well, some people prefer just one called for example "static-assets". But calling it resources is rather confusing. The src/main/resource/ directory actually contains config files for the whole project (I put my appContext.xml's in there and log.properties files), it gets copied to WEB-INF on deployment and should not be used for mapping an static resources.
Eg, in my example woul dactualyl be mapped like this :
<mvc:resources location="/css/" mapping="/css/**"/>
I want to create my wsdl by spring-ws automatically and I inserted the code below to my app context file, but I got the error;
"Cannot locate BeanDefinitionParser for element [dynamic-wsdl]"
what does that mean and what can I do? tnx
<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:sws="http://www.springframework.org/schema/web-services"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org /schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="payloadMapping"
class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="defaultEndpoint" ref="inferenceEndPoint" />
<property name="interceptors">
<list>
<ref local="validatingInterceptor" />
<ref local="payLoadInterceptor" />
</list>
</property>
</bean>
<bean id="payLoadInterceptor"
class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
<bean id="validatingInterceptor"
class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
<description>
This interceptor validates the incoming
message contents
according to the 'Request.xsd' XML
Schema file.
</description>
<property name="schema" value="/WEB-INF/schemas/Request.xsd" />
<property name="validateRequest" value="true" />
<property name="validateResponse" value="false" />
</bean>
<bean id="inferenceEndPoint" class="com.mywebsite.ws.web.InferenceEndPoint">
<property name="messageService" ref="messageService" />
</bean>
<bean id="messageService" class="com.mywebsite.ws.service.MessageService">
<property name="inferenceService" ref="inferenceService" />
</bean>
<bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
<property name="xsd" value="/WEB-INF/schemas/Request.xsd" />
</bean>
<sws:dynamic-wsdl id="mtwsdl"
portTypeName="mtWS"
locationUri="http://localhost:8080/mws/">
<sws:xsd location="/WEB-INF/schemas/Request.xsd" />
</sws:dynamic-wsdl>
<bean id="inferenceService" class="com.mywebsite.ws.im.InferenceService">
<property name="webServiceConfiguration" ref="playerConfiguration" />
</bean>
<!-- <bean id="inferenceConfig" class="com.mywebsite.ws.im.InferenceService">
<constructor-arg ref="playerConfiguration"/> </bean> -->
<!-- ~~~~~~~ Application beans ~~~~~~~ -->
<bean id="playerConfiguration"
class="com.mywebsite.ws.configuration.WebServiceConfiguration"
init-method="init">
<property name="playerConfigXml" value="/WEB-INF/config/webserviceconfiguration.xml" />
<property name="executingPathResource" value="/WEB-INF" />
<property name="developmentMode" value="true" />
</bean>
Replace the first section of your appcontext where you define namespaces:
<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:sws="http://www.springframework.org/schema/web-services"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
I highly suggest to use Maven. The error you are getting is due to a missing library. In Maven you should have an entry like the following.
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
I suggest you look at the class path and also server run time path, I think you may have both (spring-ws 1.5.x and spring-ws 2.x) version's of jar files either in the compile/run time path. If that is not the case clean up the both class and run time path and add only spring-ws 2.x jar files.
As for the differences, when spring framework name space handler (WebServicesNamespaceHandler) encounters (dynamic-wsdl tag in spring context file), It will register a (DynamicWsdlBeanDefinitionParser) bean with all the properties specified in the dynamic wsdl tag. It is essentially same as you registering (DefaultWsdl11Definition) bean in spring context.