美文网首页程序员
Spring MVC中 控制器 向 视图 传参(返回数据到页面)

Spring MVC中 控制器 向 视图 传参(返回数据到页面)

作者: 凡哥爱丽姐 | 来源:发表于2020-11-26 12:21 被阅读0次

    1、方法一(ModelMap)

    1.1、index.jsp和success.jsp页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <body>
    <h2>Hello World!</h2>
    <form action="/user2" method="post">
        <input type="text" name="username"/>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    
    <%--
      Created by IntelliJ IDEA.
      User: Mr Wei
      Date: 2020/*/*
      Time: 15:19
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>欢迎回来</h1>
    <h1> ModelMap测试 ---> 欢迎回来${username} </h1>
    </body>
    </html>
    

    1.2、Controller类

    package com.fan.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class Controller2 {
        @RequestMapping("/user2")
        public String test1(String username, ModelMap map){
            System.out.println(username);
            map.addAttribute("username",username);
            return "success";
        }
    }
    

    1.3、启动tomcat测试

    测试图片1.png
    测试图片2.png

    2、方法二(ModelAndView,注意与第一种和第三种区分)

    2.1、index.jsp和success.jsp页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <body>
    <h2>Hello World!</h2>
    <form action="/user2" method="post">
        <input type="text" name="username"/>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    
    <%--
      Created by IntelliJ IDEA.
      User: Mr Wei
      Date: 2020/*/*
      Time: 15:19
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>欢迎回来</h1>
    <h1> ModelMap测试 ---> 欢迎回来${username} </h1>
    <h1> ModelAndView测试 ---> 欢迎回来${user} </h1>
    </body>
    </html>
    

    2.2、Controller类

    package com.fan.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class Controller2 {
        @RequestMapping("/user2")
        public ModelAndView test2(String username){
            ModelAndView modelAndView=new ModelAndView();
            modelAndView.addObject("user",username);
            //跳转页面
            modelAndView.setViewName("success");
            return modelAndView;
        }
    }
    
    

    2.3、启动tomcat测试

    测试图片1.png 测试图片2.png

    3、方法三(Model)

    3.1、index.jsp和success.jsp页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <body>
    <h2>Hello World!</h2>
    <form action="/user2" method="post">
        <input type="text" name="username"/>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>
    
    <%--
      Created by IntelliJ IDEA.
      User: Mr Wei
      Date: 2020/*/*
      Time: 15:19
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>欢迎回来</h1>
    <h1> ModelMap测试 ---> 欢迎回来${username} </h1>
    <h1> ModelAndView测试 ---> 欢迎回来${user} </h1>
    <h1> Model测试 ---> 欢迎回来${u} </h1>
    </body>
    </html>
    

    3.2、Controller类

    package com.fan.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class Controller2 {
        @RequestMapping("/user2")
        public String test3(String username, Model model){
            model.addAttribute("u",username);
            return "success";
        }
    }
    

    3.3、启动tomcat测试

    测试图片1.png
    测试图片2.png

    4、方法四(HttpServletRequest)老方法

    5、我们有时候往页面传数据时,通过EL表达式显示不出来,我们往往在每个jsp页面加上isELIgnored="false",如下:

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
    

    其实我们可以通过在更改web.xml头部,如下所示,这样就达到全局设置的目的,从而不需要在每个jsp页面加上isELIgnored="false"

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    

    相关文章

      网友评论

        本文标题:Spring MVC中 控制器 向 视图 传参(返回数据到页面)

        本文链接:https://www.haomeiwen.com/subject/jlkrzhtx.html