开发者

In Spring MVC (2.0) how can you easily hook multiple pages/urls to use 1 controller?

开发者 https://www.devze.com 2022-12-26 07:20 出处:网络
<!--dispatcher file--> <bean id=\"urlMapping\" class=\"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping\">
<!--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="bo开发者_如何学CzController" 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?


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消