SpringMVC学习笔记(四)--数据回显-->域

数据回显,顾名思义就是后台数据返回给页面的过程,主要有两种方式

  • Ajax

一、域

1.1 HttpServlet

和Servlet中一样,设置参数

1.2 ModelAndView

@RequestMapping("findBySid")
    public ModelAndView findById(int id) throws IOException {
        before();
        Student student = studentMapper.findById(id);
//      方法一
//        ModelAndView modelAndView=new ModelAndView();
//        modelAndView.setViewName("updateStudent");
//        modelAndView.addObject("student",student);
//      方法二
        HashMap<String,Student> map=new HashMap<>();
        map.put("student",student);
        ModelAndView modelAndView=new ModelAndView("updateStudent",map);
        after();
        return modelAndView;
    }

1.3 ModelMap

@RequestMapping("findAll")
    public String findAll(ModelMap modelMap) throws IOException {
        before();
        List<Student> studentList = studentMapper.findAll();
        modelMap.addAttribute("studentList",studentList);
        after();
        return "showStudent";
    }

1.4 Model

@RequestMapping("insert")
    public String add(Model model,String sname,String sex,String password,int age) throws IOException {
        before();
        Student student=new Student();
        student.setSname(sname);
        student.setSex(sex);
        student.setPassword(password);
        student.setAge(age);
        studentMapper.add(student);
        after();
        return "redirect:findAll";
    }

1.5 Map

@RequestMapping("update")
    public String update(Map map,int sid,String sname,String sex,String password,int age) throws IOException {
        before();
        Student student=new Student();
        student.setSid(sid);
        student.setSname(sname);
        student.setSex(sex);
        student.setPassword(password);
        student.setAge(age);
        studentMapper.update(student);
        after();
        return "redirect:findAll";
    }

 


 二、 @ModelAttribute注解回显

2.1 用在Controller的方法上

在到达目标处理方法之前,会优先执行标注了@ModelAttribute的方法,并且会将该方法返回的结果存入域中,执行@RequestMapping的时候就可直接使用域中的值

/**
     * 在调用RequestMapping前执行,如果有返回值则直接放入域中
     * @return
     */
    @ModelAttribute
    public Student index(){
        System.out.println("index");
        Student student=new Student();
        student.setSid(99);
        student.setSname("haiqing");
        student.setSex("女");
        student.setPassword("000");
        student.setAge(18);
        return student;
    }

    /**
     * 不带请求参数的则是程序一打开默认的方法
     * @return
     */
    @RequestMapping
    public ModelAndView mv(Student student){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("hello");//视图名字为index.jsp
        //数据是默认存放在request域中
        modelAndView.addObject("name",student.getSname());
        return modelAndView;
    }

结果:

 

2.2 用在方法的参数上

就是将对象保存到域中,但是如果方法上保存的对象和参数对象的key是一致的情况下,相同属性的会覆盖,不同属性的会保留,也就是参数优先级高


 

三、@SessionAttributes

这个注解比较简单,只能使用在定义上,其作用是将request里面的内容,复制一份到Session域中,但是其写法有两种:

3.1@SessionAttributes(value={“name1”, “name2”})

通过属性名指定需要放到会话中的属性 

@Controller
@SessionAttributes({"name1","name2"})
//@SessionAttributes(types = {String.class})
public class HelloWorldController {
    /**
     * 路径映射,访问相应的方法和页面
     * 页面发起welcome请求,则调用hello方法并跳转页面
     * @return
     */
    @RequestMapping({"welcome","hello"})//多路径映射
    public String hello(Model model){
        model.addAttribute("name","小红");
        model.addAttribute("name1","小小红");
        model.addAttribute("name2","大红");
        System.out.println("hello");
        //使用重定向:return ”redirect : index”;
        //使用请求转发:return ”forward: index”;(默认)
        return "hello";
    }

jsp页面:使用sessionScope接受

hello,${name}<br>
Session内容:<br>
${sessionScope.name1}<br>
${sessionScope.name2}<br>

结果:

 

3.2 @SessionAttributes(types={User.class, Dept.class})

通过模型属性的对象类型​​​​​​​指定

@SessionAttributes(types = {String.class})

页面:

hello,${name}<br>
Session内容:<br>
${sessionScope.name1}<br>
${sessionScope.name2}<br>
${sessionScope.name}<br>

结果: String类型的值都被放到session域中了

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值