溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringMVC怎么實(shí)現(xiàn)簡(jiǎn)單跳轉(zhuǎn)方法

發(fā)布時(shí)間:2021-05-18 09:48:36 來(lái)源:億速云 閱讀:144 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下SpringMVC怎么實(shí)現(xiàn)簡(jiǎn)單跳轉(zhuǎn)方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

簡(jiǎn)單跳轉(zhuǎn)專題

個(gè)人建議重新練習(xí)一遍搭建的過(guò)程,如果感覺(jué)麻煩你可以直接復(fù)制上一個(gè)工程,但是需要修改pom.xml中的一點(diǎn)信息

<groupId>com.hanpang.springmvc</groupId>
<artifactId>springmvc-demo01</artifactId>
<version>0.0.1-SNAPSHOT</version>

1.核心配置類和加載類

package com.hanpang.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.hanpang.**.web")
public class WebConfig {
}
package com.hanpang.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

 @Override
 protected Class<?>[] getRootConfigClasses() {
 return new Class[] {WebConfig.class};
 }

 @Override
 protected Class<?>[] getServletConfigClasses() {
 return null;
 }

 @Override
 protected String[] getServletMappings() {
 return new String[] {"/"};
 }
}

2.JavaWeb階段的跳轉(zhuǎn)方式

請(qǐng)注意SpringMVC的方法中的形參,框架給我們完成了實(shí)例化的操作

package com.hanpang.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller//告知其是一個(gè)控制器
public class Demo01Controller {

 @RequestMapping(path="/test01")
 public ModelAndView 傳統(tǒng)方式跳轉(zhuǎn)_請(qǐng)求轉(zhuǎn)發(fā)(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
 System.out.println("默認(rèn)對(duì)形參的進(jìn)行了實(shí)例化操作");
 request.getRequestDispatcher("/WEB-INF/jsp/demo01.jsp").forward(request, response);
 return null;
 }

 @RequestMapping(path="/test02")
 public ModelAndView 傳統(tǒng)方式跳轉(zhuǎn)_重定向(HttpServletRequest request,HttpServletResponse response) throws IOException {
 System.out.println("默認(rèn)對(duì)形參的進(jìn)行了實(shí)例化操作");
 response.sendRedirect(request.getContextPath()+"/view/result01.jsp");
 return null;
 }
}

NOTE: 這種方式我們幾乎不再使用了,只是只是簡(jiǎn)單的演示和回顧一下,至少我們可以使用這種方式獲取Servlet API!!!

3.Controller跳轉(zhuǎn)到JSP的方式演示

在示例最后的時(shí)候,我們會(huì)加入JSP的視圖解析器,開(kāi)始階段我們還是按照傳統(tǒng)的方式,有一個(gè)循序漸進(jìn)的過(guò)程

package com.hanpang.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller//告知其是一個(gè)控制器
public class Demo01Controller {

 @RequestMapping(path="/test03")
 public ModelAndView 默認(rèn)情況下是請(qǐng)求轉(zhuǎn)發(fā)(){
 ModelAndView mav = new ModelAndView();
 mav.setViewName("/WEB-INF/jsp/demo01.jsp");
 return mav;
 }
 @RequestMapping(path="/test04")
 public ModelAndView 設(shè)置重定向的方式(){
 ModelAndView mav = new ModelAndView();
 mav.setViewName("redirect:/view/result01.jsp");
 //或者
 //mav.setViewName(UrlBasedViewResolver.REDIRECT_URL_PREFIX+"/view/result01.jsp");
 return mav;
 }

}

感覺(jué)跟Java Web階段的方式差不多,只是重定向的時(shí)候設(shè)置了一個(gè)簡(jiǎn)單的前綴

4.Controller跳轉(zhuǎn)到Controller的方式演示

類似于從一個(gè)Servlet調(diào)到另一個(gè)Servlet

package com.hanpang.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller//告知其是一個(gè)控制器
public class Demo01Controller {

 @RequestMapping(path="/test05")
 public ModelAndView 直接設(shè)置映射路徑即可(){
 ModelAndView mav = new ModelAndView();
 mav.setViewName("/test03");
 return mav;
 }

 @RequestMapping(path="/test06")
 public ModelAndView 設(shè)置重定向(){
 ModelAndView mav = new ModelAndView();
 mav.setViewName("redirect:/test04");
 return mav;
 }
}

5.加入JSP的視圖解析器

上面的演示過(guò)程中,我們發(fā)現(xiàn)ModelAndView中setViewName是用來(lái)完成跳轉(zhuǎn)的,這里面?zhèn)鬟f的數(shù)據(jù)是字符串,但是處理方式不太一樣,當(dāng)跳轉(zhuǎn)的路徑有前綴redirect:的時(shí)候,那么處理方式不一樣.

還有,如果我們有多個(gè)類似/WEB-INF/jsp/demo01.jsp形式的字符串的時(shí)候,我們發(fā)現(xiàn)公共的部分很多,SpringMVC給我們提供了一個(gè)專門(mén)處理  Controller請(qǐng)求轉(zhuǎn)發(fā)JSP頁(yè)面  的類.

請(qǐng)注意我的描述: 如果發(fā)現(xiàn)你傳遞的字符串沒(méi)有設(shè)置任何前綴標(biāo)識(shí),那么默認(rèn)情況下使用配置JSP視圖解析器處理,并且完成請(qǐng)求轉(zhuǎn)發(fā)的操作

注解:配置核心配置類

package com.hanpang.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.hanpang.**.web")
public class WebConfig {

