日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

springMVC 几种页面跳转方式

發(fā)布時(shí)間:2025/3/14 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springMVC 几种页面跳转方式 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


今天主要寫一下響應(yīng)界面跳轉(zhuǎn)的幾種方式

1.在注解的方式中

1.1通過(guò)HttpServletResponse的API直接輸出(不需要配置渲染器)

controller類的主要代碼 @Controller public class RequestController{@RequestMapping("/resp")public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {resp.getWriter().println("hello HttpServletResponse");}

  

web.xml配置

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping> </web-app>

  

dispatcher-servlet.xml主要代碼

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!--作用是掃描指定包下所有的包含注解的類--><context:component-scan base-package="com.sawshaw.mvc"/></beans>

  

1.2 使用HttpServletResponse 重定向到另一個(gè)視圖(其他不變 )

@RequestMapping("/resp")public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {resp.sendRedirect("index.jsp");} }

  

1.3 使用HttpServletRequest 轉(zhuǎn)發(fā)(默認(rèn)訪問(wèn)/下的index.jsp頁(yè)面 不受渲染器的影響)

@RequestMapping("/resp")public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {req.setAttribute("message","it's forword ");req.getRequestDispatcher("index.jsp").forward(req,resp);}

  

1.4直接返回jsp頁(yè)面的名稱(無(wú)渲染器)

其他的配置不變

@RequestMapping("/nice")public String hello1(){//轉(zhuǎn)發(fā)方式1return "home.jsp";//轉(zhuǎn)發(fā)方式2return "forward:index.jsp";//重定向方式return "redirect:index.jsp";}

  

1.5當(dāng)有渲染器指定

@RequestMapping("/nice")public String hello1(){//轉(zhuǎn)發(fā)方式1return "home";//轉(zhuǎn)發(fā)方式2return "forward:index";//重定向方式 hello指的是requsrmappingreturn "redirect:hello";}

  

2 使用view

2.1 使用modelandview

需要視圖解析器 能指定跳轉(zhuǎn)頁(yè)面 public class HelloController implements Controller {@Overridepublic ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest,javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {ModelAndView mv = new ModelAndView();//封裝要顯示到視圖的數(shù)據(jù)mv.addObject("msg","hello myfirst mvc");//視圖名mv.setViewName("hello");return mv;} }

  

[servlet-name]-servlet.xml

<!--配置渲染器--><!--配置hellocontroller中頁(yè)面的位置--><bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /><bean id="viewResolver"class="org.springframework.web.servlet.view.UrlBasedViewResolver"><property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/><!--結(jié)果視圖的前綴--><property name="prefix" value="/WEB-INF/jsp/"/><!--結(jié)果視圖的后綴--><property name="suffix" value=".jsp"/> </bean><bean name="/hello.do" class="com.jsu.mvc.HelloController"></bean>

  

2.2 使用modelview

不需要視圖解析器 不能指定跳轉(zhuǎn)頁(yè)面

//通過(guò)modelmap方式@RequestMapping("/modelmap")public String modelHello(String name,ModelMap map){map.addAttribute("name",name);System.out.println(name);return "index.jsp";}

  

結(jié)語(yǔ)

與君共勉!

轉(zhuǎn)載于:https://www.cnblogs.com/JAYIT/p/9366302.html

總結(jié)

以上是生活随笔為你收集整理的springMVC 几种页面跳转方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。