您的位置:首页 > 编程语言 > Java开发

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

2016-05-02 14:58 537 查看
在springMVC中,通过@RequestMapping发送请求地址,转发到目标页面,但是,有时候想直接访问页面,

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

实例参考 :/article/9620806.html

在上面的实例中修改spirngmvc.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"> <!--  配置自动扫描的包  -->
<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映射地址请求转发页面。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: