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不需要