Announcement Announcement Module
Collapse
No announcement yet.
Dispatching & Annotations for mapping request to control Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dispatching & Annotations for mapping request to control

    I was just wondering
    I've been using Spring for 14days now :-$
    and I have some mind things:

    can't we define "mapping request to controller" through annotations.

    ----------------------------

    Something else is:
    if there exists something like DispatchAction from struts.
    so that you can define 1 bean and handle multiple request with it

    for instance handle all registration requests with one bean
    giving to this bean: different attributes
    registration.htm?call=login
    registration.htm?call=logout

    account.htm?call=create
    account.htm?call=edit
    account.htm?call=remove

    this way you can group business logic
    with the advantage that you have only one bean to configure and a single setter injection for that logic.

    Am I just talking sh*t here or is there some logic in it.
    just tell me I can handle it ;-)

    grtz 2Fast2BCn.

  • #2
    forget the second question

    this feature already exists (looked over it :-$)

    but the first is still going :-D
    (annotation for transaction might be something to?)

    Comment


    • #3
      Transaction annotations are in the works and it's already possible to have annotated controllers. Currently only through commons attributes (CommonsPathMapHandler I believe). I'm not fully up-to-date on what the story is with annotations. I'll have a look at this and update you, okay?

      regards,
      Alef Arendsen

      Comment


      • #4
        yeah that's fine.
        I knew about the commons attributes
        But I thought that with these new J2SE5.0 features, annotations is probably a better solution for that.

        maybe you can provide a 1.4 and 5.0 release (containing these new features like hibernate-annotation support and so on...)

        thx for checking it out

        Comment


        • #5
          any update on this topic?

          grts

          Comment


          • #6
            Maybe I didn't fully understand the question, so bear with me... Wasn't it the idea to separate the mappings from the code and put them into a config file so that we can change them without rebuilding the code? How does putting the mapping into annotation help that purpose - if not defeating it?

            Comment


            • #7
              Originally posted by manifoldronin
              Maybe I didn't fully understand the question, so bear with me... Wasn't it the idea to separate the mappings from the code and put them into a config file so that we can change them without rebuilding the code? How does putting the mapping into annotation help that purpose - if not defeating it?
              so (currently supported) commons attributes is not a good solution and using annotations isn't a good alternative ?

              Comment


              • #8
                Originally posted by 2Fast2BCn
                so (currently supported) commons attributes is not a good solution and using annotations isn't a good alternative ?
                Is that a question or just rhetorical?

                Don't get me wrong, I'm not saying annotations(or commons attributes for that matter) is a bad thing in general. It's just in this case I'm not so sure that it adds lots of value from XML-based configuration schemes, considering the volatility of mapping configuration.

                Comment


                • #9
                  I guess compiler validation can be considered as a good advantage.
                  I guess there are more...

                  don't know annotations enough to have a good judgment

                  Comment


                  • #10
                    Annotation mappings

                    I was getting tired of adding all the mappings in the SimpleUrlHandlerMapping beans so I made something that uses annotations to do it for me, which is pasted below. There is a couple little hacks, mainly a reflective call as the method I need to call is'nt public. Alternatively you could subclass SimpleUrlHandlerMapping and make it private, or Spring could be updated to make it public, which would be good for runtime configuration anyway, and then this could be added to the sandbox.

                    Code:
                    /**
                     * BeanFactoryPostProcessor that finds all the AbstractController classes with a @{link UrlHandlerMapping} annotation
                     * and adds them to the mappings for the SimpleUrlHandler bean given by the annotation values.
                     * @author Daniel Campagnoli
                     */
                    public class SimpleUrlHandlerAnnotationPostProcessor implements BeanFactoryPostProcessor {
                    
                       private Logger logger = Logger.getLogger(getClass());
                    
                       public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
                          
                          Map<String, SimpleUrlHandlerMapping> mappers = beanFactory.getBeansOfType&#40;SimpleUrlHandlerMapping.class&#41;;
                          Map<String, AbstractController> controllers = beanFactory.getBeansOfType&#40;AbstractController.class&#41;;
                          
                          for&#40;String name &#58; controllers.keySet&#40;&#41;&#41; &#123;
                             AbstractController controller = controllers.get&#40;name&#41;;
                             // see if this bean has the annotation
                             UrlHandlerMapping mapping = controller.getClass&#40;&#41;.getAnnotation&#40;UrlHandlerMapping.class&#41;;
                             if&#40;mapping != null&#41; &#123;
                                
                                SimpleUrlHandlerMapping urlMapping = mappers.get&#40;mapping.urlHandlerBean&#40;&#41;&#41;;
                                // check the SimpleUrlHandler bean with the name from the annotation exists
                                if&#40;urlMapping == null&#41; &#123;
                                   throw new NoSuchBeanDefinitionException&#40;"name", 
                                         "No HandlerMapping object found with the bean name " + name + " when trying to configure the mapping metadata for the controller bean " + name + " of type " + controller.getClass&#40;&#41;&#41;;
                                &#125;
                                
                                // this is done in SimpleUrlHandlerMapping.initApplicationContext&#40;&#41;
                                String urlPath = mapping.mappingUrl&#40;&#41;;
                                if&#40;!urlPath.startsWith&#40;"/"&#41;&#41;
                                   urlPath = "/" + urlPath;
                                
                                // reflective call to AbstractUrlHandlerMapping.registerHandler&#40;String name, Object target&#41;
                                try &#123;
                                   Method method = null;
                                   Method&#91;&#93; methods = AbstractUrlHandlerMapping.class.getDeclaredMethods&#40;&#41;;
                                   for&#40;Method m &#58; methods&#41; &#123;
                                      if&#40;m.getName&#40;&#41;.equals&#40;"registerHandler"&#41;&#41; &#123;
                                         method = m;
                                         break;
                                      &#125;
                                   &#125;
                                   method.setAccessible&#40;true&#41;;
                                   method.invoke&#40;urlMapping, new Object&#91;&#93;&#123;urlPath, controller&#125;&#41;;
                                &#125; catch&#40;Exception e&#41; &#123;
                                   throw new FatalBeanException&#40;"Error registering handler", e&#41;;
                                &#125;
                                logger.debug&#40;"Mapped URL path &#91;" + urlPath + "&#93; onto handler &#91;" + controller + "&#93;"&#41;;
                             &#125;
                          &#125;
                       &#125;
                    &#125;
                    The annotation class is
                    Code:
                    /**
                     * Metadata for mapping controllers &#40;of type &#123;@link AbstractController&#125;&#41; to a URL path via a SimpleUrlMappingHandler bean
                     * @author Daniel Campagnoli
                     */
                    @Retention&#40;RetentionPolicy.RUNTIME&#41;
                    @Target&#40;&#123;ElementType.TYPE&#125;&#41;
                    public @interface UrlHandlerMapping &#123;
                    
                       /** The bean name of the SimpleUrlHandler to add the annotated class to it's mappings */
                       String urlHandlerBean&#40;&#41;;
                       /** The URL the annotated controller class should be mapped to */
                       String mappingUrl&#40;&#41;;
                    &#125;
                    I have a few different SimpleUrlHandlerMapping beans in spring-servlet.xml, each with different sets of interceptors (mainly for security).
                    In your spring-servlet.xml you will need to add a SimpleUrlHandlerAnnotationPostProcessor bean.
                    Your controller class might look like this
                    Code:
                    @UrlHandlerMapping&#40;mappingUrl="/enterTrade.html", urlHandlerBean="userMapping"&#41;
                    public class EnterTradeController extends SimpleFormController &#123;
                    ...
                    &#125;
                    or this
                    Code:
                    @UrlHandlerMapping&#40;mappingUrl="/deleteAccount.html", urlHandlerBean="adminMapping"&#41;
                    public class DeleteAccountController extends SimpleFormController &#123;
                    ...
                    &#125;
                    Dan

                    Comment

                    Working...
                    X