Spring MVC:将多个URL映射到同一个控制器

问题描述:

我有20多个表格,它们是从同一页面链接的。某些表单共享同一个控制器,而其他表单使用自己的控制器例如,表格 A B C 使用 DefaultController ,而表格 D 使用 ControllerD

I have like 20+ forms which are linked from the same page. Some forms share the same controller, while others use their own. For example, form A, B, and C use DefaultController, while form D uses ControllerD.

我想要实现的是以一致的方式将URL映射到每个表单。

所以,理想情况下,链接页面看起来像:

So, ideally, the link page would look like :


  • 这个

  • either this

<a href="/formA.html">Form A</a>
<a href="/formB.html">Form B</a>
<a href="/formC.html">Form C</a>
<a href="/formD.html">Form D</a>


  • 或者这个:

  • or this:

    <a href="/form.html?name=A">Form A</a>
    <a href="/form.html?name=B">Form B</a>
    <a href="/form.html?name=C">Form C</a>
    <a href="/form.html?name=D">Form D</a>
    


  • 问题是如何将每个网址映射到适当的控制器。使用第一个URL模式,您可以将 formD.html 映射到 ControllerD ,但不知道如何映射表格[A | B | C] .h​​tml DefaultController 。使用第二个URL模式,我甚至不知道从哪里开始...

    The question is how to map each URL to the appropriate controller. With the first URL pattern, you would map formD.html to ControllerD, but not sure how to map form[A|B|C].html to DefaultController. With the second URL pattern, I don't even know where to begin...

    有没有人做过这样的事情?

    Has anyone done something like this?

    由于似乎没有人在这里提出完整的答案:

    Since nobody seems to have put the full answer on here yet:

    @RequestMapping 注释可以为其value参数获取数组。要使用第一个模式在控制器级别映射它,您将使用:

    The @RequestMapping annotation can take an array for its "value" parameter. To map this at the controller level using the first pattern, you would use:

    @Controller
    @RequestMapping(value={"/formA.html", "/formB.html", "/formC.html"})
    public class ControllerA {
    
    }
    

    然后:

    @Controller
    @RequestMapping(value="/formD.html")
    public class ControllerD {
    
    }