Spring 4 @controller config
I am migrating webapp from spring 2.5 to spring 4, but I found the problem. I have two different urls 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 can I do this using annotations?
+3
source to share
1 answer
Use @Controller annotation for your controller class and map / url1.htm and / url 2.htm with @RequestMapping annotation. Have a look at Spring's @RequestMapping Reference .
You will end up with something like this:
@Controller
@RequestMapping("/url1.htm")
public class bean1{
}
@Controller
@RequestMapping("/url2.htm")
public class bean2{
}
And set the bean properties in each class. If you don't want to duplicate methods, you can do this
@Controller
public class bean1{
@RequestMapping("/url{id}.htm")
public void setBeanProp(@PathVariable int id){
if (id.equals(1))
...
else
...
}
+1
source to share