主题:构建一个基于SpringMVC的HelloWord Web 项目

目的:快速体验什么是SpringMVC

方案

  1、创建工程,命名:SpringMVC

  

  2、导包

  

  3、在SRC下添加spring-mvc.xml配置文件

   (注意:名字可以随便取,最好就是看上就知道是什么)

  

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> </beans>

  4、在web.xml配置封装在Spring里面的servlet--DispatcherServlet前端控制器,并指定spring-mvc.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" 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>SpringMVC</display-name>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<!-- DispathcherServlet 前端控制器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 变量名随便取 -->
<param-name>contextConfigLocation</param-name>
<!-- 指定SpringMVC配置文件名 -->
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!-- load-on-startup等于1,则表示容器启动就实例化此Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!-- 要与上面Servlet的名字对应 -->
<servlet-name>SpringMVC</servlet-name>
<!-- 用来匹配客户端请求 -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>

  5、在spring-mvc.xml中配置 【HandlerMapping组件】------------------作用------>设置客户端请求与Controller

                       【InternalResourceViewResolver组件】--作用------>设置视图配置

【HelloController】------------------------作用------->测试请求处理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 定义客户端请求映射关系 -->
<!-- HeanlerMapping是Spring核心组件之一 -->
<bean id="headlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<map>
<entry key="/hello.action">
<value>helloController</value>
</entry>
</map>
</property>
</bean> <!-- 增加HelloController的Bean -->
<bean id="helloController" class="controller.HelloController" /> <!-- 定义视图解释器(Spring核心组件之一) -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

  6、编写HelloController【注意:需要实现Controller接口】

package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller{ @Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
ModelAndView mv = new ModelAndView("hello");
System.out.println("处理hello.action请求");
return mv;
} }

  7、在WEB-INF文件夹下新增"jsp"文件夹,并添加hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
欢迎来到Spring的世界!
</body>
</html>

  8、跑起来吧兄弟们~然后访问http://localhost/SpringMVC/hello.action,效果如下:

  

  

简单的SpringMVC经典案例的更多相关文章

  1. Linux运维之道(大量经典案例、问题分析,运维案头书,红帽推荐)

    Linux运维之道(大量经典案例.问题分析,运维案头书,红帽推荐) 丁明一 编   ISBN 978-7-121-21877-4 2014年1月出版 定价:69.00元 448页 16开 编辑推荐 1 ...

  2. Altera OpenCL用于计算机领域的13个经典案例(转)

    英文出自:Streamcomputing 转自:http://www.csdn.net/article/2013-10-29/2817319-the-application-areas-opencl- ...

  3. php中foreach()函数与Array数组经典案例讲解

    //php中foreach()函数与Array数组经典案例讲解 function getVal($v) { return $v; //可以加任意检查代码,列入要求$v必须是数字,或过滤非法字符串等.} ...

  4. HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (二)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/31513065 上一篇已经实现了这个项目的整体的HTML和CSS: HTML5 C ...

  5. 经典案例复盘——运维专家讲述如何实现K8S落地

    经典案例复盘——运维专家讲述如何实现K8S落地 背景介绍 运满满自开始微服务改造以来,线上线下已有数千个微服务的 Java 实例在运行中.这些 Java 实例部署在数百台云服务器或虚机上,除少数访问量 ...

  6. springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目

    一个简单的用户登录系统 用户有账号密码,登录ip,登录时间 打开登录页面,输入用户名密码 登录日志,可以记录登陆的时间,登陆的ip 成功登陆了的话,就更新用户的最后登入时间和ip,同时记录一条登录记录 ...

  7. Python递归的经典案例

    目录 : 一.递归的简介 二.递归的经典应用   2.1 递归求阶乘   2.2 递归推斐波那契数列   2.3 二分法找有序列表指定值   2.4 递归解汉诺塔 前言: 当我们碰到诸如需要求阶乘或斐 ...

  8. SpringMVC经典系列-15对SpringMVC的总结---【LinusZhu】

    注意:此文章是个人原创,希望有转载须要的朋友们标明文章出处,假设各位朋友们认为写的还好,就给个赞哈.你的鼓舞是我创作的最大动力,LinusZhu在此表示十分感谢,当然文章中如有纰漏,请联系linusz ...

  9. Spark之权威指南经典案例

    hadoop权威指南上有一个求历史最高温度的经典案例,源数据如下: -- sample.txt0067011990999991950051507004+68750+023550FM-12+038299 ...

随机推荐

  1. MVC使用TempData将返回的string传到另一个控制器页面中显示!

    我需要把数据库中查询出的数据,传递到另一个控制器的页面去显示. https://blog.csdn.net/ma_jiang/article/details/8164212 看了上面这篇文章,反应过来 ...

  2. .net程序和管理员权限的一些事

    1.对某个方法设置管理员权限运行(未考证)(假的,必须以管理员权限启动,不然报错) [PrincipalPermission(SecurityAction.Demand, Role = @" ...

  3. Lily-一个埋点管理工具

    本文来自网易云社区 前言 在很多项目中,埋点数据使用表格来统计的,随着项目的进行,数据量越来越复杂,越来越难以维护.所以很多公司都已经开发了一整套系统,从埋点的录入到代码的输出. 我们项目中iOS和A ...

  4. 【文文殿下】NOIp2018游记

    Day-1 本段更新于 2018年11月8日23:26:44 今天还在机房里面,无所事事吧.上午睡了一上午,出去理了一下发,花了20块钱 QAQ. 下午来到机房,复习了一下exgcd的东西. 发现自己 ...

  5. 转---谈谈HTTP协议中的短轮询、长轮询、长连接和短连接

    作者:伯乐在线专栏作者 - 左潇龙 http://web.jobbole.com/85541/ 如有好文章投稿,请点击 → 这里了解详情 引言 最近刚到公司不到一个月,正处于熟悉项目和源码的阶段,因此 ...

  6. linux防火墙(三)—— iptables语法之匹配条件

    一.iptables规则的匹配条件类型有三类 1.通用匹配:可直接使用,不依赖于其他条件或扩展,包括网络协议.IP地址.网络接口等条件 2.隐含匹配:要求以特定的协议匹配作为前提,包括端口.TCP标记 ...

  7. django安装与环境调配

    91.查看当前已有虚拟环境 2.进入虚拟环境 3.退出当前虚拟环境 4.创建python隔离环境前先可用此先查看已经拥有什么版本的python 5.创建一个名为first的python隔离环境 6.安 ...

  8. 找到IIS 站点对应的站点日志

    IIS6 下 IIS7 下 1 找到日志文件的路径 2 找到站点ID 3 打开日志文件路径,找到站点ID 对应的日志文件夹.文件夹的最后一位数字,就对应着站点ID.

  9. 题外话 -- windows10系统C盘空间变大 CPU莫名跑满

    场景描述: 安装windows10一段时间了,发现C盘空间越来越小 CPU有时候,莫名其妙的跑满,造成操作卡顿. 如何处理参考: windows10 C盘空间清理:https://jingyan.ba ...

  10. JIRA服务器搭建

    JJIRA服务器搭建 http://wiki.csdn.net/display/CSDN/Atlassian CSDN国内代理: http://atlassian.csdn.net/m/btc/atl ...