1.视图模型封装,ModelAndView可以向页面返回视图的同时吧模型也传入页面

2.注解参数,springMvc很好的地方在于简单,高效,@RequestParam注解能非常好的取得页面参数

代码:

1.创建model

package com.java1234.model;

public class Student {

private int id;
private String name;
private int age;

public Student() {
super();
// TODO Auto-generated constructor stub
}

public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

2.创建controller

package com.java1234.controller;

import java.util.ArrayList;
import java.util.List;

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

import com.java1234.model.Student;

@Controller
@RequestMapping("/student")
public class StudentController {

private static List<Student> studentList=new ArrayList<Student>();

static{
studentList.add(new Student(1,"张三",11));
studentList.add(new Student(2,"李四",12));
studentList.add(new Student(3,"王五",13));
}

@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mav=new ModelAndView();
mav.addObject("studentList", studentList);
mav.setViewName("student/list");
return mav;
}

@RequestMapping("/preSave")
public ModelAndView preSave(@RequestParam(value="id",required=false) String id){
ModelAndView mav=new ModelAndView();
if(id!=null){
mav.addObject("student", studentList.get(Integer.parseInt(id)-1));
mav.setViewName("student/update");
}else{
mav.setViewName("student/add");
}
return mav;
}
}

3.页面向controller提交参数:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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="${pageContext.request.contextPath}/student/preSave.do">添加学生</a>
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<c:forEach var="student" items="${studentList }">
<tr>
<td>${student.id }</td>
<td>${student.name }</td>
<td>${student.age }</td>
<td><a href="${pageContext.request.contextPath}/student/preSave.do?id=${student.id}">修改</a></td>
</tr>
</c:forEach>
</table>

</body>
</html>

  

springMvc-视图模型封装及注解参数的更多相关文章

  1. JavaWeb - 模仿SpringMVC抽取 BaseServlet + 封装表单参数

    模仿SpringMVC抽取一个BaseServlet,接收所有请求,然后自动封装表单参数和分发到对应的servlet执行,下面用一个页面表单提交,转发显示的项目做示例. 1)首先准备一个Entity, ...

  2. KnockoutJS 3.X API 第二章 数据监控(1)视图模型与监控

    数据监控 KO的三个内置核心功能: 监控(Observable)和依赖性跟踪(dependency tracking) 声明绑定(Declarative bindings) 模板(Templating ...

  3. 关于SpringMVC映射模型视图的几点小事

    一.SpringMVC概述 SpringMVC为展现层提供的基于MVC设计理念的优秀的Web框架,是目前最主流的MVC框架之一. SpringMVC通过一套MVC注解,让POJO成为处理请求的控制器, ...

  4. SpringMVC的controller方法中注解方式传List参数使用@RequestBody

    在SpringMVC控制器方法中使用注解方式传List类型的参数时,要使用@RequestBody注解而不是@RequestParam注解: //创建文件夹 @RequestMapping(value ...

  5. 简单实现springmvc框架(servlet+自定义注解)

    个人水平比较菜,没有这么高的实力简单实现springmvc框架,我是看了一个老哥的博客,这老哥才是大神! 原文链接:https://www.cnblogs.com/xdp-gacl/p/4101727 ...

  6. SpringMVC视图解析器

    SpringMVC视图解析器 前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视 图解析器.当我们对SpringMVC控制的资源发起 ...

  7. SpringMVC视图解析器(转)

    前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视图解析器.当我们对SpringMVC控制的资源发起请求时,这些请求都会被Sprin ...

  8. SpringMVC第三篇【收集参数、字符串转日期、结果重定向、返回JSON】

    业务方法收集参数 我们在Struts2中收集web端带过来的参数是在控制器中定义成员变量,该成员变量的名字与web端带过来的名称是要一致的-并且,给出该成员变量的set方法,那么Struts2的拦截器 ...

  9. SPRINGMVC 视图介绍

    SpringMVC视图解析器 前言 在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视图解析器.当我们对SpringMVC控制的资源发起请 ...

随机推荐

  1. 【转】ANT安装、环境变量配置及验证

    http://www.cnblogs.com/yuzhongwusan/archive/2013/03/26/2982411.html Posted on 2013-03-26 14:01 yuzho ...

  2. idea中java项目增加module后,手动增加xml文件,合并到webapp/WEB-INFO或WEB-INFO(包)

    当手工增加一个module,增加配置文件(如:web.xml)需要合并到文件夹里,要不众多文件在一个src文件夹下,太凌乱. 1. 合并到webapp/WEB-INFO下 a. 首先增加webapp目 ...

  3. C# 原码与补码的转换

    /// <summary> /// 求一个16位数数的补码 /// </summary> /// <param name="OriginalCode" ...

  4. 蚂蚁金服HR电话面

    1.先自我介绍一下. 2.问到项目问题.(项目介绍,用到哪些技术,如何测试的.) 3.一些基础知识. a) list.map.set是继承了collection的吗?  List:1.可以允许重复的对 ...

  5. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

  6. 图解SSH上传安装eclipse

    安装eclipse 步骤: 1.SSH上传eclipse tar文件 2.linux下Computer-->FilesSystem-->soft显示上传的压缩包 3.解压eclipse文件 ...

  7. 2017-10-17 NOIP模拟赛2

    a [问题描述]你是能看到第一题的 friends 呢.——hja何大爷对字符串十分有研究,于是天天出字符串题虐杀 zhx.何大爷今天为字符串定义了新的权值计算方法.一个字符串由小写字母组成,字符串的 ...

  8. sap smartform 打印乱码问题

    在smartforms打印的时候会遇到中英文结合的form 有时候系统会处理时出现乱码   有时不会  不知道是系统的事情还是配置的事情 现在是我的解决办法  因为是中英文结合 在中文环境建立form ...

  9. 分层图最短路 【bzoj1579】[Usaco2009 Feb]Revamping Trails 道路升级

    1579: [Usaco2009 Feb]Revamping Trails 道路升级 Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M< ...

  10. 算法学习分析-点分治 HDU 6269 Master of Subgraph

    首先给出定义 点分治是一种处理树上路径的工具 挂出一道题目来:Master of Subgraph 这道题目让你求所有联通子图加和所能产生数字,问你1到m之间,那些数字可以被产生 这道题目,假如我们利 ...