springMVC(8)------mvc:view-controller直接转发页面

来源:互联网 发布:单片机温度采集系统 编辑:程序博客网 时间:2024/05/22 07:44

在springMVC中,通过@RequestMapping发送请求地址,转发到目标页面,但是,有时候想直接访问页面,

不想通过xxx.jsp直接访问页面,可以通过springmvc.xml配置文件中的mvc:view-controller标签做到页面的直接访问。

实例参考 :http://blog.csdn.net/yhl_jxy/article/details/51265077

在上面的实例中修改spirng-mvc.xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:mvc="http://www.springframework.org/schema/mvc"         xmlns:p="http://www.springframework.org/schema/p"         xmlns:context="http://www.springframework.org/schema/context"         xmlns:aop="http://www.springframework.org/schema/aop"         xmlns:tx="http://www.springframework.org/schema/tx"         xsi:schemaLocation="http://www.springframework.org/schema/beans              http://www.springframework.org/schema/beans/spring-beans-4.1.xsd              http://www.springframework.org/schema/context               http://www.springframework.org/schema/context/spring-context-4.1.xsd              http://www.springframework.org/schema/aop               http://www.springframework.org/schema/aop/spring-aop-4.1.xsd              http://www.springframework.org/schema/tx               http://www.springframework.org/schema/tx/spring-tx-4.1.xsd              http://www.springframework.org/schema/mvc               http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd              http://www.springframework.org/schema/context               http://www.springframework.org/schema/context/spring-context-4.1.xsd">  <!--  配置自动扫描的包,扫描到@controller注解的类是控制器,是使用注解的前提  --><context:component-scan base-package="com.lanhuigu.springmvc"/><!-- 视图解析器 --><bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  <!-- 配置前缀 -->  <property name="prefix" value="/WEB-INF/views/"/>  <!-- 配置后缀 -->  <property name="suffix" value=".jsp"/></bean><!-- 配置直接转发的页面 --><mvc:view-controller path="/success" view-name="success"/><!-- 解决mvc:view-controller配置后RequestMapping映射地址报404的问题 -->      <mvc:annotation-driven></mvc:annotation-driven></beans>

这个时候,我们可以在浏览器直接访问success.jsp页面:

http://localhost:9000/SpringMVC/success

不用通过访问某个RequestMapping地址,返回到success页面,也不需要通过success.jsp的绝对路径访问。

通过这样直接转发页面的配置,不影响RequestMapping映射地址请求转发页面。


0 0