在 Spring MVC (2.0) 中,如何轻松挂钩多个页面/url 以使用 1 个控制器?

发布于 2024-08-29 06:58:36 字数 759 浏览 2 评论 0原文

<!--dispatcher file-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
      <prop key="/foo/bar/baz/boz_a.html">bozController</prop>
    </props>
  </property>
</bean>



<!--mappings file-->
<bean id="bozController" class="com.mycompany.foo.bar.baz.BozController">
    <property name="viewPathA" value="foo/bar/baz/boz_a" />
    <property name="viewPathB" value="foo/bar/baz/boz_b" />
    ...
    <property name="viewPathZ" value="foo/bar/baz/boz_z" />
</bean>

如何设置以便当用户加载页面 boz_w.html 时它使用 bozController,并将 viewPath 设置为使用 boz_w.jsp?

<!--dispatcher file-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
      <prop key="/foo/bar/baz/boz_a.html">bozController</prop>
    </props>
  </property>
</bean>



<!--mappings file-->
<bean id="bozController" class="com.mycompany.foo.bar.baz.BozController">
    <property name="viewPathA" value="foo/bar/baz/boz_a" />
    <property name="viewPathB" value="foo/bar/baz/boz_b" />
    ...
    <property name="viewPathZ" value="foo/bar/baz/boz_z" />
</bean>

how do I set it up so that when the user loads page boz_w.html it uses the bozController, and sets the viewPath to use boz_w.jsp?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2024-09-05 06:58:36

即使使用 Spring 2.0,您也可以使用 Spring 注解。当您想要将多个 url 映射到 simgle Controller 时,您可以使用 MultiActionController,如下所示

package br.com.spring.view;

// Do not use @Controller when using Spring 2.0 MVC Controller
// It does not work as expected
// Use @Component instead
@Component
public class MutliPurposeController extends MultiActionController {

    @Autowired
    private Service service;

    // mapped to /mutliPurpose/add
    public ModelAndView add(...) {}

    // mapped to /mutliPurpose/remove
    public ModelAndView remove(...) {}

    // mapped to /mutliPurpose/list
    public ModelAndView list(...) {}

}

您的 WEB-INF/-servlet.xml 如下所示

<beans ...>
    <context:component-scan base-package="br.com.spring.view"/>
    <context:annotation-config/>
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
        <property name="order" value="0"/>
        <property name="caseSensitive" value="true"/>
    </bean>
</beans>

Even when using Spring 2.0, you can use Spring annotations. As you want to map multiple url's to a simgle Controller, you can use MultiActionController, as shown bellow

package br.com.spring.view;

// Do not use @Controller when using Spring 2.0 MVC Controller
// It does not work as expected
// Use @Component instead
@Component
public class MutliPurposeController extends MultiActionController {

    @Autowired
    private Service service;

    // mapped to /mutliPurpose/add
    public ModelAndView add(...) {}

    // mapped to /mutliPurpose/remove
    public ModelAndView remove(...) {}

    // mapped to /mutliPurpose/list
    public ModelAndView list(...) {}

}

Your WEB-INF/<servlet-name>-servlet.xml is shown as follows

<beans ...>
    <context:component-scan base-package="br.com.spring.view"/>
    <context:annotation-config/>
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
        <property name="order" value="0"/>
        <property name="caseSensitive" value="true"/>
    </bean>
</beans>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文