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方法

原文地址:https://www.cnblogs.com/2016024291-/p/8205145.html