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.
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 working with SpringFramework and Java. I use Spring xml files to define the flow of the architecture and also the beans that will be used in the Java part.
I have two beans of the same class in my xml file, but they have different arguments for the constructor:
<bean id="beanA" class="Class" >
<constructor-arg><value>valueA1</value></constructor-arg>
<constructor-arg><value>ValueA2</value></constructor-arg>
</bean>
<bean id="beanB" class="Class" >
<constructor-arg><value>valueB1</value></constructor-arg>
<constructor-arg><value>valueB2</value></constructor-arg>-->
</bean>
Is there a way to set one of the beans as default in order to #Autowired it from Java? And, when I want to use the non default bean, apply the #Qulifier("beanName") annotation.
try primary attribute, eg
<bean id="b1" class="test.B" />
<bean id="b2" class="test.B" />
<bean id="b3" class="test.B" primary="true" />
this guarantees that b3 bean will be injected here
public class Test {
#Autowired
B b;
...
Finally I used a the next thing: I have a setter (setClassValue(Class classValue)) in the java code for the class I want to use. Then, I set the property autowire-candidate to false in the bean that it's not going to be the default one:
<bean id="beanA" class="Class" autowire-candidate="false">
<constructor-arg><value>valueA1</value></constructor-arg>
<constructor-arg><value>valueA2</value></constructor-arg>
</bean>
<bean id="beanB" class="Class" >
<constructor-arg><value>valueB1</value></constructor-arg>
<constructor-arg><value>valueB2</value></constructor-arg>
</bean>
Then, in the xml file where I'm defining the bean of the class that is going to #Autowired the Class, I use the java setClassValue(Class classValue) method on this way:
<bean id="classThatAutowire" class="ClassThatAutowire" >
<property name="classValue" ref="beanA" />
</bean>
In the Java code yo will have #Autowired the beanB and then, set the beanA. It's not the best practice, but it works.
I'm trying to advise (for logging purposes) methods that uses #RequestMappging annotations in a Spring Web App with an Aspect. Here is my aspect class:
#Aspect
public class InboundSpringPerfLoggerAspect extends AbstractInboundPerfLoggerAspect {
#Around(value = "execution(* *(..)) && #annotation(requestMapping)" , argNames = "pjp, requestMapping")
public Object doPerfLoggingForOperation(final ProceedingJoinPoint pjp, final RequestMapping requestMapping) throws Throwable {
return doPerfLogging(pjp, LOGGER_NAME);
}
My XML servlet configuration (MVC context) file is this:
<aop:aspectj-autoproxy/>
<!-- Logging aspect for inbound rest calls -->
<bean id="inboundServicePerfLoggerAspect" class="common.logging.aspect.InboundSpringPerfLoggerAspect"/>
<context:annotation-config />
<context:component-scan base-package="web.configuration" />
<context:component-scan base-package="web.controller" />
<context:component-scan base-package="web..service" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean name="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order" value="1" />
</bean>
</beans>
However at start up in DEBUG mode logs from Spring shows same message:
DEBUG [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] Rejected bean name 'inboundServicePerfLoggerAspect': no URL paths identified
And of course controllers classes are not properly advised. They worked fine but no Spring proxy AOP takes place.
Where is the problem?
Thanks
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>
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>