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?
Related
I study for maintain an old web-app (Spring 2.5, Java/JDK 1.6, Eclipse IDE 2022-06, Windows 10 x64).
Learned from tutorial https://mkyong.com/spring-mvc/spring-mvc-jquery-autocomplete-example/
File mvc-dispatcher-servlet.xml
<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/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.mkyong" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
I need convert this web-app (Spring 3.0) to Spring 2.5, how to revise? What is the same of Spring 3.0's mvc:annotation-driven in Spring 2.5?
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>
I use the STS to creat Spring MVC project, STS version is 3.7.3.RELEASE.
the default spring xml configuration file is :
<?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="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="test.dmh.hw" />
</beans:beans>
but when I configure my own HandlerInterceptorAdapter, I found the config file is not meeting my requirement. because the head is :
beans:beans xmlns="http://www.springframework.org/schema/mvc"
So I have to edit the file 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-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.xsd">
<context:component-scan base-package="com.shanshan.bo" />
<!-- 配置拦截器 -->
<mvc:interceptors>
<!-- 多个拦截器,按顺序执行 -->
<mvc:interceptor>
<mvc:mapping path="/**"/> <!-- 表示拦截所有的url包括子url路径 -->
<bean class="com.shanshan.bo.interceptor.LoginHandlerInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
change the head : beans xmlns="http://www.springframework.org/schema/beans"
Now I want the file format is this when I creat the project, so I don't have to change it anymore.
How can I edit the springmvc.xml file content ?
Spring Tool Suite has Spring Config Editor, which you can use to edit namespaces.
To open file with Spring Config Editor, right click on the file and select Open With>Spring Config Editor. Check image below
Now you can edit the namespaces using the Namespaces tab at the bottom of this editor.
Following is a sample Namespaces tab
In my context file I want to add line to be able to access static content. Until I add it, everything works allright, but after I add it, I can't access pages that have some controller and I get this warning: WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/fit/] in DispatcherServlet with name 'mvc-dispatcher'. Before adding <mvc:resources mapping="/resources/**" location="/resources/" /> to context file, it 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"
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 />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<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>
Any idea how to fix this?
PS:those pages are secured with Spring security, but I don't think that this could be the problem.
Every dispatcher servlet ends with -servlet.
Try renaming your dispatcher servlet.
On initialization of a DispatcherServlet, the framework will look for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and create the beans defined there.
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/**"/>