操作步骤:

1)下载spring framework开发包,给eclipse安装spring开发插件,如何安装开发插件&下载开发包请参考我的博文:《Spring(一):eclipse上安装spring开发插件&下载Spring开发包

2)使用eclipse创建Dynamic web project,并把spring mvc开发必须包引入,引入commons-logging日志包;

3)修改web.xml配置文件,配置dispatcherServlet;在src下创建一个springmvc.xml配置文件,配置文件内指定扫描包,及配置视图解析器。

/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>SpringMVC_01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 因为我这里安装了spring ide 插件,因此可以 alt + / 找到dispatcherServlet -->
<!-- The front controller of this Spring Web application, responsible for
handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</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>
<load-on-startup>1</load-on-startup>
</servlet> <!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

配置src/springmvc.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 配置自定义扫描的包 -->
<context:component-scan base-package="com.dx.springlearn"></context:component-scan> <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图(路径) -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

4)创建一个springmvc的handlers包,并在包下创建HelloWord.java,并把类注解为Controller;抒写一个hello方法,把该方法注解为一个Action。

 package com.dx.springlearn.handlers;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HelloWord {
/**
* 1.使用@RequestMapping注解来映射请求的url
* 2.返回值会通过视图解析器解析为实际的物理视图,对于InternalResourceViewResolver视图解析器,
* 会做如下解析:通过prefix+returnVal+suffix 拼接出来的实际的物理视图,然后做转化操作。
* prefix:/WEB-INF/views/
* reuturnVal:success
* suffix:.jsp
* */
// 接收请求的路径http://localhost:8080/hello
// 响应返回的视图物理位置:/WEB-INF/views/success.jsp
@RequestMapping("/hello")
public String hello(){
System.out.println("hello word...");
return "success";
}
}

在WebContent目录下创建index.jsp,并添加链接:

<a href="hello">hello action</a>

在WEB-INF/下创建views文件夹,在views文件夹中添加success.jsp页面,页面内添加内容"Success Page ..."

到这里为止,项目完整结构如下:

5)运行测试项目是否正常运行。

点击“hello action”链接后:

另外一种配置dispatcherServlet的方式(也是默认配置方式):

1)修改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>SpringMVC_01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 因为我这里安装了spring ide 插件,因此可以 alt + / 找到dispatcherServlet -->
<!-- The front controller of this Spring Web application, responsible for
handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</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>
-->
<load-on-startup>1</load-on-startup>
</servlet> <!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

2)把springmvc.xml转移位置到WEB-INF/<servlet-name>-servlet.xml,其中<servlet-name>就是web.xml配置的servlet-name名称。

把springmvc.xml转移到WEB-INF下,并重命名为:springDispatcherServlet-servlet.xml,内容做修改。

修改后项目结构如下:

SpringMVC(一):搭建一个SpringMVC helloword项目的更多相关文章

  1. Intellij IDEA采用Maven+Spring MVC+Hibernate的架构搭建一个java web项目

    原文:Java web 项目搭建 Java web 项目搭建 简介 在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring ...

  2. Spring框架——事务管理方式搭建一个小的项目

    学习Spring框架,通过事务管理的方式搭建一个小的项目,该项目可以查询对数据库中的图书库存数量进行修改. 首先,使用MVC分层的设计模式思想搭建项目目录结构. 此部分代码源码之中都有相关注释,所以尽 ...

  3. asp.netmvc 三层搭建一个完整的项目

    接下来用 asp.net mvc 三层搭建一个完整的项目: 架构图: 使用的数据库: 一张公司的员工信息表,测试数据 解决方案项目设计: 1.新建一个空白解决方案名称为Company 2.在该解决方案 ...

  4. 如何搭建一个WEB服务器项目(二)—— 对数据库表进行基本的增删改查操作

    使用HibernateTemplate进行增删改查操作 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出 ...

  5. 从零开始的Spring Boot(1、搭建一个Spring Boot项目Hello World)

    搭建一个Spring Boot项目Hello World 写在前面 从零开始的Spring Boot(2.在Spring Boot中整合Servlet.Filter.Listener的方式):http ...

  6. 搭建一个Web API项目(DDD)

    传送阵:写在最后 一.创建一个能跑的起来的Web API项目 1.建一个空的 ASP.NET Web应用 (为什么不直接添加一个Web API项目呢,那样会有些多余的内容(如js.css.Areas等 ...

  7. 如何快速搭建一个 Node.JS 项目并进入开发?

    了解:如何快速搭建一个项目并进入开发? 在此不概述 Node.JS 的历史以及发展过程. 因为之前接触过通过 Java 开发语言,所以明确地知道一个服务器所需的文件,以及一个服务器所需要的操作. 那么 ...

  8. 如何搭建一个spring boot项目

    什么是springboot? Spring Boot俗称微服务.Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特 ...

  9. 搭建一个springMVC项目以及遇到的问题

    首先找到jar包(lz现在还在学习maven,以后回了,就用maven了,自己配置时,jar包不全就很容易到时搭建失败)

随机推荐

  1. 安装VMware workstation遇到的两个问题:安装过程中的DLL问题和安装后打开需要的管理权限问题

    1.安装过程中遇到Microsoft runtime DLL安装程序未能完成安装的问题? 在遇到这个问题时不要点击确定,需要在开始菜单中输入%temp%,然后跳转到一个文件夹里,找到后缀为setup的 ...

  2. Spring boot(4)-应用打包部署

    1.Spring Boot内置web spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函数入口启动.其内置Tomcat容器或Jetty容器,具体由配置来决定 ...

  3. spring boot — InputStream

    @Componentpublic class TextFileDownloadView extends AbstractFileDownloadView { @Override protected I ...

  4. 每天学习点js

    ---- 有人会说js很简单,很容易学,但是要想把这门语言用活,用精通还是需要下很大的功夫的.那么我们就来看看接下来这几道js面试题吧. 题1 if(!(a in window)){ ; } cons ...

  5. 转载:解决微信OAuth2.0网页授权回调域名只能设置一个的问题

    项目地址:https://github.com/HADB/GetWeixinCode 说明:微信项目很多,但是回调域名有限,经常使用,做个笔记. 解决微信OAuth2.0网页授权只能设置一个回调域名的 ...

  6. lua精灵移除报对象非法

    function addLeftCard(isVisible) if self.left_CardSprite == nil then self.left_CardSprite = cc.Sprite ...

  7. vim的配置

    修改根目录下.vimrc文件: 1.设定解码,支持中文 set fileencodings=utf-8,ucs-born,gb18030,gbk,gb2312,cp936 set termencodi ...

  8. Build to win

    UPDATE:看到周筠老师的评论里的链接,那版式真的非常舒服.我想想模仿模仿他的布局来看看,虽然感觉做的也不是太好.另外对博客内容稍作修改. 一.获得小黄衫的感受 很幸运能够获得"领骑衫&q ...

  9. 敏捷冲刺每日报告四(Java-Team)

    第四天报告(10.28  周六) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://gi ...

  10. C语言——第一次作业

    **学习内容总结** 本周进行了算法的初步学习,用计算机的思维方式去思考问题,并学习了如何用传统程序框图表示算法. 相关内容: 1.算法是用来解决问题的方法与步骤. 2.计算机擅长重复,常用枚举的方法 ...