您的位置:首页 > 编程语言 > Java开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: