springMV中的view-controller的作用

先来看一段代码,下面这段代码的作用是在Controller中实现一个页面的跳转功能。

  1. @Controller

  2. @RequestMapping("index")

  3. public class TestController {

  4. /**

  5. * @Title: toIndex

  6. * @Description: 实现页面跳转功能

  7. * @return

  8. */

  9. @RequestMapping("toIndex")

  10. public String toIndex(){

  11. return "index";

  12.  
  13. }

  14. }

     在平常的开发过程中我们经常使用这种方式来实现页面之间的跳转,这样的没有任何业务逻辑的页面跳转功能,需要我们定义一个请求类或请求方法来实现这一功能,页面跳转管理起来比较混乱,并且每个Controller类中都有分布,查找起来比较困难、浪费时间。

       基于对上面的代码的分析,我想到了使用mvc命名空间中的view-controller这个属性来定义这样的一个页面跳转功能。
使用<mvc:view-controller path="" view-name=""/>方式配置页面的跳转,配置如下:
 <mvc:view-controller path="/index/toIndex.do" view-name="index"/>

  path:指的是请求的路径,

 view-name:返回的视图名字(视图解释器 配置的文件目录的名字)

 
  1. <beans xmlns="http://www.springframework.org/schema/beans"

  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"

  3. xmlns:context="http://www.springframework.org/schema/context"

  4. xmlns:aop="http://www.springframework.org/schema/aop"

  5. xmlns:tx="http://www.springframework.org/schema/tx"

  6. xmlns:task="http://www.springframework.org/schema/task"

  7. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

  8. xsi:schemaLocation="http://www.springframework.org/schema/beans

  9. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

  10. http://www.springframework.org/schema/mvc

  11. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd

  12. http://www.springframework.org/schema/context

  13. http://www.springframework.org/schema/context/spring-context-4.0.xsd

  14. http://www.springframework.org/schema/aop

  15. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

  16. http://www.springframework.org/schema/tx

  17. http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

  18. http://www.springframework.org/schema/task

  19. http://www.springframework.org/schema/task/spring-task-4.0.xsd

  20. http://code.alibabatech.com/schema/dubbo

  21. http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

  22. <mvc:view-controller path="/index/toIndex.do" view-name="index"/>

  23.  
  24. <!-- 视图解释器 -->

  25. <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  26. <property name="prefix" value="/WEB-INF/views/"/>

  27. <property name="suffix" value=".jsp"/>

  28. </bean>

  29. </beans>

    上面在xml中的配置方式,可以省去了代码的编写,可以把相同模块的页面跳转统一的放在一起,进行这个得配置,方便以后的管理。
     <mvc:view-controller path="" view-name=""/>也可以通过程序进行配置,通过项目启动的时候加载 path和view-name的映射关系

 
  1. @Configuration

  2. @EnableWebMvc

  3. public classWebConfig extendsWebMvcConfigurerAdapter {

  4. @Override

  5. public voidaddViewControllers(ViewControllerRegistry registry) {

  6. registry.addViewController("/index/toIndex.do").setViewName("index");

  7. }

  8. }

   上面这个中程序配置方式生效的前提是@Configuration 这个注解必须生效,可以使用<context:component-scan base-package="" />加载这个类中的注解。

猜你喜欢

转载自blog.csdn.net/qq_39459412/article/details/81587044