Springmvc_3(ModeAndView)

  1.准备jar

  2.编写controller

  

package com.home.springmvc.modelandview;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


/**
 * 此类是:Springmvc 的ModelAndView
 * @author hpc
 * @2017年2月17日下午10:51:42
 */
@Controller
public class SpringmvcModelAndView {
    /**
     * 目标方法的返回值可以是 ModelAndView 类型。 
     * 其中可以包含视图和模型信息
     * SpringMVC 会把 ModelAndView 的 model 中数据放入到 request 域对象中. 
     */
    @RequestMapping("/springmvcModelAndView")
    public ModelAndView springmvcModelAndView(){
        // 创建ModelAndView用于携带model和视图
        ModelAndView mv = new ModelAndView();
        // 添加指定要显示的视图名字
        mv.setViewName("success");
        // 添加model(以键值对的方式),会将其存入到request域中去
        mv.addObject("name", "dyn");
        //返回ModelAndView
        return mv;
    }
    
}

 3 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <a href="springmvcSessionAttributes">springmvcSessionAttributes</a>
    <br>
    <a href="springmvcModelAndView">springmvcModelAndView</a>
    <br>
    <a href="springmvcMap">springmvcMap</a>
    <br>
    <form action="springmvcPojo">
        userName:<input type="text" name="userName">
        <br>
        userSex:<input type="text" name="userSex">
        <br>
        userAge:<input type="text" name="userAge">
        <br>
        address.city:<input type="text" name="address.city">
        <br>
        address.street:<input type="text" name="address.street">
        <br>
        <input type="submit" value="submit">
    </form>
</body>
</html>

   4 success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    springmvc pojo
    <br>
    <br>
    <%-- springmvcMap name: ${requestScope.name } --%>
    <br>
    <br>
    springmvcModelAndView name:${requestScope.name }
    <br>
    <br>
    <%-- springmvcSessionAttributes name:${sessionScope.name } --%>
</body>
</html>

   5 结果

 

posted @ 2017-03-02 16:09  墨.荷  阅读(1306)  评论(0编辑  收藏  举报