zoukankan      html  css  js  c++  java
  • springMVC 几种页面跳转方式

    转自:https://www.cnblogs.com/jpfss/p/8692815.html

    前面已经了解了Controller的几种配置方式
    今天主要写一下响应界面跳转的几种方式

    1.在注解的方式中
    1.1通过HttpServletResponse的API直接输出(不需要配置渲染器)
    controller类的主要代码

    1 @Controller
    2 public class RequestController{
    3  @RequestMapping("/resp")
    4     public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    5 
    6          resp.getWriter().println("hello HttpServletResponse");
    7 
    8     }

    web.xml配置

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     5          version="3.1">
     6 
     7     <servlet>
     8         <servlet-name>dispatcher</servlet-name>
     9         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    10         <load-on-startup>1</load-on-startup>
    11     </servlet>
    12     <servlet-mapping>
    13         <servlet-name>dispatcher</servlet-name>
    14         <url-pattern>/</url-pattern>
    15     </servlet-mapping>
    16 </web-app>

    dispatcher-servlet.xml主要代码

     1 <beans xmlns="http://www.springframework.org/schema/beans"
     2        xmlns:context="http://www.springframework.org/schema/context"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xsi:schemaLocation="
     5         http://www.springframework.org/schema/beans
     6         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     7         http://www.springframework.org/schema/context
     8         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
     9 
    10     <!--作用是扫描指定包下所有的包含注解的类-->
    11     <context:component-scan base-package="com.jsu.mvc"/>
    12 
    13 </beans>

    1.2 使用HttpServletResponse 重定向到另一个视图(其他不变 )

    1     @RequestMapping("/resp")
    2     public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    3 
    4         resp.sendRedirect("index.jsp");
    5 
    6     }
    7 }

    1.3 使用HttpServletRequest 转发(默认访问/下的index.jsp页面 不受渲染器的影响)

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

    1.4直接返回jsp页面的名称(无渲染器)
    其他的配置不变

    1   @RequestMapping("/nice")
    2     public String hello1(){
    3         //转发方式1
    4         return "home.jsp";
    5         //转发方式2
    6         return "forward:index.jsp";
    7         //重定向方式
    8         return "redirect:index.jsp";
    9     }

    1.5当有渲染器指定

    1  @RequestMapping("/nice")
    2     public String hello1(){
    3         //转发方式1
    4         return "home";
    5         //转发方式2
    6         return "forward:index";
    7         //重定向方式  hello指的是requsrmapping
    8         return "redirect:hello";
    9     }

    2 使用view
    2.1 使用modelandview
    需要视图解析器 能指定跳转页面

     1 public class HelloController implements Controller {
     2 
     3 
     4     @Override
     5     public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest,
     6                                       javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
     7 
     8         ModelAndView mv = new ModelAndView();
     9         //封装要显示到视图的数据
    10         mv.addObject("msg","hello myfirst mvc");
    11         //视图名
    12         mv.setViewName("hello");
    13         return mv;
    14 
    15     }
    16 }

    [servlet-name]-servlet.xml

     1 <!--配置渲染器-->
     2     <!--配置hellocontroller中页面的位置-->
     3 
     4     <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
     5     <bean id="viewResolver"
     6                      class="org.springframework.web.servlet.view.UrlBasedViewResolver">
     7     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
     8     <!--结果视图的前缀-->
     9     <property name="prefix" value="/WEB-INF/jsp/"/>
    10     <!--结果视图的后缀-->
    11     <property name="suffix" value=".jsp"/>
    12 </bean>
    13     <bean name="/hello.do" class="com.jsu.mvc.HelloController"></bean>

    2.2 使用modelview
    不需要视图解析器 不能指定跳转页面

    1 //通过modelmap方式
    2     @RequestMapping("/modelmap")
    3     public String modelHello(String name,ModelMap map){
    4         map.addAttribute("name",name);
    5         System.out.println(name);
    6 
    7         return "index.jsp";
    8     }
  • 相关阅读:
    golang API开发过程的中的自动重启(基于gin框架)
    单位时间的调度问题 —— 贪心
    Qt获取文件夹下文件
    C++ using
    QGridLayout动态添加控件
    数据库查询优化-20条必备sql优化技巧
    Django:类视图的装饰器
    使用同一个模态框进行新增和修改
    Django:使用celery处理异步任务
    jenkins:调用jenkinsAPI
  • 原文地址:https://www.cnblogs.com/sharpest/p/5731284.html
Copyright © 2011-2022 走看看