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 ...
随机推荐
- 埃及分数-IDA*
Description 在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数.如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的.对于一个分数a/b, ...
- 自动化脚本过程中出现This element neither has attached source nor attached Javadoc...的解决方法
This element neither has attached source nor attached Javadoc and hence no Javadoc could be found Ec ...
- Linux:远程到linux的图形界面
一般linux都没有安装图形界面,可以通过VNC服务来实现步骤如下: 一.安装vnc server1.查看是否安装vncrpm -q vnc-serverpackage vnc is not inst ...
- Yarn&Mapreduce参数的具体含义和配置参考
Yarn & Mapreduce 参数的具体含义和配置 http://zh.hortonworks.com/blog/how-to-plan-and-configure-yarn-in-hdp ...
- WEB-INF下jsp跳转
今天才知道:浏览器是不允许直接访问WEB-INF文件夹的 瞬间感觉自己好shi的有没有,纠结了2天 看来还得通过springMVC来跳,,,
- html弹出窗并用遮罩层的实例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Mycat配置文件schema.xml参数配置
Mycat原理: Mycat的原理中最重要的一个动词是"拦截",它拦截了用户发送过来的SQL语句,首先对SQL语句做了一些特定的分析:如分片分析.路由分析.读写分离分析.缓存分析等 ...
- Apache错误:[error] (OS 10038)在一个非套接字上尝试了一个操作
Apache错误:[error] (OS 10038)在一个非套接字上尝试了一个操作 博客分类: vb2005xu软件学习 OSApache防火墙PHPWindows 日志如下:[ ...
- 删除多余的provisioning profile
当你的工程中有个人证书和企业证书时,你xCode打包的时候可能会发现一个问题:当你的Code Signing设置iOS Developer和Automatic,并且你的Bundle ID和Team选择 ...
- Elasticsearch分布式搜索集群配置
配置文件位于%ES_HOME%/config/elasticsearch.yml文件中,用Editplus打开它,你便可以进行配置. 所有的配置都可以使用环境变量,例如:node.rack: ${ ...