SpringBoot-视图

JackSon

自定义全局ObjectMapper代替默认ObjectMapper

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.text.SimpleDateFormat;

@Configuration
public class JacksonConf {
    
    // 此处根据返回值, 对原有的`ObjectMapper`进行替换
    @Bean
    public ObjectMapper getObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return objectMapper;
    }
}

下面例子是获取当前时间的请求

@GetMapping("/now.json")
public @ResponseBody Map now() {
	Map map = new HashMap();
	map.put("time", new Date());
	return map;
}
// {"time": "2022-11-15 11:38:05"}

Redirect 和 Forward

Controller中重定向可以返回redirect:为前缀的URI, 内部重定向则可以使用前缀为forward:的前缀

@RequestMapping("/order/saveorder.html")
public String saveOrder(Order order) {
	Long orderId = service.addOrder(order);
	return "redirect:/order/detail.html?orderId=" + orderId;
}

@RequestMapping("/bbs")
public String index() {
	return "forward:/bbs/module/1-1.html"
}

@RequestMapping("/bbs/module/{type}-{page}")
public ModelAndView module(@PathVariable int type, @PathVariable int page) {
	...
}

对于bbs请求, 都会到module方法, 因为forwardURLbbs/module/1-1.html
ModelAndView中设置带有redirect:前缀的URI

ModelAndView view = new ModelAndView("redirect:/order/detail.html?orderId=" + orderId);
// 或者使用`RedirectView`的类
RedirectView view = new RedirectView("/order/detail.html?orderId=" + orderId);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值