1.新建项目

  File->New->Other,选择Dynamic web project:

项目建好之后,目录结构如下:

2.WEB-INF/web.xml 中配置 dispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring mvc下的配置文件 ,如果不配置,默认加载WEB-INF下XXX-servlet.xml(XXX为上面的<servlet-name>)-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
...
</web-app>

3.在src目录下新建spring-web.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: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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.jackie.springmvc"></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.HelloWorld.java(com.jackie.springmvc.handlers下)

//1. 首先要在类的前面添加“Controller”注解,表示是spring的控制器
@Controller
public class HelloWorld {
//@RequestMapping, 是用于匹配请求的路径,比如这里匹配的请求路径就是“http://localhost:8080/springTest/springmvc/helloworld”
@RequestMapping("/helloworld")
public String hello(){
System.out.println("hello world");
//这个返回的字符串与上面springmvc.xml进行配合,springmvc.xml中声明了prefix和suffix,而夹在这两者之间的就是这里返回的字符串,所以执行完这个方法后,我们可以得到这样的请求资源路径“/WEB-INF/views/success.jsp”,这个success.jsp是需要我们新建的
return "success";
}
}

5.index.jsp(WebContent下)

 在新建success.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> <a href="helloworld">hello world</a> </body>
</html>

当访问index.jsp时,页面上会展示一个超链接,点击超链后,url中的地址就会发生跳转,由“http://localhost:8080/springTest/index.jsp”跳转到“http://localhost:8080/springTest/helloworld”,而这个url请求就会进入HelloWorld中的hello方法,因为其与该方法上的“/helloworld”匹配

6.success.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>
<title>success</title>
</head>
<body>
<h4>Success Page</h4>
</body>
</html>

[spring mvc]Hello World入门的更多相关文章

  1. Spring MVC 教程,快速入门,深入分析

    http://elf8848.iteye.com/blog/875830/ Spring MVC 教程,快速入门,深入分析 博客分类: SPRING Spring MVC 教程快速入门  资源下载: ...

  2. Spring MVC整合Mybatis 入门

    本文记录使用Intellij创建Maven Web工程搭建Spring MVC + Mybatis 的一个非常简单的示例.关于Mybatis的入门使用可参考这篇文章,本文在该文的基础上,引入了Spri ...

  3. Spring MVC 教程,快速入门,深入分析[1-11]

    资源下载: Spring_MVC_教程_快速入门_深入分析V1.1.pdf SpringMVC核心配置文件示例.rar     作者:赵磊 博客:http://elf8848.iteye.com   ...

  4. [转]Spring MVC 教程,快速入门,深入分析

    原文地址:http://elf8848.iteye.com/blog/875830 目录 一.前言 二.spring mvc 核心类与接口 三.spring mvc 核心流程图 四.spring mv ...

  5. Spring MVC 教程,快速入门,深入分析(转载)

    作者:赵磊 博客:http://elf8848.iteye.com 下载: Spring的官方下载网址是:http://www.springsource.org/download    (本文使用是的 ...

  6. 基于注解的Spring MVC的简单入门——简略版

    网上关于此教程各种版本,太多太多了,因为我之前没搭过框架,最近带着两个实习生,为了帮他们搭框架,我只好...惭愧啊...基本原理的话各位自己了解下,表示我自己从来没研究过Spring的源码,所以工作了 ...

  7. Spring MVC 简介及入门小例子

    说明:文章内容全部截选自实验楼教程[Spring MVC 简易教程] 一.什么是 Spring MVC Spring MVC 属于 SpringFrameWork 的后续产品,已经融合在 Spring ...

  8. Spring MVC之简单入门

    一.Spring MVC简介: 1.什么是MVC 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型(Model).视图(View)及控制器(Contr ...

  9. maven 纯注解一步一步搭建Spring Mvc项目(入门)

    初次接触spring MVC项目,通过一段时间的学习,本文介绍一种以纯注解的方法去配置spring MVC环境,让那些配置的.xml文件统统见鬼吧. 什么是Spring MVC Spring MVC属 ...

随机推荐

  1. ubuntu安装markdown

    # sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys BA300B7755AFCFAE linuxidc@linuxidc:~ ...

  2. ALV tree标准DEMO

    BCALV_TREE_01 ALV 树控制:构建层次树 BCALV_TREE_02 ALV 树控制:事件处理 BCALV_TREE_03 ALV 树控制:使用自己的上下文菜单 BCALV_TREE_0 ...

  3. IntelliJ IDEA的几个常用快捷键

    一.将IntelliJ IDEA的快捷键设置为Eclipse环境的快捷键 如果之前长期使用Eclipse作为开发工具的程序员在刚开始接触IDEA的时候肯定会很不习惯,所以如果你没有太多时间去研究的话可 ...

  4. linux下python编辑器的tab补全

    vi tab.py #!/usr/bin/env python # python startup file import sys import readline import rlcompleter ...

  5. cnn for qa

    最近在做QA系统,用tensorflow做了些实验,下面的的是一个cnn的评分网络.主要参考了<APPLYING DEEP LEARNING TO ANSWER SELECTION: A STU ...

  6. HDevelop数据类型

    *图形类型*图像Hwnd:=3600read_image(Image, 'fabrik')disp_obj(Image, Hwnd)*region 区域gen_rectangle1(Rectangle ...

  7. 编译安装MariaDB-10.0.21

    一.源码编译安装gcc-5.1.0 1.下载gcc源码包 Download (HTTP): http://ftpmirror.gnu.org/gcc/gcc-5.2.0/gcc-5.2.0.tar.b ...

  8. hadoop记录(一)

    linux基础和javase基础[包含mysql] 这些是基本功,刚开始也不可能学的很精通,最起码要对linux中的一些基本的命令混个脸熟,后面学习各种框架的时候都会用到,用多了就熟悉了.javase ...

  9. Hive的metastore

    hive --service metastore 默认端口是9083 <property> <name>hive.metastore.uris</name> < ...

  10. C++命名规则(转)

    如果想要有效的管理一个稍微复杂一点的体系,针对其中事物的一套统一.带层次结构.清晰明了的命名准则就是必不可少而且非常好用的工具. 活跃在生物学.化学.军队.监狱.黑社会.恐怖组织等各个领域内的大量有识 ...