最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解SpringMVC中的四種跳轉(zhuǎn)方式、視圖解析器問題

 更新時間:2022年10月30日 14:44:24   作者:姓蔡小朋友  
這篇文章主要介紹了SpringMVC的四種跳轉(zhuǎn)方式、視圖解析器,springmvc核心配置文件和視圖解析器的使用,添加視圖解析器,通過案例講解四種跳轉(zhuǎn)方式,需要的朋友可以參考下

一、視圖解析器:

1.springmvc核心配置文件,添加視圖解析器:

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--添加視圖解析器
        視圖解析器作用:
             比如我們要訪問/admin/main.jsp時,傳統(tǒng)頁面跳轉(zhuǎn)需要輸入完整URI訪問路徑,
             而使用了視圖解析器后,會自動在訪問路徑前后添加配置前綴和配置后綴,
             比如配置了如下前綴和后綴后,我們要訪問/admin/main.jsp,訪問路徑只需要寫main就可以了
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前綴-->
        <property name="prefix" value="/admin/"></property>
        <!--配置后綴-->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

2.視圖解析器的使用:

@Controller
public class JumpAction {
    @RequestMapping("/one")
    public String one(){
        System.out.println("請求轉(zhuǎn)發(fā)頁面(默認(rèn))");
        return "main";
        //方法返回"main","main"會被視圖解析器添加前后綴,變成/admin/main.jsp,接著訪問該URI對應(yīng)的頁面。
    }

3、視圖解析器類InternalResourceViewResolver源碼解析:

public class UrlBasedViewResolver extends AbstractCachingViewResolver implements Ordered {
    public static final String REDIRECT_URL_PREFIX = "redirect:";//重定向
    public static final String FORWARD_URL_PREFIX = "forward:";//轉(zhuǎn)發(fā)
    private String prefix = "";//前綴
    private String suffix = "";//后綴
  • 在springmvc核心配置文件中配置視圖解析器,為視圖解析器添加前后綴,實(shí)際上是給視圖解析器類InternalResourceViewResolver的成員方法賦值,視圖解析器類會自動為Action類的方法中return的字符串進(jìn)行拼接,拼接兩個成員方法作為前后綴生成新的URI。
  • 我們注意到視圖解析器類InternalResourceViewResolver還有兩個靜態(tài)成員變量,如果Action類的方法中return的字符串包含這兩個值時,視圖解析器類就不再進(jìn)行前綴后綴的拼接。

二、SpringMVC四種跳轉(zhuǎn)方式:

  • 請求轉(zhuǎn)發(fā)頁面。
  • 請求轉(zhuǎn)發(fā)action。
  • 重定向頁面。
  • 重定向action。

1.跳轉(zhuǎn)方式案例:

前端:

<a href="${pageContext.request.contextPath}/one.action" rel="external nofollow" >請求轉(zhuǎn)發(fā)頁面(默認(rèn))</a><br>
<a href="${pageContext.request.contextPath}/two.action" rel="external nofollow" >請求轉(zhuǎn)發(fā)action</a><br>
<a href="${pageContext.request.contextPath}/three.action" rel="external nofollow" >重定向頁面</a><br>
<a href="${pageContext.request.contextPath}/four.action" rel="external nofollow" >重定向action</a><br>

后端:

@Controller
public class JumpAction {
    @RequestMapping("/one")
    public String one(){
        System.out.println("請求轉(zhuǎn)發(fā)頁面(默認(rèn))");
        //return "main"; //這種訪問方式,默認(rèn)會調(diào)用視圖解析器自動拼接前綴和后綴進(jìn)行請求轉(zhuǎn)發(fā)頁面跳轉(zhuǎn)
        return "forward:/fore/user.jsp";//只要使用了forward:就可以屏蔽前綴和后綴的拼接,自己手工構(gòu)建返回的路徑
    }
    @RequestMapping("/two")
    public String two(){
        System.out.println("請求轉(zhuǎn)發(fā)action");
        return "forward:/other.action";
    }
    @RequestMapping("/three")
    public String three(){
        System.out.println("重定向頁面");
        return "redirect:/admin/main.jsp";//只要使用了redirect:就可以屏蔽前綴和后綴的拼接,自己手工構(gòu)建返回的路徑
    }
    @RequestMapping("/four")
    public String four(){
        System.out.println("重定向action");
        //觀察地址欄的變化  http://localhost:8080/other.action
        return "redirect:/other.action";
    }
}

到此這篇關(guān)于SpringMVC的四種跳轉(zhuǎn)方式、視圖解析器的文章就介紹到這了,更多相關(guān)SpringMVC跳轉(zhuǎn)方式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

桐庐县| 定南县| 定襄县| 镇康县| 永康市| 鄂托克旗| 桑植县| 正安县| 且末县| 定襄县| 得荣县| 望谟县| 岳池县| 根河市| 昌黎县| 高邑县| 台北市| 八宿县| 泽库县| 天等县| 海阳市| 玛沁县| 饶河县| 伊金霍洛旗| 乌鲁木齐市| 芒康县| 垣曲县| 古浪县| 平阴县| 筠连县| 江西省| 怀柔区| 双桥区| 溧阳市| 邓州市| 民和| 万宁市| 横山县| 莒南县| 泾源县| 鄢陵县|