注解配置的Spring MVC
基于注解配置的Spring MVC 简单的HelloWorld实例应用
2.1 问题
使用注解的方式重构helloworld应用案例。
2.2 方案
1. @RequestMapping注解应用
@RequestMapping可以用在类定义和方法定义上,它标明这个类或方法与哪一个客户请求对应。实例代码如下:

@RequestMapping("/day01")
public class HelloController {
@RequestMapping("/hello.form")
public String execute() throws Exception {
return "hello";
}
}

2. 开启@RequestMapping注解映射,需要在Spring的XML配置文件进行配置,配置代码如下:
<mvc:annotation-driven/>
3. @Controller注解应用
推荐使用@Controller注解声明Controller组件,这样可以使得Controller定义更加灵活,可以不用实现Controller接口,请求处理的方法也可以灵活定义。代码如下:

@Controller
@RequestMapping("/day01")
public class HelloController {
@RequestMapping("/hello.form")
public String execute() throws Exception {
return "hello";
}
}

4. 为了使@Controller注解生效,需要在Spring的XML配置文件中开启组件扫描定义,并指定Controller组件所在包,配置代码如下:
<context:component-scan base-package="com.souvc.controller"/>
使用的环境是eclipse ,jdk7.0 ,Tomcat 7.0,导入Spring依赖包。
2.3 步骤
步骤一: 新建一个新工程,工程名为SpringControler,如图下:

步骤二:增加HelloController类
使用注解的方式,修改HelloController类,代码如下所示:

package com.souvc.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/day01")
public class HelloController {
@RequestMapping("/hello.form")
public String execute() throws Exception {
return "hello";
}
}

步骤三:spring-mvc.xml的配置
在spring-mvc.xml文件中,开启@RequestMapping注解映射以及组件扫描,代码如下所示:

<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com.souvc.controller"/> <mvc:annotation-driven/> <!-- 定义视图解析器ViewResolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>

web.xml 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- 指定Spring的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

步骤四:测试
通过地址“http://localhost:8080/SpringControler/day01/hello.form”访问HelloController:

原文作者:daliu_it ;
博文出处:标明本文链接地址 ;
注解配置的Spring MVC的更多相关文章
- 基于注解配置的Spring MVC 简单的HelloWorld实例应用
2.1 问题 使用注解的方式重构helloworld应用案例. 2.2 方案 1. @RequestMapping注解应用 @RequestMapping可以用在类定义和方法定义上,它标明这个类或方法 ...
- Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用
Spring 注解驱动(二)Servlet 3.0 注解驱动在 Spring MVC 中的应用 Spring 系列目录(https://www.cnblogs.com/binarylei/p/1019 ...
- 使用JavaConfig和注解方式实现零xml配置的Spring MVC项目
1. 引言 Spring MVC是Spring框架重要组成部分,是一款非常优秀的Web框架.Spring MVC以DispatcherServlet为核心,通过可配置化的方式去处理各种web请求. 在 ...
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...
- 基于XML配置的Spring MVC 简单的HelloWorld实例应用
1.1 问题 使用Spring Web MVC构建helloworld Web应用案例. 1.2 方案 解决本案例的方案如下: 1. 创建Web工程,导入Spring Web MVC相关开发包. Sp ...
- 非web环境的注解配置的spring项目应用(non-web, Spring-data-jpa, JavaConfig, Java Application, Maven, AnnotationConfigApplicationContext)
非web环境的spring应用 springframework提供的spring容器,非常适合应用于javaweb环境中. 同时,spring组件的低耦合性为普通java应用也提供了足够的支持. 以下 ...
- IDEA中配置Maven+spring MVC+tomcat
一:配置Maven安装教程如下: http://blog.csdn.net/qq_32588349/article/details/51461182 实际安装过程中,如果按照教程配置如下属性,最后创建 ...
- Tomcat配置和Spring MVC配置
Tomcat启动时,先找系统变量CATALINA_BASE,如果没有,则找CATALINA_HOME.然后找这个变量所指的目录下的conf文件夹,从中读取配置文件.最重要的配置文件:server.xm ...
- Spring注解配置、Spring aop、整合Junit——Spring学习 day2
注解配置: 1.为主配置文件引入新的命名空间(约束) preference中引入文件 2.开启使用注解代理配置文件 <?xml version="1.0" encoding= ...
随机推荐
- Hdu 5073 Galaxy 精度问题
思路: 其实求解很简单直接说解法,移动K个后 上下的角动量最小,能肯定是相连的(n-k)个,至于为什么 你自己好好想想(easy): 对于一些等质量的质点中心在 所在位置和除以点的个数 average ...
- ASP.NET 应用程序生命周期
1.请求到达IIS服务器,IIS根据文件后缀找到对应的ISAPI(Internet Server API)扩展来处理,这个配置可在网站属性里的“根目录”选项卡中的“配置”里看到.可以看到,ashx.a ...
- Solr入门指南
本文转自http://chuanliang2007.spaces.live.com/blog/cns!E5B7AB2851A4C9D2!499.entry?wa=wsignin1.0 因为搜索引擎功能 ...
- tcpdump VS tshark用法(转)
Tcpdump是网络协议分析的基本工具.tshark是大名鼎鼎的开源网络协议分析工具wireshark (原名叫ethereal)的命令行版本,wireshark可对多达千余种网络协议进行解码分析.W ...
- 【leetcode列】3Sum
现在的问题是,我开始思考:一是制定了一些,然后设置这个数字,除了里面找到两个数字.最后,计算和.重复,供N的数量,需要N-2周期. 我的问题是如何找到的其他两个数字,其实,我想引用Two Sum内部解 ...
- sql中 in 、not in 、exists、not exists 使用方法和区别
% 的一类. NOT IN:通过 NOT IN keyword引入的子查询也返回一列零值或很多其它值. 以下查询查找没有出版过商业书籍的出版商的名称. SELECT pub_name FROM pub ...
- 对于COM对象使用ComPtr代替传统指针
对于COM对象来说使用传统指针比较麻烦,还要记得Release()防止内存泄漏,一不小心就会出现各种各样的问题.针对这种问题微软提供了对于COM对象的智能指针ComPtr,这里是官方文档https:/ ...
- [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- FTP文件操作之上传文件
上传文件是一个比较常用的功能,前段时间就做了一个上传图片的模块.开始采用的是共享文件夹的方式,后来发现这种方法不太好.于是果断将其毙掉,后来选择采用FTP的方式进行上传.个人感觉FTP的方式还是比较好 ...
- Lua 数据库访问(转)
本文主要为大家介绍 Lua 数据库的操作库:LuaSQL.他是开源的,支持的数据库有:ODBC, ADO, Oracle, MySQL, SQLite 和 PostgreSQL. 本文为大家介绍MyS ...