 @Bean//實(shí)例化
 public ViewResolver viewResolver() {
 InternalResourceViewResolver jspViewResolver = new InternalResourceViewResolver();
 jspViewResolver.setViewClass(JstlView.class);//springmvc支持jstl標(biāo)簽
 jspViewResolver.setPrefix("/WEB-INF/");
 jspViewResolver.setSuffix(".jsp");

 return jspViewResolver;
 }
}

**NOTE:**請(qǐng)注意方法上的注解@Bean

方法等價(jià)于XML中的代碼如下

  <bean id="jspResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".jsp"/>
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  </bean>

改進(jìn)Controller跳轉(zhuǎn)JSP代碼

該視圖解析器只能針對(duì)于JSP的請(qǐng)求轉(zhuǎn)發(fā),對(duì)重定向無(wú)效,請(qǐng)注意代碼的注釋內(nèi)容

 @RequestMapping(path="/test03")
 public ModelAndView 默認(rèn)情況下是請(qǐng)求轉(zhuǎn)發(fā)(){
 ModelAndView mav = new ModelAndView();
 //mav.setViewName("/WEB-INF/jsp/demo01.jsp");

 //改進(jìn)后:默認(rèn)情況下,會(huì)使用JSP視圖解析器處理,
 // prefix+"jsp/demo01"+suffix => /WEB-INF/jsp/demo01.jsp
 mav.setViewName("jsp/demo01");//發(fā)現(xiàn)字符串沒(méi)有前綴修飾
 return mav;
 }
 @RequestMapping(path="/test04")
 public ModelAndView 設(shè)置重定向的方式(){
 ModelAndView mav = new ModelAndView();
 //解析器對(duì)重定向無(wú)效
 mav.setViewName("redirect:/view/result01.jsp");
 return mav;
 }

改進(jìn)Controller跳轉(zhuǎn)Controller代碼

@RequestMapping(path="/test05")
public ModelAndView 直接設(shè)置映射路徑即可(){
 ModelAndView mav = new ModelAndView();
 mav.setViewName("/test03");
 return mav;
}

當(dāng)配置完JSP視圖解析器后,對(duì)上述的代碼再次進(jìn)行測(cè)試,查看訪問(wèn)結(jié)果有驚喜

SpringMVC怎么實(shí)現(xiàn)簡(jiǎn)單跳轉(zhuǎn)方法

符合我們之前說(shuō)的"/test03"是一個(gè)字符串,默認(rèn)情況下會(huì)使用JSP視圖解析器進(jìn)行處理,那么如何改進(jìn)呢?

可以設(shè)置前綴"forward:",改進(jìn)代碼如下:

@RequestMapping(path="/test05")
public ModelAndView 直接設(shè)置映射路徑即可(){
 ModelAndView mav = new ModelAndView();
 mav.setViewName("forward:/test03");
 //或者
 //mav.setViewName(UrlBasedViewResolver.FORWARD_URL_PREFIX+"/test03");
 return mav;
}

當(dāng)發(fā)現(xiàn)字符串使用forward:修飾后,處理情況改變?yōu)閺腃ontroller請(qǐng)求轉(zhuǎn)換到另一個(gè)Controller,而如果做到重定向的話,代碼如下:

@RequestMapping(path="/test06")
public ModelAndView 設(shè)置重定向(){
 ModelAndView mav = new ModelAndView();
 mav.setViewName("redirect:/test04");
 return mav;
}

6.InternalResourceViewResolver 附錄

InternalResourceViewResolver:它是URLBasedViewResolver的子類,所以URLBasedViewResolver支持的特性它都支持。

在實(shí)際應(yīng)用中InternalResourceViewResolver也是使用的最廣泛的一個(gè)視圖解析器,那么InternalResourceViewResolver有什么自己獨(dú)有的特性呢?

單從字面意思來(lái)看,我們可以把InternalResourceViewResolver解釋為內(nèi)部資源視圖解析器,這就是InternalResourceViewResolver的一個(gè)特性。

InternalResourceViewResolver會(huì)把返回的視圖名稱都解析為InternalResourceView對(duì)象,InternalResourceView會(huì)把Controller處理器方法返回的模型屬性都存放到對(duì)應(yīng)的request屬性中,然后通過(guò)RequestDispatcher在服務(wù)器端把請(qǐng)求forword重定向到目標(biāo)URL。

比如在InternalResourceViewResolver中定義了prefix=/WEB-INF/,suffix=.jsp,然后請(qǐng)求的Controller處理器方法返回的視圖名稱為test,那么這個(gè)時(shí)候InternalResourceViewResolver就會(huì)把test解析為一個(gè)InternalResourceView對(duì)象,先把返回的模型屬性都存放到對(duì)應(yīng)的HttpServletRequest屬性中,然后利用RequestDispatcher在服務(wù)器端把請(qǐng)求forword到/WEB-INF/test.jsp。這就是InternalResourceViewResolver一個(gè)非常重要的特性,我們都知道存放在/WEB-INF/下面的內(nèi)容是不能直接通過(guò)request請(qǐng)求的方式請(qǐng)求到的,為了安全性考慮,我們通常會(huì)把jsp文件放在WEB-INF目錄下,而InternalResourceView在服務(wù)器端跳轉(zhuǎn)的方式可以很好的解決這個(gè)問(wèn)題。下面是一個(gè)InternalResourceViewResolver的定義,根據(jù)該定義當(dāng)返回的邏輯視圖名稱是test的時(shí)候,InternalResourceViewResolver會(huì)給它加上定義好的前綴和后綴,組成“/WEB-INF/test.jsp”的形式,然后把它當(dāng)做一個(gè)InternalResourceView的url新建一個(gè)InternalResourceView對(duì)象返回。

以上是“SpringMVC怎么實(shí)現(xiàn)簡(jiǎn)單跳轉(zhuǎn)方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI