要传递数据到页面上,需要用到构造方法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>页面效果如下:
本文介绍了一个简单的Spring MVC示例,展示了如何使用HelloController类通过ModelAndView对象将数据传递到welcome.jsp页面,并在页面上利用EL表达式显示这些数据。
259

被折叠的 条评论
为什么被折叠?



