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.
Related
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
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/**"/>
My project works well with "/" pattern. But when i connect js and css it doesnt work as well as want, 'cause dispatcher servlet doesnt map css and js. When I specified ".htm" pattern css and js work, but all my pages(e.g. "/polls/categories") dont work.
This is my web.xml file.
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And dispatcher-servlet:
<?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:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="1"/>
</beans>
Add this bit to your dispatcher-servlet.xml :
<resources mapping="/resources/**" location="/resources/" />
You should put static resources inside this location :
PROJECT_ROOT/src/main/webapp/resources
And then access them from your views like this (example for css):
<link rel="stylesheet" href="<c:url value="${webappRoot}/resources/css/style.css" />" type="text/css">
Answer update (dispatcher-servlet.xml) :
<?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"
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">
<!-- 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/jsp/" />
<beans:property name="suffix" value=".jsp" />
<beans:property name="order" value="1" />
</beans:bean>
</beans:beans>
Not sure what are you trying to accomplish with this bit here:
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
But if you're trying to create controller all you have to do is to add annotation to your class like this :
#Controller
class ControllerClassNameHandlerMapping{
....body omitted..
}
Update 2
If this doesn't help, why do the things hard way. Download spring tool suite (easy to google). With this tool you can just create new spring project and the tool will create appropriate structure with valid configurations. And check this one out, really short and concise :
https://github.com/SpringSource/spring-mvc-showcase/blob/master/MasteringSpringMVC3.pdf?raw=true
I'm trying to use resources mapping feature of Spring 3 and it does not seem to be working. Here is what I have:
<servlet>
<servlet-name>aaa</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>aaa</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
in my web.xml
then in my aaa-servlet.xml I have the following:
<resources mapping="/resources/**" location="/resources/" />
I'm accessing the content in jsp like this:
<link href="<c:url value="/resources/css/blueprint/screen.css" />" rel="stylesheet" type="text/css">
Everything I've been reading suggests that I have it all setup correctly however it is not working. I'm using weblogic server and on startup it does map the /resources/ folder.
Any help would be greately appreciated!
aaa-servlet.xml in its entirety:
<?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:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- 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/jsps directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/jsps/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basename" value="messages"/>
</beans:bean>
<!-- Imports user-defined #Controller beans that process client requests -->
<beans:import resource="controllers.xml" />
controllers.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"
xmlns:util="http://www.springframework.org/schema/util"
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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Scans within the base package of the application for #Components to configure as beans -->
<context:component-scan base-package="com.app.controller" />
Startup log:
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0'
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
You are mapping your app to the root context, so you should probably include
<mvc:default-servlet-handler/>
in your mvc config. Have a look at 15.12.5 in the spring docs. I wasn't able to get mvc:resources to work without that setting when my dispatcher servlet was mapped to /. At least, that's what I seem to remember having to do when I configured this a couple of months ago in my project.
Try replacing your <resources mapping="/resources/**" location="/resources/" />
with
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
Got it fixed!
Looks like there is a bug in ResourceHttpRequestHandler which only appears with Weblogic.
To fix the issue:
Remove
<resources mapping="/resources/**" location="/resources/"/>
Add
<default-servlet-handler/>
I know some of you suggested using the default-servlet-handler but the resources mapping must be removed in order for it to work!