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:

 
源码如下:http://yunpan.cn/cm3CqEXD9TeJf  访问密码 2d10
 
 

基于注解配置的Spring MVC 简单的HelloWorld实例应用的更多相关文章

  1. 基于XML配置的Spring MVC 简单的HelloWorld实例应用

    1.1 问题 使用Spring Web MVC构建helloworld Web应用案例. 1.2 方案 解决本案例的方案如下: 1. 创建Web工程,导入Spring Web MVC相关开发包. Sp ...

  2. 注解配置的Spring MVC

    基于注解配置的Spring MVC 简单的HelloWorld实例应用   2.1 问题 使用注解的方式重构helloworld应用案例. 2.2 方案 1. @RequestMapping注解应用 ...

  3. 使用 Spring 2.5 基于注解驱动的 Spring MVC

    http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...

  4. Spring MVC简单的HelloWorld例子

    1.web.xml配置(主要配置Servlet)[默认情况 Spring的配置文件在WEB-INF的<servlet-name>-servlet.xml] <?xml version ...

  5. spring启动,spring mvc ,要不要xml配置,基于注解配置

    老项目是09-11年搞的,用的是spring+struts2,没有用注解,全xml配置.web.xml中也配置了一大堆. 现在启动新项目,在项目中用spring+springmvc ,主要用注解,也用 ...

  6. spring mvc 基于注解 配置默认 handlermapping

    spring mvc 是类似于 Struts 的框架.他们都有一个最主要的功能就是URL路由.URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 ...

  7. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

  8. Spring mvc系列一之 Spring mvc简单配置

    Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...

  9. 基于XML配置的spring aop增强配置和使用

    在我的另一篇文章中(http://www.cnblogs.com/anivia/p/5687346.html),通过一个例子介绍了基于注解配置spring增强的方式,那么这篇文章,只是简单的说明,如何 ...

随机推荐

  1. 复利计算v6.0--web版--软件工程

    一.结对同伴 姓名:蔡舜 学号: 博客园地址:http://www.cnblogs.com/caishun/ http://www.cnblogs.com/caishun/p/5392896.html ...

  2. Entity FrameWork 增删查改

    Add #region 1.0 新增+void Add() /// <summary> /// 新增 /// </summary> static void Add() { // ...

  3. MVC中Action之间传值

    一  MVCAction之间的传值 之前一直觉得关于MVC里面的传值,只能从<视图—>Action>,和<Actoin->视图>但是今天在项目里面需要实现将几个视图 ...

  4. LNK1179 无效或损坏的文件: 重复的 COMDAT“_IID_IDispatchEx”

    fatal error LNK1179: invalid or corrupt file: duplicate comdat  "XXX" LNK1179 无效或损坏的文件: 重复 ...

  5. 面向对象的Javascript(4):重载

    在小项目中对于JavaScript使用,只要写几个function就行了.但在大型项目中,尤其是在开发追求 良好的用户体验的网站中,如SNS,就会 用到大量的JavaScrpt,有时JavaScrip ...

  6. windows的host文件的位置和作用

    在Window系统中有个Hosts文件(没有后缀名),在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\ ...

  7. table.appand(行数据) datagrid分页

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  8. 重新想象 Windows 8 Store Apps (68) - 后台任务: 控制通道(ControlChannel)

    [源码下载] 重新想象 Windows 8 Store Apps (68) - 后台任务: 控制通道(ControlChannel) 作者:webabcd 介绍重新想象 Windows 8 Store ...

  9. Fluent NHibernate example

    http://www.codeproject.com/Articles/26466/Dependency-Injection-using-Spring-NET http://stackoverflow ...

  10. [Xamarin.Android] ActionBar Tips

    [Xamarin.Android] ActionBar Tips ActionBar用途 快速搞懂 ActionBar的用途,可以参考下列文章: [Android]使用 ActionBarCompat ...