SpringMVC配置入門
我的開發環境
開發工具: 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配置入門的更多相关文章
- 依賴注入入門——Unity(二)
參考博客文章http://www.cnblogs.com/kebixisimba/category/130432.html http://www.cnblogs.com/qqlin/tag/Unity ...
- SpringMVC配置实例
一.SpringMVC概述 MVCII模式实现的框架技术 Model--业务模型(Biz,Dao...) View--jsp及相关的jquery框架技术(easyui) Contraller--Dis ...
- springmvc 配置拦截器
package com.aaa.zxf.interceptor; import org.springframework.boot.autoconfigure.SpringBootApplication ...
- GOOGLE搜索從入門到精通V4.0
1,前言2,摘要3,如何使用本文4,Google簡介5,搜索入門6,初階搜索 6.1,搜索結果要求包含兩個及兩個以上關鍵字 6.2,搜索結果要求不包含某些特定資訊 6.3,搜索結果至少包含多個關鍵字中 ...
- Flask從入門到入土(三)——模板
模板是一個包含響應文本的文件,其中包含佔位變量表示的動態部分,其具體值只是請求上下文中才能知道.使用真實值替換變量,再返回最終得到的響應字符串,這一過程稱爲渲染.爲了渲染模板,Flask使用了一個名爲 ...
- SpringMVC配置与使用
一.MVC概要 MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范,用一种将业务逻辑.数据.显示分离的方法组织代码,MVC主要作用是降低了视图与业务 ...
- 3.2.2 SpringMVC配置式开发
SpringMVC配置式开发 1. SpringMVC运行原理(执行过程) 2. 需求 用户提交一个请求, 服务端处理器接收到请求后, 给出一条信息,在相应页面中显示该条信息 3. 开发步骤 (1) ...
- springmvc配置之mvc:annotation-driven
为了简化springmvc配置,spring同时引入了mvc namespace, 配置了 <mvc:annotation-driven/> spring会默认注册a RequestMap ...
- SpringMVC配置多视图-内容协商原理
SpringMVC配置多视图-内容协商原理 2014年03月06日 16:46:59 日积月累_滴水石穿 阅读数:10964更多 个人分类: SpringMVC Spring Framework ...
随机推荐
- WPF绑定数据源
using System;using System.Collections.Generic;using System.Collections.ObjectModel;using System.Comp ...
- C++设计模式-Factory工厂模式
Factory1.定义创建对象的接口,封装对象的创建2.将实际创建工作延迟到子类中,例如,类A中药使用类B,B是抽象父类,但是在类A中不知道具体要实例化哪一个B的子类,但是在类A的子类D中是可以知道的 ...
- 百度Ueditor
最近用到了百度Ueditor,也来写一写百度Ueditor的使用教程: 一.从官网下载百度Ueditor,http://ueditor.baidu.com/website/download.html, ...
- 码农谷 找出N之内的所有完数
题目描述 一个数如果恰好等于它的因子之和,这个数就称为"完数". 例如,6的因子为1.2.3,而6=1+2+3,因此6是"完数". 编程序找出N之内的所有完数, ...
- 9.S5PV210的时钟系统
1.时钟域:MSYS.DSYS.PSYS(1)因为S5PV210的时钟体系比较复杂,内部外设模块太多,因此把整个内部的时钟划分为3大块,叫做3个域.(2)MSYS: CPU(Cortex-A8内核). ...
- VBS中对Error的处理
VBScript语言提供了两个语句和一个对象来处理"运行时错误",如下所示: On Error Resume Next语句 On Error Goto 0语句 Err对象 简单介绍 ...
- nodejs 下载,安装,测试(windows环境下)
1.下载 nodejs英文官网:http://nodejs.org/ nodejs中文官网:http://nodejs.cn/ 两个都可以下载,用户可以根据自己的网络来选择用哪个下载. 进入官网之后版 ...
- GBDT(MART) 迭代决策树入门教程 | 简介
GBDT(MART) 迭代决策树入门教程 | 简介 http://blog.csdn.net/w28971023/article/details/8240756
- Java NIO教程 Channel
Channel是一个连接到数据源的通道.程序不能直接用Channel中的数据,必须让Channel与BtyeBuffer交互数据,才能使用Buffer中的数据. 我们用FileChannel作为引子, ...
- navicat for mysql
下载地址:https://www.navicat.com/cht/download 详情:http://baike.baidu.com/link?url=zo3CUg3HC5XUHkz4YqXO6Em ...