要传递数据到页面上,需要用到构造方法ModelAndView(String viewName, String modelName, Object modelObject),页面上采用EL表达式取出值。
HelloController:
package com.xdy.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloController implements Controller{
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setAttribute("hello", "welcome to spring!");
String result = "hello xiongdy";
return new ModelAndView("/welcome","result",result);
}
}
welcome.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>springMVC HelloWold!</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
${hello }
<br>
传递数据:${result }
</body>
</html>
页面效果如下: