springmvc的model是实体类,可以理解为把数据库里的一张表变成了一个对象
/*** */ package com.test.model;/*** @ClassName: User.java* @Description: TODO(用一句话描述该文件做什么) * * @author JerryZhou* @Date 2014-7-15 上午10:24:04 */ public class User {public int id;public String name;public String sex;/*** @return the id*/public int getId() {return id;}/*** @param id the id to set*/public void setId(int id) {this.id = id;}/*** @return the name*/public String getName() {return name;}/*** @param name the name to set*/public void setName(String name) {this.name = name;}/*** @return the sex*/public String getSex() {return sex;}/*** @param sex the sex to set*/public void setSex(String sex) {this.sex = sex;}}
controller控制器,把model里的对象拿来,进行一些动作
/*** */ package com.test.controller;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController;import com.test.model.User;/*** @ClassName: HelloWorldController.java* @Description: TODO(用一句话描述该文件做什么) * * @author JerryZhou* @Date 2014-7-14 下午5:54:46 */ public class HelloWorldController extends MultiActionController{// http://localhost:8080/SpringMVCTest/hellowlrldpublic ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception { User user = new User();user.setId(1);user.setName("Test");user.setSex("男");List<User> usrst = new ArrayList<User>();User user1 = new User();user1.setId(2);user1.setName("张三");user1.setSex("男");usrst.add(user1);User user2 = new User();user2.setId(3);user2.setName("李四");user2.setSex("女");usrst.add(user2);User user3 = new User();user3.setId(4);user3.setName("王五");user3.setSex("男");usrst.add(user3);Map<String, Object> model =new HashMap<String, Object>();model.put("usrst", usrst);ModelAndView mv = new ModelAndView();mv.addObject(user);mv.addAllObjects(model);mv.setViewName("welcome");return mv;}}
然后就是视图层
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body>Welcome ${user.name} <br><br>Welcome ${user.sex} <br><br>User List<br/><br><table border=""><tr><td>序号</td><td>编号</td><td>名称</td><td>性别</td></tr><c:forEach items="${usrst }" var="usr" varStatus="status"><tr><td>${status.index + 1 }</td><td>${usr.id }</td><td>${usr.name }</td><td>${usr.sex }</td></tr></c:forEach></table></body> </html>
从请求地址(控制器->model;控制器->view)到输出,这个规则是路由定制的,配置文件是这样的
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean>
<bean name="/hellowlrld" class="com.test.controller.HelloWorldController"></bean> (重要)
</beans>
xml里控制了访问(当访问url/hellowlrld时,请求com.test.controller.HelloWorldController这个控制器)
一个user的表,里面有id,name,sex。。。映射成面向对象(表即对象,字段即属性)
控制器使用这个对象(即操作这个表)
new一个user对象
为id赋值为1
为name赋值为test
为sex赋值为男
new一个list对象,并把user放list里,变成一个数组(?。。。。)
new一个user对象
为id赋值为2
为name赋值为张三
。。。
。。。。
。。。。
mv.setViewName("welcome");
最后去welcome.jsp这个页面