数据的处理

1、提交数据的处理

a:提交的域名称和处理方法的参数名一致

提交的数据:http://localhost:8080/spring_data/hello.do?name=allen

处理方法:

HelloController:

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

	public HelloController() {
		System.out.println("hello controller");
	}

	@RequestMapping("/hello")
	public String hello(String name) {
		// 重定向:
		System.out.println(name);
		return "index.jsp";
	}

}

b:如果域名称和参数名不一致

http://localhost:8080/spring_data/hello.do?uname=allen
HelloController.java
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {

	public HelloController() {
		System.out.println("hello controller");
	}
	//uname是提交的域名称
	@RequestMapping("/hello")
	public String hello(@RequestParam("uname")String name) {
		// 重定向:
		System.out.println(name);
		return "index.jsp";
	}

}

c:提交的是一个对象

要求提交的表单域名和对象的属性名一致,参数使用对象即可
http://localhost:8080/spring_data/user.do?name=leijun&age=48
处理方法:
package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sxt.vo.User;

@Controller
public class HelloController {

	public HelloController() {
		System.out.println("hello controller");
	}

	@RequestMapping("/user")
	public String user(User user) {
		System.out.println(user);
		return "user.jsp";

	}

}

实体类:User.java

package cn.sxt.vo;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

public class User {
	private String name;
	private int age;
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birthday;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}
	
}


2、将数据显示到UI层

第一种通过ModelAndView---需要视图解析器:

package springmvc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloController implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		 
		ModelAndView mv = new ModelAndView();
//相当于req.setAttribute("msg","hello spring mvc");
		mv.addObject("msg", "hello springmvc controller");
		mv.setViewName("hello");
		return mv;
	}

}

第二种:通过ModelMap来实现,不需要视图解析器

需要作为处理方法的参数
	@RequestMapping("/hello")
	public String hello(@RequestParam("uname") String name,ModelMap model) {
		// 相当于request.setAttribute("name",name);
		model.addAttribute("name","name");
		System.out.println(name);
		return "index.jsp";
	}

ModelAndView和ModelMap的区别:

相同点:都可以将数据封装显示在UI层
不同点:ModelAndView可以指定跳转的视图,ModelMap不能
ModelAndView需要视图解析器,ModelMap不需要






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值