想做一点自己喜欢的东西,研究了一下springMVC,所以就自己搭建一个小demo,可供大家吐槽。

  1. 先建一个WEB工程,这个相信大家都会,这里不在多说。
  2. 去网上下载spring jar包,然后在WEB-INF下新建一个lib文件,将下载的jar包放进去。如下图
  3. 在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"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><display-name>springMVCtest</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>springMVCtest</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVCtest</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 加载spring的xml配置文件到 spring的上下文容器中 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param>
    </web-app>

    注意:servlet-name的名字必须和接下来要创建的servlet.xml的名字要对应。 context-param指定applicationContext.xml 文件的位置。

  4. 在WEB-INF下新建一个xml,因为我上文中定义的名字是springMVCtest,所以,文件名字为springMVCtest-servlet.xml:
    <?xml version="1.0" encoding="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:task="http://www.springframework.org/schema/task"xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd "><!-- 使用默认的注解映射 <mvc:annotation-driven /><mvc:resources location="/" mapping="/index.html" /><context:annotation-config></context:annotation-config>--><!-- 自动扫描controller包中的控制器 --><context:component-scan base-package="com.controller" /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean><!-- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><prop key="/index.do">controllerDoem</prop></props></property></bean><bean id="controllerDoem" class="com.controller"><property name="view"><value>index</value></property></bean>--></beans>

  5. 在src目录下新建文件applicationContext.xml,
    <?xml version="1.0" encoding="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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
    </beans>

  6. 在src下新建一个包名,com.controller,新建一个类DemoController,注:这里的类名必须已Controller结尾,之前我写其他的类名,然后项目一直404,后来改成这样之后就好了,具体原因,还没搞清楚,知道的大神欢迎回复。
    package com.controller;import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;@Controller
    public class DemoController {@RequestMapping("/index")public ModelAndView index(){//创建模型跟视图,用于渲染页面。并且指定要返回的页面为index页面ModelAndView mav = new ModelAndView("index");return mav;}}

  7. 最后一步,在WEB-INF下新建目录jsp,里面新建文件index.jsp
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body><h1>hahahaha</h1>
    </body>
    </html>

  8. 将项目部署到tomcat下,启动tomcat,就可以运行了,访问http://localhost:8080/springMVCtest/index

    最后配上整个项目结构:

    搞定!(如有错误,请大神指出。)

转载于:https://www.cnblogs.com/mei0619/p/6560332.html

SpringMVC搭建+实例相关推荐

  1. springmvc与mysql实例_Spring+Mybatis+SpringMVC+Maven+MySql搭建实例

    摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+SpringMVC+MySql的搭建实例,文章写得很详细,有代码有图片,最后也带有运行的效果. 一.准备工作 1. 首先创建一个 ...

  2. Spring+Mybatis+SpringMVC+Maven+MySql(SSM框架)搭建实例

    这篇文章我们来实现使用maven构建工具来搭建Spring+Mybatis+SpringMVC+MySql的框架搭建实例.工程下载 使用maven当然得配置有关环境了,不会配置的请看我前几篇文章,都有 ...

  3. 【SpringMVC架构】SpringMVC入门实例,解析工作原理(二)

    上篇博文,我们简单的介绍了什么是SpringMVC,这篇博文,我们搭建一个简单SpringMVC的环境,使用非注解形式实现一个HelloWorld实例,从简单入手,逐步深入. 环境准备 我们需要有基本 ...

  4. SSH框架总结(框架分析+环境搭建+实例源代码下载)

    首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是眼下较流行的一种Web应用程序开源集成框架,用于构建灵活.易于扩展的多层Web应用程序. 集成SSH框 ...

  5. springMVC简单实例

    springMVC简单实例 参考: 使用springMVC实现简单的登录例子 - Admol - 博客园 https://www.cnblogs.com/admol/articles/4199546. ...

  6. 使用SpringMVC搭建第一个项目

    概述 使用SpringMVC搭建第一个项目,入门教程,分享给大家. 详细 代码下载:http://www.demodashi.com/demo/10596.html 一.概述 1.什么是Spring ...

  7. 郭卓惺:互动课堂的搭建实例及相关领域应用

    本文来自腾讯云技术沙龙,本次沙龙主题为在线教育个性化教学技术实践 演讲嘉宾:郭卓惺 | 腾讯视频云终端技术中心 随着在线教育覆盖面的增加,互动课堂授课方式正在向多样化发展,为了适应新形式的发展,腾讯视 ...

  8. SSH框架总结(框架分析+环境搭建+实例源码下载)

     版权声明:本文为博主原创文章,未经博主允许不得转载. 首先,SSH不是一个框架,而是多个框架(struts+spring+hibernate)的集成,是目前较流行的一种Web应用程序开源集成框架,用 ...

  9. elasticsearch集群搭建实例

    下个月又开始搞搜索了,几个月没动这块还好没有落下. 晚上在自己虚拟机上搭建了一个简易搜索集群,分享一下. 操作系统环境: Red Hat 4.8.2-16 elasticsearch : elasti ...

最新文章

  1. AttributeError: Cant get attribute SPPF on module models
  2. Intellij Idea 创建Web项目入门(一)
  3. SparkSql官方文档中文翻译(java版本)
  4. 零基础学HTML5和CSS3前端开发第一课
  5. rstudio 保存_Rstudio学习笔记
  6. nginx location 正则表达式匹配多个地址_就是要让你搞懂Nginx,这篇就够了!
  7. 熟悉c语言运行环境实验原理,c语言实验报告1
  8. 成功领导者的20个好习惯
  9. 国外游戏开发商吐槽:开发VR游戏付账单的钱都赚不到
  10. k-means算法概述
  11. 【转】Android业务组件化之URL Scheme使用
  12. JS入门到精通完整版
  13. IE可以实现的浏览器js下载文件的方法
  14. 卡尔曼滤波器工作原理
  15. vue倒计时翻页插件
  16. 词干提取算法Porter Stemming Algorithm解读
  17. 做外贸如何防止邮箱被封?已解决!
  18. RandomAccess接口的使用
  19. anaconda python更换清华源
  20. 2020第八届“泰迪杯”特等奖(基于 BERT 深度语言模型的“智慧政务”文本挖掘应用)

热门文章

  1. 一个逼格很低的appium自动化测试框架
  2. idea 内存溢出解决方法
  3. MFC学习之路之多媒体 --(1) DirectShow
  4. Linux系统Shutdown命令定时关机详解
  5. map 循环_被问到Spring循环依赖怎么解决?秀给面试官看!内附图解
  6. 1.关于python
  7. oracle连接满报错日志,Oracle归档日志满了导致Oracle连接(ORA-00257)报错处理
  8. python使用redis做缓存_Python中的Redis客户端缓存(二)
  9. 教你玩转CSS 属性选择器
  10. tomcat mysql如何优化_Tomcat+Mysql高并发配置优化讲解