Spring-mvc 4.0 ModelAndView (一)

把 ModelAndView 對象裏面的鍵值對使用request.attribute()放到request域對象裏

public ModelAndView addObject(String attributeName, Object attributeValue){
    getModelMap().addAttribute(attribute ,attributeValue);
    return this;
}

使用方式

超鏈接

<a href="modelandview">modelandview</a>

JAVA類

@Controller
public class Hello {
    @RequestMapping("/modelandview")
    public ModelAndView modAndView() {
        String forward = "model";
        ModelAndView modelAndView = new ModelAndView(forward);
        modelAndView.addObject("time",new Date());
        return modelAndView;
    }

}

model.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>model</title>
  </head>
  <body>
    time : ${requestScope.time}
  </body>
</html>

下一篇 Spring-mvc 4.0 ModelAndView (二)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章