SpringMVC学习系列(7) 之 格式化显示(三)
(){
return date;
}
public void setMoney(double money){
this.money=money;
}
public void setDate(Date date){
this.date=date;
}
}
复制代码
注意:这里的money和date不再是String类型,而是它们自己本来的类型。
把FormatController.java改为如下内容:
复制代码
package com.demo.web.controllers;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.demo.web.models.FormatModel;
@Controller
@RequestMapping(value = "/format")
public class FormatController {
@RequestMapping(value="/test", method = {RequestMethod.GET})
public String test(Model model) throws NoSuchFieldException, SecurityException{
if(!model.containsAttribute("contentModel")){
FormatModel formatModel=new FormatModel();
formatModel.setMoney(12345.678);
formatModel.setDate(new Date());
model.addAttribute("contentModel", formatModel);
}
return "formattest";
}
}
复制代码
注意:这里代码里面只有赋值已经没有格式化的内容了。
更改视图formattest.
jsp的内容如下:
复制代码
<%@ page language="java" contentType="text/
html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
money:
date: