Miscillo
Miscillo

Reputation: 31

Spring 4 @controller config

I'm migrating a webapp from spring 2.5 to spring 4, but I found a problem. I have two differents url that work for two different configurations of the same class. In my old version, I have something like:

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/url1.htm">bean1</prop>
                <prop key="/url2.htm">bean2</prop>
              </props>
          </property>
  </bean>

and the beans are something like

<bean id="bean1" class="com.package.Controller" scope="session">
    <property name="property" value="value of property"/>
</bean>
<bean id="bean2" class="com.package.Controller" scope="session">
    <property name="property" value="a different value of the same property"/>
</bean>

How could I do this with the annotations?

Upvotes: 3

Views: 362

Answers (1)

njjnex
njjnex

Reputation: 1555

Use @Controller annotation on your controller class and map /url1.htm and /url2.htm with @RequestMapping annotation. Look Spring Reference @RequestMapping.

You will get something like this:

@Controller
@RequestMapping("/url1.htm")
public class bean1{

}
@Controller
@RequestMapping("/url2.htm")
public class bean2{

}

And set bean properties in each class. If you dont want to duplicate methods you can do like this

@Controller
public class bean1{

   @RequestMapping("/url{id}.htm")
   public void setBeanProp(@PathVariable int id){
     if (id.equals(1))
     ...
     else
     ...


    }

Upvotes: 1

Related Questions