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 结果

 

转载于:https://www.cnblogs.com/mohehpc/p/6491128.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值