SpringMVC框架(二)

ModelAndView传递数据
例:
package com.jredu.controller;

import java.util.HashMap;
import java.util.Map;

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

import com.jredu.entity.Notice;

@Controller
@RequestMapping("/model")
/**
* ModelAndView:传递下一个页面需要的数据,设置转发页面
* @author Administrator
*
*/
public class ModelController {
/**
* 获取公告
*/
@RequestMapping("/m1")
public ModelAndView getNotice1(int id,ModelAndView modelAndView) {
//获取公告
//..
Notice notice=new Notice();
notice.setId(id);
notice.setTitle("本周天不上班");
notice.setContent("由于时就大召开,本周天不用上班");
//使用modelandview对象,视图名称,模型数据
modelAndView.setViewName("hello4");
//传递模型数据
modelAndView.addObject("notice", notice);
modelAndView.addObject("a","abc");
return modelAndView;
}
/**
* 获取公告
*/
@RequestMapping("/m2")
public ModelAndView getNotice2(int id,ModelAndView modelAndView) {
//获取公告
//..
Notice notice=new Notice();
notice.setId(id);
notice.setTitle("本周天不上班");
notice.setContent("由于时就大召开,本周天不用上班");
//使用modelandview对象,视图名称,模型数据
modelAndView.setViewName("hello4");
//传递模型数据
Map<String, Object> map=new HashMap<String, Object>();
map.put("notice", notice);
map.put("a", "abc");
modelAndView.addAllObjects(map);
return modelAndView;
}
//ModelAndView Model+String
@RequestMapping("/m3")
public String getNotice3(Model model) {
//..业务处理
Notice notice=new Notice();
notice.setId(15);
notice.setTitle("本周天不上班");
notice.setContent("由于时就大召开,本周天不用上班");
model.addAttribute("notice", notice);
model.addAttribute("a", "1234");
return "hello4";
}
@RequestMapping("/m4")
public String getNotice4(Map<String, Object> map) {
Notice notice=new Notice();
notice.setId(15);
notice.setTitle("本周天不上班");
notice.setContent("由于19大召开,本周天不用上班");
map.put("notice", notice);
map.put("a", "lalallala");
return "hello4";
}

}

jsp:代码


SpringMVC-@ModeAttribute

在方法定义上使用@ModelAttribute:Spring MVC在调用目标处理方法前,会先逐个调用在方法上标注了

@ModelAttribute的方法
例:
package com.jredu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jredu.entity.User;

@Controller
@RequestMapping("/attr")
/**
* @ModelAttribute的本质作用就是在模型当中添加数据
* (request.setAttribute(被@ModelAttribute所标记的对象))
* 当我们调用被@RequestMapping所标的方法时,
* 会先调用被@ModelAttribute所标记的方法
* @author Administrator
*
*/
public class ModelAttributeController {

@RequestMapping("/attr1")
public String attr1(User user) {
user.setUname("xiaoli");
return "hello5";
}
/**
* 处理器方法当中的参数会直接放到模型数据中
* reqeust.setAttribute(类对象名称首字母小写)
* 键的名称是类的名字首字母小写
* @param user2
* @return
*/
@RequestMapping("/attr2")
public String attr2(User user2) {
user2.setUname("laowang");
return "hello5";
}
/**
* 模型数据中包含两个相同的的数据,但是名字不一样user,user3
* @param user3
* @param model
* @return
*/
@RequestMapping("/attr3")
public String attr3(User user3,Model model) {
user3.setUname("laowang");
model.addAttribute("user3", user3);
return "hello5";
}
/**
* 加了@ModelAttribute的参数会把原来模型数据的名字改了,
* user-->user4
* @param user4
* @return
*/
@RequestMapping("/attr4")
public String attr4(@ModelAttribute("user4")User user4) {
user4.setUname("xiaozhang");
return "hello5";
}
/**
* 在方法定义上使用@ModelAttribute:
* Spring MVC在调用目标处理方法前,
* 会先逐个调用在方法上标注了@ModelAttribute的方法
* @param user
* @return
*/
@RequestMapping("/attr5")
public String attr5() {
return "hello5";
}
@ModelAttribute("user5")
public User test() {
User user=new User();
user.setUname("admin");
return user;
}
@ModelAttribute("user6")
public User test6() {
User user=new User();
user.setUname("admin2");
return user;
}
@ModelAttribute("user7")
public User test7() {
User user=new User();
user.setUname("admin3");
return user;
}
@RequestMapping("/attr6")
public String attr6(
@ModelAttribute("user5")User user5,
@ModelAttribute("user6")User user6,
@ModelAttribute("user7")User user7) {
user5.setUname("admin");
user6.setUname("admin2");
user7.setUname("admin3");
return "hello5";
}
/**
* 添加模型数据的方法
* 假设现在参数中有一个User对象,
* 我们想给他命名user2,
* 要把它添加到模型数据中
* 1.ModelAndView (在模型当中必然有一个名字叫user的对象,还有一个user2)
* 2.Model (在模型当中必然有一个名字叫user的对象,还有一个user2)
* 3.Map (在模型当中必然有一个名字叫user的对象,还有一个user2)
* 4.直接写在参数里 (在模型当中必然有一个名字叫user的对象)
* 5.套用@ModelAttribute注解(只有一个user2)
*/
}

把模型数据存储到session中 如图:


SpringMVC数据绑定机制


SpringMVC数据转换
HttpMessageConverter<T>是spring中新增的一个接口,负责将请求信息转换为一个对象
<T>,将对象输出为响应信息

Spring MVC处理JSON向客户端发送json数据
编写jackson jar包
编写目标方法,使其返回JSON对应的对象或者集合
在该方法上添加@ResponseBody注解

在配置文件中增加
    <mvc:default-servlet-handler />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>


SpringMVC数据转换
HttpMessageConverter处理方式



SpringMVC处理JSON
如图:


SpringMVC处理JSON向服务器端提交json转为java对象



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值