springMVC框架的入门demo
2018-03-12 10:30
387 查看
1.Spring web mvc和struts2都属于表现层的框架,它是spring框架的一部分
2.springMVC的处理流程(都是围绕这个请求和响应来处理)
3.springMVC的入门程序
Springmvc的执行流程:
1.用户将请求发送导前端控制器dispatcherServlet
2.dispatcherServlet收到请求调用handlerMapping处理器映射器
3.处理映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给dispatcherServlet
4.dispatcherServlet通过handleradapter处理器适配器调用处理器
5.执行处理器(Controller,也交后端控制器)
6.controller执行完成返回ModelAndView
7.handlerAdapter将controller执行结构modelAndView返回给dispatherServlet
8.dispatherServlet将modelandView传给viewReslover视图解析器
9.ViewReslover解析后返回具体view
10.dispatherServlet对view进行渲染视图(即将模型数据填充至视图中)
11.dispatcherServlet响应给用户
创建一个web工程.
springMvc的配置文件springmvc.xml,开启扫描注解.<?xml version="1.0" en
4000
coding="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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.springmvc.controller"/>
</beans>Web.xml中配置前端控制器dsipatherservlet,springmvc在web.xml中配置文件<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>Contorller和jsp页面package com.springmvc.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.servlet.ModelAndView;
import com.springmvc.pojo.Items;
@Controller
public class ItemsController {
@RequestMapping("/itemList")
public ModelAndView itemsList(){
List<Items> itemList = new ArrayList<Items>();
//商品列表
Items items_1 = new Items();
items_1.setName("联想笔记本_3");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机!");
itemList.add(items_1);
itemList.add(items_2);
//创建modeladnView对象
ModelAndView modelAndView = new ModelAndView();
//添加model
modelAndView.addObject("itemList",itemList);
//添加视图
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}jsp页面<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
2.springMVC的处理流程(都是围绕这个请求和响应来处理)
3.springMVC的入门程序
Springmvc的执行流程:
1.用户将请求发送导前端控制器dispatcherServlet
2.dispatcherServlet收到请求调用handlerMapping处理器映射器
3.处理映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给dispatcherServlet
4.dispatcherServlet通过handleradapter处理器适配器调用处理器
5.执行处理器(Controller,也交后端控制器)
6.controller执行完成返回ModelAndView
7.handlerAdapter将controller执行结构modelAndView返回给dispatherServlet
8.dispatherServlet将modelandView传给viewReslover视图解析器
9.ViewReslover解析后返回具体view
10.dispatherServlet对view进行渲染视图(即将模型数据填充至视图中)
11.dispatcherServlet响应给用户
创建一个web工程.
springMvc的配置文件springmvc.xml,开启扫描注解.<?xml version="1.0" en
4000
coding="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:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.springmvc.controller"/>
</beans>Web.xml中配置前端控制器dsipatherservlet,springmvc在web.xml中配置文件<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>Contorller和jsp页面package com.springmvc.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.servlet.ModelAndView;
import com.springmvc.pojo.Items;
@Controller
public class ItemsController {
@RequestMapping("/itemList")
public ModelAndView itemsList(){
List<Items> itemList = new ArrayList<Items>();
//商品列表
Items items_1 = new Items();
items_1.setName("联想笔记本_3");
items_1.setPrice(6000f);
items_1.setDetail("ThinkPad T430 联想笔记本电脑!");
Items items_2 = new Items();
items_2.setName("苹果手机");
items_2.setPrice(5000f);
items_2.setDetail("iphone6苹果手机!");
itemList.add(items_1);
itemList.add(items_2);
//创建modeladnView对象
ModelAndView modelAndView = new ModelAndView();
//添加model
modelAndView.addObject("itemList",itemList);
//添加视图
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
return modelAndView;
}
}jsp页面<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!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>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryitem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
相关文章推荐
- 微信支付 java 服务端demo (v3版本app支付 springMVC框架中)
- Dubbo入门-分布式原理详解--搭建一个最简单的Demo框架
- springmvc学习笔记(1)-框架原理和入门配置
- Shiro权限控制框架入门2:如何将Shiro非入侵地整合到SpringMVC等Web项目中
- 微信支付 java 服务端demo (v3版本app支付 springMVC框架中)
- Spring学习笔记-springMVC入门Demo
- Dubbo入门---搭建一个最简单的Demo框架
- openjpa框架入门_Demo概览(一)
- 【SpringMVC框架】springmvc入门程序-环境搭建
- SpringMVC学习(1):SpringMVC框架结构详解和入门案例详解
- 《Java从入门到放弃》框架入门篇:springMVC数据校验
- Dubbo入门理解详解!(入门---搭建一个最简单的Demo框架)
- 最新SpringMVC框架搭建入门教程
- 【SSM-SpringMVC框架】SpringMVC入门小程序
- hessian入门与springMVC框架集成---Service服务
- 微软同步框架(MSF)入门开篇(附SnapShot快照Demo)
- 微信支付 java 服务端demo (v3版本app支付 springMVC框架中)
- 【原创】NIO框架入门(一):服务端基于Netty4的UDP双向通信Demo演示
- springMVC框架入门
- springMVC框架--入门程序(一)