简单的SpringMVC经典案例

主题:构建一个基于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,效果如下:

  

  

时间: 2024-08-05 11:11:52

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

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

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/31513065 上一篇已经实现了这个项目的整体的HTML和CSS: HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (一) 这篇博客直接在上篇的基础上完成,最终效果: 效果图1: 效果图2: 好了,请允许我把图片贴了两遍,方便大家看效果了~ 可以看出我们的图片的li的html其实还是挺复杂的,于是我把html文档做了一些修改: <span style=&quo

[CSDN] OpenCL用于计算机领域的13个经典案例

http://www.csdn.net/article/2013-10-29/2817319-the-application-areas-opencl-can-be-used 摘要:当使用加速器和OpenCL时,哪种类型的算法更加快速?来自弗吉尼亚理工大学的Wu Feng教授和他的团队例举了一份算法列表,分享了OpenCL常被用于计算机领域的13个经典案例. 哪种算法可以最好的映射GPU及矢量处理器呢?换句话说,当使用加速器和OpenCL时,哪种类型的算法更加快速? 来自弗吉尼亚理工大学的Wu

C#接口的经典案例

C#接口(interface)实例子(简单而经典)2008/12/04 10:04using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace AppTest { class Demo_interface { static void Main(string[] args) { //使用(注意,这里是使用接口 IPrint,下面是不同的实例,获得不同功能) I

Systemstate Dump分析经典案例(上)

前言 本期我们邀请中亦科技的另外一位Oracle专家老K来给大家分享systemstate dump分析的经典案例.后续我们还会有更多技术专家带来更多诚意分享. 老K作为一个长期在数据中心奋战的数据库工程师,看到小y前期的分享,有种跃跃欲试的感觉,也想把我日常遇到的一些有意思的案例拿出来分享讨论,希望我们都能从中获得些许收获,少走弯路.同时本文涉及到很多基础知识,又涉及看似枯燥的trace分析,但老K还是建议大家耐心看完本文. 精彩预告 如何分析cursor:pin S wait on X? 如

Spark之权威指南经典案例

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

Apache-rewrite+13个经典案例

Apache 重写规则的常见应用 (rewrite) 一:目的 本文旨在提供如何用Apache重写规则来解决一些常见的URL重写方法的问题,通过常见的 实例给用户一些使用重写规则的基本方法和线索. 二:为什么需要用重写规则? 一个网站,如果是长期需要放在internet上提供服务,必定会有不断地更新和维护,如临 时转移到其它服务器进行维护,重新组织目录结构,变换URL甚至改变到新的域名等等, 而为了让客户不会因此受到任何影响,最好的方法就是使用Apache Rewrite Rule(重写 规则)

多线程十大经典案例之一 双线程读写队列数据

本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5136035 转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8646902 欢迎关注微博:http://weibo.com/MoreWindows 在<秒杀多线程系列>的前十五篇中介绍多线程的相关概念,多线程同步互斥问题<秒杀多线程第四篇一个经典的多线程同步问题>及解决多线程同步互斥的常用方法

java多线程经典案例

/** * 典型案例:子线程执行10次,主线程执行100次,两者交替50次. */ package cn.itcast.lesson4; public class TestWaitNotify { public static void main(String[] args){ final Business business= new Business(); new Thread( new Runnable() { public void run() { for(int i=1;i<=50;i++

Java对象简单实用(计算器案例)

Java对象简单实用(计算器案例) 对 Java中的对象与属性,方法的使用,简单写了个案例 1 import java.util.Scanner; 2 class Calculste 3 { 4 int a; //定义两个整数 5 int b; 6 String option; //定义接收操作符的字符串 7 public void count(){ 8 9 //对操作符进行判断 10 switch(option){ 11 case "+": 12 System.out.println