SpringMVCHelloWorld
来源:互联网 发布:加人软件使用视频 编辑:程序博客网 时间:2024/06/14 17:12
1、SpringMVC组件说明
- DispatcherServlet(前端控制器):相当于MVC中的controller,dispatcherServlet是整个流程控制的中心,由他调用其他组件处理用户的请求,DispatcherServlet的存在降低了组件之间的耦合性
- HandlerMapping(处理器映射器):负责根据用户的请求找到相应的Handler(处理器),springMVC提供了不同的映射器实现了不同的映射方式,例如:配置文件方式,注解方式
- Handler(处理器):在DispatcherServlet的控制下,handler对具体的用户请求进行处理
- HandlerAdapter(处理器适配器):适配器模式,通过扩展适配器,可以对更多类型的handler进行控制
- ViewResolver(视图解析器):负责将处理结果生成视图(View),SpringMVC提供了多种视图类型,即:jstlView、freemarkerView、pdfView等
2、开始编码
2.1、在/WebContent/WEB-INF/web.xml文件中注册前端控制器
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>springmvcfirst1208</display-name> <!-- springmvc前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等) 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 第一种:*.action,访问以.action结尾 由DispatcherServlet进行解析 第二种:/,所以访问的地址都由DispatcherServlet进行解析,对于静态文件的解析需要配置不让DispatcherServlet进行解析 使用此种方式可以实现 RESTful风格的url 第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面时, 仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错。 --> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>classpath指定了springmvc的配置文件路径
2.2、添加springMVC的配置文件,注册处理器映射器、处理器适配器和视图解析器
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "><!-- 处理器映射器 --><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/><!-- 处理器适配器 --><bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/><!-- 配置handler --><bean name="/queryItems.action" class="org.test.mvc.controller.ItemsController"/></beans>2.3、创建Controller:org.test.mvc.controller.ItemsController.java
package org.test.mvc.controller;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import org.test.mvc.po.Items;public class ItemsController implements Controller {@Overridepublic ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {// 调用service查找 数据库,查询商品列表,这里使用静态数据模拟List<Items> itemsList = new ArrayList<Items>();// 向list中填充静态数据Items items_1 = new Items();items_1.setName("联想笔记本");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苹果手机!");itemsList.add(items_1);itemsList.add(items_2);//返回ModelAndViewModelAndView modelAndView = new ModelAndView();modelAndView.addObject("itemsList", itemsList);modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");return modelAndView;}}2.4、创建jsp页面,用jstl解析商品列表
<%@ 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="${itemsList }" 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 }/item/editItem.action?id=${item.id}">修改</a></td></tr></c:forEach></table></form></body></html>2.5、源代码
SpringMVCHelloWorld
1 0
- SpringMVCHelloWorld
- Result-Global_Results_全局结果集
- Debian系统备份成iso镜像的工具安装启动
- POJ1837:Balance
- android ViewGoup事件分发机制dispatchTouchEvent
- xtrabackup 全备、增量备份恢复
- SpringMVCHelloWorld
- 求解一个矩阵的逆矩阵
- 单例模式
- The following classes could not be instantiated: android.support.v7.widget.RecyclerView v7中找不到的终极方法
- javascript 闭包的理解以及它的好处与坏处
- Spark 随机森林算法原理、源码分析及案例实战
- 关于控件滑动至顶部的监听
- 【UOJ22】. [UR #1]外星人
- 【消息队列MQ】各类MQ比较