SpringMVCæä¾äº1.ModelAndViewä½ä¸ºè¿åå¼æ·»å 模åæ°æ®2.Mapä½ä¸ºä¼ å ¥åæ°ï¼å¨å¤çæ¹æ³è¿åæ¶å°Mapä¸çæ°æ®æ·»å å°æ¨¡åä¸
ä¸ãModelAndView
ModelAndViewä¸æ¢å å«äºè§å¾ä¿¡æ¯ä¹å å«äºæ¨¡åæ°æ®ï¼SpringMVCä¼æModelAndViewçmodelä¸æ°æ®æ¾å ¥å°request å对象ä¸ã
å¨å¤çæ¹æ³ä¸ï¼
package springmvc;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import springmvc.User;
@Controller
@RequestMapping("/springmvc")
public class HelloWorld {
private static final String SUCCESS = "success";
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
String viewName = SUCCESS;
ModelAndView modelAndView = new ModelAndView(viewName);
//æ·»å 模åæ°æ®å°ModelAndViewä¸
modelAndView.addObject("time", new Date());
return modelAndView;
}
}
å¨index.jspä¸
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SpringMVC</title>
</head>
<body>
<a href="springmvc/testModelAndView" target="_blank" rel="external nofollow" >Test ModelAndView</a>
</body>
</html>
å¨success,jspä¸
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SpringMVC</title>
</head>
<body>
<h4>success page</h4>
time:${requestScope.time}
<br><br>
</body>
</html>
è¿è¡èµ·æ¥å¯ä»¥çå°ææ
äºãMap
ç®æ æ¹æ³å¯ä»¥æ·»å Mapç±»å(å®é ä¸ä¹å¯ä»¥æ¯Modelç±»åæè ModelMapç±»å)çåæ°
å¤çæ¹æ³æ件ï¼
package springmvc;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import springmvc.User;
@Controller
@RequestMapping("/springmvc")
public class HelloWorld {
private static final String SUCCESS = "success";
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map) {
System.out.println(map.getClass().getName());
map.put("names", Arrays.asList("Tom", "Jerry", "Mike"));
return SUCCESS;
}
}
index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SpringMVC</title>
</head>
<body>
<a href="springmvc/testMap" target="_blank" rel="external nofollow" >Test Map</a>
</body>
</html>
success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>SpringMVC</title>
</head>
<body>
<h4>success page</h4>
names:${requestScope.names}
<br><br>
</body>
</html>
è¿è¡ä¸ä¸å¯ä»¥çå°ææï¼