SpringMvc 多个URl访问路径指向同一个Controller类

1:springmvc配置文件中的配置方式1

不同的访问url 指向用一个 Controller类(普通的一个java类实现Contrlloer接口)

使用的映射器为默认的适配器

 方式1

一对一的映射

<!--一个类 配置多个Url-->
    <bean  name="/user.action" class="com.cn.controllers.UserAction"></bean>
    <bean  name="/user_u.action" class="com.cn.controllers.UserAction"></bean>
    <bean  name="/user_f.action" class="com.cn.controllers.UserAction"></bean>
    <bean  name="/user_d.action" class="com.cn.controllers.UserAction"></bean>

  <!--映射器 beanNameUrl. 这个是默认的配置 意思就是那bean的name作为url访问路径
   可选
  -->
  <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
 

jsp页面

  <body>
    <a href="${pageContext.request.contextPath}/user.action">增加用户</a>
    <a href="${pageContext.request.contextPath}/user_u.action">修改用户</a>
    <a href="${pageContext.request.contextPath}/user_d.action">删除用户</a>
    <a href="${pageContext.request.contextPath}/user_f.action">查询用户</a>
  </body>

 

 

方式2

多对1的映射 (主要是修改默认的 映射器为:"SimpleUrlHandlerMapping")

    <bean  id="userAction" class="com.cn.controllers.UserAction"></bean>
    <!--另外一种映射器实现多对一的映射-->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/user.action">userAction</prop>
                <prop key="/user_u.action">userAction</prop>
                <prop key="/user_f.action">userAction</prop>
                <prop key="/user_d.action">userAction</prop>
            </props>
        </property>
    </bean>

 

 

方式3注解方式:

@Controller
public class HelloAction {

    @RequestMapping(value = {"/hello1.action","hello2.action","hello3.action"})
    public String hello(Model model, HttpServletRequest request, HttpServletResponse response) throws  Exception{
        System.out.println("调用了我吗?");
        model.addAttribute("messsage","注解实现springmvc");

        System.out.println("hello world 我是通过注解来实现的");
        return "hello";
    }
}

 

posted @ 2020-06-01 17:39  gaoshengjun  阅读(2848)  评论(0编辑  收藏  举报