zoukankan      html  css  js  c++  java
  • SpringMVC返回数据到页面

    1.项目结构

    2.视图页面

    ①user_add1.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>添加用户信息!!</h1>
        <form action="add1.do" method="post">
            账号:<input type="text" name="userNum"/><br>
            密码:<input type="text" name="passWord"/><br>
            <input type="submit" value="提交">
        </form>
    </body>
    </html>

    ②user_add2.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>添加用户信息!!</h1>
        <form action="add2.do" method="post">
            账号:<input type="text" name="usernum"/><br>
            密码:<input type="text" name="password"/><br>
            <input type="submit" value="提交">
        </form>
    </body>
    </html>

    ③user_add3.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>添加用户信息3!!</h1>
        <form action="add3.do" method="post">
            账号:<input type="text" name="usernum"/><br>
            密码:<input type="text" name="password"/><br>
            <input type="submit" value="提交">
        </form>
    </body>
    </html>

    ④user_add4.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>添加用户信息3!!</h1>
        <form action="add4.do" method="post">
            账号:<input type="text" name="usernum"/><br>
            密码:<input type="text" name="password"/><br>
            <input type="submit" value="提交">
        </form>
    </body>
    </html>

    3.UserController.java

    package com.spring;
    
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    /*@RequestMapping("/user")*/
    public class UserController {
        
        
        @RequestMapping(value="/add1.do",method=RequestMethod.POST)
        public String add1(HttpServletRequest request,
                HttpServletResponse response,HttpSession session){
            String userNum=request.getParameter("userNum");
            String passWord=request.getParameter("passWord");
            System.out.println("userNumber:"+userNum+" password:"+passWord);
            String user="userNumber:"+userNum+" password:"+passWord;
            request.setAttribute("user", user);
            return "user/success";
        }
        
        @RequestMapping(value="/add2.do",method=RequestMethod.POST)
        public ModelAndView add2(String usernum,String password){
            ModelAndView mav=new ModelAndView("user/success");
            mav.addObject("userNum",usernum);
            mav.addObject("passWord", password);
            return mav;
        }
        
        /*用的最多*/
        @RequestMapping(value="/add3.do",method=RequestMethod.POST)
        public String add3(Model model,@RequestParam(value="usernum")String usernum,Integer password){
            model.addAttribute("userNum",usernum);
            model.addAttribute("passWord", password);
            
            return "user/success";
        }
        
        @RequestMapping(value="/add4.do",method=RequestMethod.POST)
        public String add4(Map map,@RequestParam(value="usernum")String usernum,Integer password){
            map.put("userNum",usernum);
            map.put("passWord", password);
            
            return "user/success";
        }
        
    }

    注:1.返回数据主要有四种方法,①通常用的request方法 ②ModelAndView ③Model ④Map

      2.本质还是request方法

  • 相关阅读:
    十年经验大牛浅谈自动化测试与测试用例的编写
    从功能测试到自动化测试,携程大牛总结一些工作经验分享
    阿里大牛谈软件测试面试的几个建议
    记一个Selenium自动化测试网页
    腾讯大牛教你简单的自动化测试模型(Python+Selenium)
    阿里大牛教你基于Python的 Selenium自动化测试示例解析
    携程大牛谈自动化测试里的数据驱动和关键字驱动思路的理解
    论:关于自动化测试的前期发展历史及未来发展趋势
    阿里大牛教你一分钟了解自动化测试
    绑定银行卡的一些细节
  • 原文地址:https://www.cnblogs.com/2016024291-/p/8205145.html
Copyright © 2011-2022 走看看