springMVC入门程序-注解开发环境配置

在进行springMVC开发时,我们也希望通过注解进行开发,这样比较快捷方便。现将springMVC开发步骤记录如下:

1、新建web程序,导入springMVC需要的jar包:

2、配置web.xml文件。主要是进行servlet的配置。

 1  <servlet>
 2       <servlet-name>springmvc</servlet-name>
 3       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4       <init-param>
 5           <param-name>contextConfigLocation</param-name>
 6           <param-value>classpath:springmvc.xml</param-value>
 7       </init-param>
 8   </servlet>
 9
10   <servlet-mapping>
11       <servlet-name>springmvc</servlet-name>
12       <url-pattern>*.action</url-pattern>
13   </servlet-mapping>

注意:其中servlet-class的路径org.springframework.web.servlet.DispatcherServlet可以在spring-webmvc-3.2.0.RELEASE.jar中找到。

3、在工程目录下新建resource-folder命名为config,然后创建springmvc.xml文件,并进行配置,头部可以拷贝。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 8         http://www.springframework.org/schema/mvc
 9         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context-3.2.xsd
12         http://www.springframework.org/schema/aop
13         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
14         http://www.springframework.org/schema/tx
15         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
16
17 </beans>

4、在springmvc.xml文件中配置注解开发的映射器、适配器和视图解析器:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 8         http://www.springframework.org/schema/mvc
 9         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context-3.2.xsd
12         http://www.springframework.org/schema/aop
13         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
14         http://www.springframework.org/schema/tx
15         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
16
17     <!-- 注解的映射器 -->
18     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
19     <!-- 注解的适配器 -->
20     <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
21
22
23     <!-- 使用 mvc:annotation-driven代替上边注解映射器和注解适配器配置
24     mvc:annotation-driven默认加载很多的参数绑定方法,
25     比如json转换解析器就默认加载了,
26     如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
27     实际开发时使用mvc:annotation-driven
28      -->
29     <!-- <mvc:annotation-driven></mvc:annotation-driven> -->
30     <!-- 视图解析器 -->
31       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
32 </beans>

注意:在实际开发工程可以使用<mvc:annotation-driven></mvc:annotation-driven>代替映射器和适配器。

5、编写模型层,并开发controler处理器。在处理器类上面使用@Controller注解,并在需要映射的方法上面使用@RequestMapping("/queryItems")注解。这样就可以访问地址/queryItmes.action了。

注意:注解的时候,使用的url会默认加上.action,所以输入网站的时候不要忘记了带上.action.

附:

其实,我们也可以改变springmvc.xml的配置,在视图解析器上配置视图的前缀和后缀。

1 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
2           <!--视图的前缀  -->
3           <property name="prefix" value="" />
4           <!--视图的后缀  -->
5           <property name="suffix" value=".jsp" />
6       </bean>   

当加入了视图解析器的前缀和后缀以后,我们在跳转页面的时候就不用再加了,否则会出现重复导致路径错误。

1         // 指定视图
2         //modelAndView.setViewName("itemsList.jsp");
3         //配置了后缀,所以不要加.jsp
4         modelAndView.setViewName("itemsList");        
时间: 2024-07-31 14:35:13

springMVC入门程序-注解开发环境配置的相关文章

golang入门一之开发环境配置

一:golang 下载地址 https://studygolang.com/dl 二:环境变量配置 1, Linux vim /etc/profile.d/go.sh #新建环境变量文件,输入以下内容 export GOROOT=/opt/go # 安装目录 export GOPATH=/opt/gopath # 项目目录 export PATH=$PATH:$GOROOT/bin export GOPROXY=https://mirrors.aliyun.com/goproxy/ # 使用阿里

一起学Google Daydream VR开发,快速入门开发基础教程一:Android端开发环境配置一

原文因涉及翻墙信息,被强制删除,此文为补发! 准备工作 进入Google Daydream开发者官网,开启准备工作,官网地址:https://vr.google.com/daydream/developers/ -------------------------------------------------------------------------------------------------------------------- Google Daydream开发者网址: https

