SpringMVC-mvc:view-controller+mvc:annotation-driven

SpringMVC-mvc:view-controller+mvc:annotation-driven

在springmvc中通過前端jsp發送一個action可以不經過前端控制器,直接轉發到對應的界面。設置方法爲:只是需要在springmvc.xml中添加mvc:view-controller的配置即可。但是設置了這個配置之後,在前前端控制器中將不能再使用@requestMapping。解決方法爲:再在springmvc.xml中添加mvc:annotation-driven。其內部原理爲:添加了mvc:view-controller後前端控制器內部會減少創建一個對應的類。添加mvc:view-controller後其內部會繼續生成原來三個類。

代碼示例:

前端發送請求jsp:

<a href="${pageContext.request.contextPath}/testView">測試testView</a>

前端跳轉jsp(result.jsp):

<h1>結果頁jsp</h1>

springmvc.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <!--開啓註解掃描-->
    <context:component-scan base-package="com.helong"/>


    <!--
    mvc:view-controller
    當發送一個請求時,如果沒有找到對應的mapping
    則會在配置文件中匹配mvc:view-controller
    注意:配置了這個方式跳轉界面之後,在前端控制器中的requestMapping就不能再使用了。
    解決方法:添加<mvc:annotation-driven/>
    -->
    <mvc:view-controller path="testView" view-name="/result.jsp"/>

    <!--解決方法:添加mvc:view-controller-->
    <mvc:annotation-driven/>


</beans>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章