SpringMVC入门(7):返回页面数据

新建springmvc项目这里就不介绍过程,不会的自行查看我的博客中的SpringMVC入门(1):helloworld入门教程

配置文件的配置在这里也不介绍,不会的去看前面的章节

1. HttpServletRequest request直接使用request.setAttribute("user", str);

1.1创建页面user_add1.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here


用户信息添加

账号:
密码:
${user }

 

1.2创建UserController,添加如下方法

package com.springmvc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {
	@RequestMapping(value="/add1.do")
	public String add(HttpServletRequest request) {
		String userNumber = request.getParameter("userNumber");
		String passWord = request.getParameter("passWord");
		
		System.out.println("userNumber:"+userNumber+" passWord:"+passWord);
		
		String str = "userNumber:"+userNumber+" passWord:"+passWord;
		
		request.setAttribute("user", str);
		
		return "user_add1";
	}
	
}

运行页面测试效果


2. 使用ModelAndView

2.1创建页面user_add2.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here


用户信息添加2

${userId }---${userName }

 

2.2创建UserController,添加如下方法

package com.springmvc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {

	@RequestMapping(value="/add2.do")
	public ModelAndView add(){

		ModelAndView mav = new ModelAndView("user_add2");
		mav.addObject("userId", 1001);
		mav.addObject("userName", "博主最帅");
		
		return mav;
	}
	
}

运行页面测试效果


3. 使用Model model

3.1创建页面user_add3.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here


用户信息添加2

${personId }----${personName }

 

3.2创建UserController,添加如下方法

package com.springmvc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {

	@RequestMapping(value="/add3.do")
	public String add3(Model model){
		
		model.addAttribute("personId", 9527);
		model.addAttribute("personName", "博主最帅");
		
		return "user_add3";
	}
	
}

运行页面测试效果


4.使用Map

4.1创建页面user_add3.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>




Insert title here


用户信息添加4

用户编号:
用户名称:
${personId }----${personName }

 

4.2创建UserController,添加如下方法

package com.springmvc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@RequestMapping(value="/add4.do")
	public String add4(Integer personId,String userName,Map map){
		
		System.out.println(personId);
		System.out.println(userName);
		map.put("personId", personId);
		map.put("personName", userName);
		
		return "user_add4";
	}
	
}

运行页面测试效果

 

你可能感兴趣的:(SpringMVC)