Daydream从入门到精通——快速入门开发基础教程二:Android端开发环境配置二

开始部署 上篇介绍了开发Daydream Android VR需要的基本环境,这篇我们来看看如何部署和运用官方示例. -------------------------------------------------------------------------------------------------------------------- Daydream快速入门开发基础教程一:Android端开发环境配置一 http://blog.csdn.net/jaikydota163/arti

【OpenCV入门教程之一】 OpenCV 2.4.8 +VS2010的开发环境配置

目录(?)[-] 因为读研期间的研究方向是图像处理所以浅墨这段时间闭门研究了很多OpenCV和图像处理相关的知识与内容眼看自己积累到一定的程度了于是决定开始开设这个OpenCV系列专栏总结自己所学也分享知识给大家 还是先放出待会儿的测试用图 下载和安装OpenCV SDK sources里面是源代码想查看完整的源代码需要用cmake来解包如何解包大家百度一下就可以或者下次浅墨来专门讲一讲这里就先不多说了 配置环境变量 工程包含include目录的配置 工程库lib目录的配置 链接库的配置 在Wi

[笔记]Android开发环境配置及HelloWorld程序

Android的开发需要以下四个工具: 1.JDK 2.Eclipse 3.Android SDK 4.ADT 具体功能: 1.JDK.JDK即Java Development Kit(Java开发工具包),包括JAVA运行环境,基础类库和JAVA工具,是JAVA运行的基础. 2.Eclipse.Eclipse是开发JAVA程序的软件工具. 3.Android SDK.SDK即Software Development Kit(软件开发工具包).AndroidSDK指的是Android专属的软件开

Android adt bundle 开发环境配置及第一个“Hello world”程序运行

    最近在学习Android 顺便记录下学习过程当作复习吧,这是写的第一篇正式博客.  一.jdk环境配置     二.android adt bundle 下载     三.安装SDK     四.模拟器及真机调试     五.第一个程序 Hello world!     六.总结 一.jdk环境配置 jdk下载地址 选中"Accept License Agreement"之后才能下载,如下图: 下载之后 点击安装,一路next 之后配置环境变量 JAVA_HOME,找到jdk安

Java入门 - 语言基础 - 02.开发环境配置

原文地址:http://www.work100.net/training/java-environment-setup.html 更多教程:光束云 - 免费课程 开发环境配置 序号 文内章节 视频 1 JDK安装 2 IDE安装 请参照如上章节导航进行阅读 在本章节中我们将为大家介绍如何搭建 Java 开发环境. 1.JDK安装 Java 可以运行在多种操作系统平台上,下面分别介绍在各主流平台上如何安装配置 JDK. 1.1.Windows上安装 1.1.1 下载JDK 首先我们需要下载 Jav

(转) 安装OpenCV:OpenCV 2.4.8或OpenCV 2.4.9 +VS 开发环境配置

因为读研期间的研究方向是图像处理,所以浅墨这段时间闭门研究了很多OpenCV和图像处理相关的知识与内容.眼看自己积累到一定的程度了,于是决定开始开设这个OpenCV系列专栏,总结自己所学,也分享知识给大家. 好了,这篇文章作为OpenCV的启程篇,自然少不了先系统地介绍OpenCV开发环境的配置. 浅墨前后经历过OpenCV 2.4.6,OpenCV 2.4.7,OpenCV 2.4.8这三个版本的配置,有时候还要涉及到三个版本之间的转换,所以还是对OpenCV的配置有一定的理解的,希望自己的一

安装OpenCV:OpenCV 3.0、OpenCV 2.4.8、OpenCV 2.4.9 +VS 开发环境配置

本系列文章由@浅墨_毛星云 出品,转载请注明出处.   文章链接:http://blog.csdn.net/poem_qianmo/article/details/19809337 作者:毛星云(浅墨)    微博:http://weibo.com/u/1723155442 邮箱: [email protected] 知乎:http://www.zhihu.com/people/mao-xing-yun 写作当前博文时配套使用的OpenCV版本: 2.4.8.2.4.9.3.0   ( 2014