我的開發環境
  開發工具:    springsource-tool-suite-2.9.0
  JDK版本:    1.6.0_29
  tomcat版本:apache-tomcat-7.0.26

  本文地址:http://www.cnblogs.com/sunang/p/3419544.html 轉載請注明出處^_^

本文要注意的點已经用          標注,請大家要特別注意。

go!

step1.在你的開發環境新建一個工程,引入jar包

Maven代碼:

<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<!-- JSP -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>

step2:編輯web.xml

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置SpringMVC前端Servlet控制器DispatcherServlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- 配置初始的DispatcherServlet上下文配置文件加載路徑 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/spring/spring-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 讓DispatcherServlet處理所有以".htm"結尾的URL -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!-- 配置DispatcherServlet上下文配置文件加載路徑 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/spring/spring-servlet.xml
</param-value>
</context-param>
<!-- 配置上下文載入器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>

step3.編輯DispatcherServlet上下文配置文件

在step2中,我們配置了DispatcherServlet上下文配置文件的路徑為conf/spring/spring-servlet.xml,所以在src/main/resources/conf/spring目錄下新建spring-servlet.xml文件,代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置SpringMVC配置文件所需的xml文件解析模板 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
xmlns:tx="http://www.springframework.org/schema/tx" 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://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置控制器要掃描的包路徑 -->
<context:component-scan base-package="www.asuan.com" />
<!-- 配置視圖解析器 -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 解析器解析/WEB-INF/jsp/路徑下,以.jsp結尾的視圖文件 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

step4.編寫controller和jsp文件

  在step3中,我們配置了包掃描路徑為"www.asuan.com",所以在src/main/java目錄下新建包www.asuan.com.controller,在包內新建HelloWorldController.java,代碼如下:

package www.asuan.com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
public class HelloWorldController {
@RequestMapping("/helloWorld")//url字段名
public String helloWorld() {
return "helloWorld";//jsp文件名
}
}

視圖解析路徑設置為WEB-INF/jsp,所以在WEB-INF/jsp目錄下新建helloWorld.jsp文件(文件名為controller方法中返回的字符串),代碼如下:

<!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>
<h2>Hello World!</h2>
</body>
</html>

step5.運行與調試
將工程部署到tomcat并運行,在瀏覽器輸入:http://localhost:8080/你設置的工程名/helloWorld.htm

運行結果:

complete!

SpringMVC配置入門的更多相关文章

  1. 依賴注入入門——Unity(二)

    參考博客文章http://www.cnblogs.com/kebixisimba/category/130432.html http://www.cnblogs.com/qqlin/tag/Unity ...

  2. SpringMVC配置实例

    一.SpringMVC概述 MVCII模式实现的框架技术 Model--业务模型(Biz,Dao...) View--jsp及相关的jquery框架技术(easyui) Contraller--Dis ...

  3. springmvc 配置拦截器

    package com.aaa.zxf.interceptor; import org.springframework.boot.autoconfigure.SpringBootApplication ...

  4. GOOGLE搜索從入門到精通V4.0

    1,前言2,摘要3,如何使用本文4,Google簡介5,搜索入門6,初階搜索 6.1,搜索結果要求包含兩個及兩個以上關鍵字 6.2,搜索結果要求不包含某些特定資訊 6.3,搜索結果至少包含多個關鍵字中 ...

  5. Flask從入門到入土(三)——模板

    模板是一個包含響應文本的文件,其中包含佔位變量表示的動態部分,其具體值只是請求上下文中才能知道.使用真實值替換變量,再返回最終得到的響應字符串,這一過程稱爲渲染.爲了渲染模板,Flask使用了一個名爲 ...

  6. SpringMVC配置与使用

    一.MVC概要 MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范,用一种将业务逻辑.数据.显示分离的方法组织代码,MVC主要作用是降低了视图与业务 ...

  7. 3.2.2 SpringMVC配置式开发

    SpringMVC配置式开发 1. SpringMVC运行原理(执行过程) 2. 需求 用户提交一个请求, 服务端处理器接收到请求后, 给出一条信息,在相应页面中显示该条信息 3. 开发步骤 (1) ...

  8. springmvc配置之mvc:annotation-driven

    为了简化springmvc配置,spring同时引入了mvc namespace, 配置了 <mvc:annotation-driven/> spring会默认注册a RequestMap ...

  9. SpringMVC配置多视图-内容协商原理

    SpringMVC配置多视图-内容协商原理 2014年03月06日 16:46:59 日积月累_滴水石穿 阅读数:10964更多 个人分类: SpringMVC   Spring Framework ...

随机推荐

  1. ios 使用可视化工具charles转换pcap文件,进行流量统计(通过tcpdump抓包)

    环境准备:使用mac电脑,下载xcode,Charles 连接iPhone手机,打开xcode-window-devices-查看设备UDID 打开终端:rvictl –s 设备号 ,查看虚拟端口号 ...

  2. 创建struct类型的数组

    在autoit中,如何创建类似这样的数组呢?如下方式,数组的element只是存储的地址相邻,所以我们可以这样做 $tagMYSTRUCT = "int code; char msg[10] ...

  3. ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '='

    多表查询出错,貌似是编码问题. 我比较的两个表的某个字段,设为查询条件,两个字段等于某个值,参照网上的某些论坛调用alter语句,但是依旧没有效果.最后直接拆分成两个条件,a.字段1=x值,b.字段2 ...

  4. lintcode-【中等】数飞机

    题目: 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 样例: 对于每架飞机的起降时间列表:[[1,10],[2,3],[5,8],[4,7]], ...

  5. 5,SFDC 管理员篇 - 数据模型 - 数据关联

    1,PickList 1,填写基本信息 2, 选择能角色的权限 3,在哪一个层上显示(object 上有多个 Record Type 对应多个层,需要选择在哪一个层显示) 4,Save   2,两个P ...

  6. 关于xib文件和storyboard文件的那些事儿

    在ios中,一般建议使用代码布局,因为使用代码布局,后期维护容易,拓展容易,并且可以实现动态加载很多数据,但是代码布局比较繁琐,不适合初学者.Xib布局或者Storyboard布局比较方便.下面介绍一 ...

  7. Android IOS WebRTC 音视频开发总结(六七)-- 在线教育虽火要做好其实不容易

    本文主要介绍在线教育这个行业,文章最早发表在我们的微信公众号上,支持原创,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.rtc.help 最近很多朋友在咨询在线教育的事(其实之 ...

  8. VS2013使用EF6连接MySql

    前提:a.安装MySql的VS插件(版本请下载最新版) 我用的是:mysql-for-visualstudio-1.1.4 b.安装用于.net连接程序  mysql-connector-net-6. ...

  9. Shell_参数替换(転)

    From: http://www.cnblogs.com/yjf512/archive/2013/06/03/3114803.html Bash中的$符号的作用是参数替换,将参数名替换为参数所代表的值 ...

  10. 通过WinForm控件创建的WPF控件无法输入的问题

    今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...