Using Spring 3.0.2.RELEASE. I'm having 2 Controllers in package com.myCompany. The Controllers are activated via Component-scan
<context:component-scan base-package="com.myCompany" />
then I'm having a interceptor bind to the 2 controllers via
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="myInterceptor"/>
</list>
</property>
</bean>
How can i bind the interceptor to only one specific Controller or to only certain methods inside a Controller?
Background: I want to inspect the URL that it contains certain parameters
Docu Link
When you inject interceptors into a HandlerMapping bean, those interceptors apply to every handler mapped by that HandlerMapping. That was fine in the pre-annotation days, since you'd just have configure multiple HandlerMapping beans. However, with annotations, we tend to have a single DefaultAnnotationHandlerMapping that maps everything, so this model doesn't work.
The solution is to use <mvc:interceptors>, where you explicitly map paths to interceptor beans. See the docs, and this example:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/secure/*"/>
<bean class="org.example.SecurityInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
Related
I want to make an interceptor that will intercept every request except a few ones. The problem that I have is the interceptor still intercepts the requests that I provided with exclude-mapping. I tried all sorts of variations but nothing worked. Here is the configuration
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/checkout/campaign/**"/>
<bean class="com.package.package.package.package.package.CampaignBeforeControllerHandler" >
<-- list of services -->
</bean>
</mvc:interceptor>
And here is an actual request: https://localhost:9002/checkout/campaign/test . In my opnion the pattern matches this request so it should be excluded but it is not, I still get into the class of the interceptor. Is the pattern that I provided somehow bad?
EDIT: I am using Spring MVC 3.2.8
probably you have adding 'mvc' to inner elements that causes
Doc example
<mvc:interceptors>
<mvc:interceptor>
<mapping path="/**"/>
<exclude-mapping path="/checkout/campaign/**"/>
<bean class="com.package.package.package.package.package.CampaignBeforeControllerHandler" >
<-- list of services -->
</bean>
</mvc:interceptor>
</mvc:interceptors>
I'm developing an application in JSP using SimpleFormController with Spring MVC 3.0.2 using Hibernate. Everything is fine. I'm also using Validator to validate forms on the server side. It's also going on well.
Now, I need to use Ajax as an example, when a country is selected from a drop down (<form:select><form:option></form:option></form:select>), the states corresponding to that country should be populated from the database in the state drop down.
I have done such things using Ajax in places but yet not with Spring MVC. I have gone through many tutorials/articles about SimpleFormController on Google but none of them were using Ajax. I couldn't find a single idea about how to use Ajax with SimpleFormController.
With annotated controllers (#Controller), the thing can be made easy because methods can be mapped using the #RequestMapping annotation (nevertheless I haven't yet used it but I think I can).
But with SimpleFormController, I don't have any precise idea about how to handle Ajax requests in the Spring controller (which methods to be mapped and how). With SimpleFormController, I'm usually associated with the onSubmit(), showForm() and referenceData() methods.
Could you please expose some thoughts on how can an Ajax request be made on SimpleFormController, which methods can be mapped and how? (I don't want the full code anymore. A very simple example (if and only if it's possible) or furthermore specific links where the use of Ajax with the SimpleFormController is explained would be quite enough for me to study).
You could always just have a separate #Controller to handle the ajax requests. If you can have custom jsp on the view, there is nothing stopping you from handling an ajax request on the page. Just bind the onchange event of the select box to an ajax call pointing to the other Controller you made.
In terms of keeping it bound to just that SimpleFormController, I don't think this is possible, but if you create a new RESTful controller that the form would use, other parts of the website will be able to use this new controller as well.
In complement to dardo's answer, using both spring MVC 2 and MVC 3 controllers is possible, but a bit tricky to set up.
In order to use both SimpleFormController and #Controller controllers in the same Spring context, I used to following definition :
<!-- ======== MVC Spring 3.x ======== -->
<!-- Scans within the base package of the application for #Components to configure as beans #Controller, #Service, #Configuration, etc. -->
<context:component-scan base-package="com.your.package" />
<!-- Enables the Spring MVC #Controller programming model -->
<!-- It's a shortcut equivalent to the (more complete) bean definition below (see bean AnnotationMethodHandlerAdapter).-->
<!--<mvc:annotation-driven />-->
<!-- This HandlerAdapter will register spring 3.x controllers (#Controller) into the DispatcherServlet -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<array>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="writeAcceptCharset" value="false"/>
</bean>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</array>
</property>
</bean>
<!-- This HandlerMapping allows to map urls defined in #RequestMapping annotations to the corresponding controllers -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
</bean>
<!-- ======== MVC Spring 2.x ======== -->
<!-- This HandlerAdapter will register spring 2.x controllers (#Controller) into the DispatcherServlet -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- Url mapper -->
<bean id="urlMapper" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/foo.do" value-ref="fooController" />
<entry key="/bar.do" value-ref="barController" />
...
</map>
</property>
</bean>
I'm new to spring and its bean injection framework and I need advice on understanding how to utilize them. Currently I have the following,
<beans>
<bean id="citationService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/PPDFWeb/hello.htm"/>
<property name="serviceInterface" value="test_client.HelloService"/>
<property name="httpInvokerRequestExecutor">
<bean class="org.springframework.security.remoting.httpinvoker.AuthenticationSimpleHttpInvokerRequestExecutor">
</bean>
</property>
</bean>
</beans>
Now i need the domain name of the service url to be dynamic so I can set it somewhere programmatically in my code. Is there some way to leave the bean intact in the xml and change the serviceUrl of the bean?
Just mark it like:
<property name="serviceUrl" value="{serviceURL}"/>
Edit:
There are just too many solutions available on SO, I didn't do a good search earlier:
reading a dynamic property list into a spring managed bean
HttpInvokerProxyFactoryBean-set-url-dynamically
Which interceptor can use in Spring 3.0 ?
I want to get Action name and method name in this.
HandlerInterceptorAdapter is working , but i cant get the method name and action name on this..
Any Help..?
dispatcher-servlet.xml
<!-- Handle Interceptor -->
<mvc:interceptors> <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
/> <mvc:interceptor> <mvc:mapping path="/*" />
<bean class="com.asd.MyInterceptor"
/>
</mvc:interceptor> </mvc:interceptors>
Implement HandlerInterceptor interface. One of the parameters in preHandle and postHandle methods of that interface is Object handler. It is in fact a HandlerMethod instance which should provided you everything you need.
I have a Spring 2.5.x application which I'm migrating to Spring 3 and just bumped into a little problem.
I have an handler mapping like so:
<bean id="handlerMappings1" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="interceptor1" />
<ref bean="interceptor2" />
....
<ref bean="interceptorN" />
</list>
</property>
<property name="urlMap">
<map>
<entry key="/url1.html" value-ref="controller1" />
<entry key="/url2.html" value-ref="controller2" />
....
<entry key="/url100.html" value-ref="controller100" />
</map>
</property>
</bean>
and another one like this:
<bean id="handlerMappings2" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/urlA.html" value-ref="controllerA" />
<entry key="/urlB.html" value-ref="controllerB" />
....
<entry key="/urlN.html" value-ref="controllerN" />
</map>
</property>
</bean>
I'm slowly replacing both with #RequestMapping annotations with a <context:component-scan> (which basically registers a DefaultAnnotationHandlerMapping).
In Spring 3 I saw the <mvc:interceptors> tag which can be used to add interceptors to certain URLs but you can specify only one interceptor, at least that's what I see from the schema.
From what I can figure, I have to register one of these for each interceptor which will duplicate all my URLs for as many times as I have interceptors (and I don't even know in what order they will run).
On the other hand I can't add the iterceptors on the DefaultAnnotationHandlerMapping because they will run for all my controllers annotated with #RequestMapping and I don't want that.
So how can I specify interceptors is Spring 3 for some URLs, without repeating the URL's and
keeping the URL to controller mapping based on the #RequestMapping annotation?
You could have a look at the SelectedAnnotationHandlerMapping and the IgnoreSelectedAnnotationHandlerMapping classes from the springplugins project. The sources are some years old but the idea still stands.
There is a presentation on the creator's blog here: Spring Framework Annotation-based Controller Interceptor Configuration. Make sure you also read the comments to the blog post.
One option would be to create a custom interceptor which can delegate to a collection of injected interceptors.