Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two controllers with same name under different packages in Spring

I have two controller classes with the same name but in different packages under my Spring MVC application but when compiling the application refuses to run on server giving me an error.

Any chance anyone knows how to allow having same controller class name in two (different packages) under a Spring MVC project?

like image 383
MChan Avatar asked Oct 21 '13 09:10

MChan


2 Answers

use value attribute of controller annotation

@Controller("controller1")

and

@Controller("controller2")
like image 90
Bassem Reda Zohdy Avatar answered Nov 06 '22 23:11

Bassem Reda Zohdy


To add to Bassem's answer,

If you had two controllers in 2 packages:

  • pkg1

    • myController
  • pkg2

    • myController

You could use the @Controller annotation:

@Controller("pkg1 myController")
@Controller("pkg2 myController")

So that way it is more organized. You also cannot have the same value in your @Controller("THIS VALUE").

So if you had:

pkg1 - myController1 - myController2

You can not have them both have @Controller("pkg1")

Instead you could use @Controller("pkg1 myController1) to avoid collisions

like image 1
James Avatar answered Nov 07 '22 00:11

James