`
iloveflower
  • 浏览: 79808 次
社区版块
存档分类
最新评论
  • iloveflower: 呵呵。好好学习。。。。。。。。。。。。
    java 读书
  • Eric.Yan: 看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
    java 读书

Annotation Based Spring MVC Basics

 
阅读更多
http://sites.google.com/site/sritechforjava/spring-docs/spring-mvc-basics

Assuming that the student knows basic web application structure and all the configuration files required for Basic web application.
Assuming that the student knows Servlets.

To create Annotation based Spring MVC web application please follow the below steps:

Step 1 : Update your web.xml file with following configuration:

    <servlet>
        <servlet-name>spring-mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

DispatcherServlet - Spring's Web MVC framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for upload files.

A central servlet that dispatches requests to controllers and offers other functionality facilitating the development of web applications.
Is an expression of the “Front Controller” design pattern.
It is completely integrated with the Spring IoC container and as such allows you to use every other feature that Spring has.

The framework will, on initialization of a DispatcherServlet, look for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and create the beans defined there.


Step 2 : Create spring-mvc-servlet.xml under WEB-INF directory. (Observe that the file name should start with servlet-name of the DispatcherServlet-servlet.xml.)

Step 3 : The Basic spring-mvc-servlet.xml looks like below:

<?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"
       xsi:schemaLocation=
               "http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context.xsd">

</beans>

Step 4 : spring-mvc-servlet.xml file will contain all of your Spring Web MVC-specific components (beans). The exact location of this configuration file can be changed via a DispatcherServlet initialization parameter. You can specify that in web.xml like below:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:/spring/spring-context.xml</param-value>
    </context-param>
Step 5 : Just as with any other view technology you're integrating with Spring, for JSPs you'll need a view resolver that will resolve your views. The most commonly used view resolvers when developing with JSPs are the InternalResourceViewResolver and the ResourceBundleViewResolver.

Add the below configuration in your spring-mvc-servlet.xml file: (I am using InternalResourceViewResolver here in this example)

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

Step 6 :  Here as we are going to use annotations instead of configuring our Controllers in xml files, we also need to add the following configuration in spring-mvc-servlet.xml file, so that container scans all the controller components at the initialization of DispatcherServlet and they will be available.

    <context:component-scan base-package="com.sritech.samples">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

Step 7 : Your web application lib directory also need the following spring jars:

spring.jar
spring-webmvc.jar
commons-logging.jar

These jars will be deployed to the server and they are also used during the build process.
The above configuration allows your DispatcherServlet to scan for all the controllers in the specified package (Ex: com.sritech.samples).

All above 6 Steps are one time configuration you have to do to create your annotation based Spring MVC application.

Example 1:- Create Welcome Page using SimpleWelcomeController and Simple JSP.

Step 1 : Create the controller class:

package com.sritech.samples.mvc.example1;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class SimpleWelcomeController {

    @RequestMapping("/example1/welcome.htm")
    public void elementaryGreeter() {
    }
}

The @Controller annotation indicates that a particular class serves the role of a controller. There is no need to extend any controller base class or reference the Servlet API. You are of course still able to reference Servlet-specific features if you need to. The basic purpose of the @Controller annotation is to act as a stereotype for the annotated class, indicating its role. The dispatcher will scan such annotated classes for mapped methods, detecting @RequestMapping annotations.

The @RequestMapping annotation is used to map URLs like '/example1/welcome.htm' onto an entire class or a particular handler method.

Step 2 : Create the JSP.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Welcome to Spring MVC</title>
</head>
<body>
        Hello everyone! Welcome to Spring MVC: Example 1
</body>
</html>

Step 3 : Now create the war file and deploy into your Tomcat Server. (Any Web Server or application Server).

Step 4 : After the application is deployed go the below link:

http://server-name:portNumber/spring-mvc-samples/example1/welcome.htm

Step 5 : You will be seeing the following output on your screen:

Welcome to Spring MVCHello everyone! Welcome to Spring MVC: Example 1


Surprised!

Continue...
分享到:
评论

相关推荐

    Spring MVC Annotation验证的方法

    Spring MVC Annotation验证方法 Spring MVC 框架提供了多种验证方法,其中一种常用的方式是使用Annotation验证。本文将详细介绍 Spring MVC Annotation验证的方法,包括使用 Spring MVC 自带的 Annotation 验证和...

    Spring MVC 教程快速入门 深入分析

    十九、&lt;mvc:annotation-driven/&gt;到底做了什么工作:描述了&lt;mvc:annotation-driven /&gt;标签的作用,它自动注册了诸如ConversionService、ValidatingHandlerInterceptor等组件,并启用了特定注解的处理器映射。...

    Spring MVC入门教程

    个人认为相当适合入门和知识巩固!! 一、前言 二、spring mvc 核心类与接口 ...十九、 &lt;mvc:annotation-driven /&gt; 到底做了什么工作 二十、 本文中springMVC.xml配置文件是核心,这里给一个下载地址

    Spring MVC 教程 快速入门 深入分析

    Spring MVC 教程 快速入门 深入分析 目录 一、前言 二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 ...十九、 &lt;mvc:annotation-driven /&gt; 到底做了什么工作

    Spring MVC学习指南

    ### Spring MVC 学习指南详解 #### 一、Spring MVC 概述 Spring MVC 是 Spring 框架的一部分,主要用于构建基于 Model-View-Controller (MVC) 设计模式的 Web 应用程序。它提供了丰富的功能来简化 Web 开发过程,...

    Spring MVC IDEA版本DEMO

    **Spring MVC IDEA版本DEMO详解** 在现代Java Web开发中,Spring MVC框架是不可或缺的一部分,它为构建可扩展、模块化的Web应用程序提供了强大的支持。IntelliJ IDEA作为一款高效的Java集成开发环境,使得开发...

    Spring-MVC-3.0.rar_Java spring mvc_spring mvc_spring ppt

    1. **Annotation-based Configuration**:引入了注解配置,如@Controller、@RequestMapping等,使得配置更简洁、更直观。 2. **ModelAndView的替代**:可以使用@ModelAttribute注解直接在方法参数中注入模型数据,...

    spring mvc的简单单元测试

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.test.web.servlet.MockMvc; import ...

    IDEA 搭建Spring MVC环境示例二

    &lt;mvc:annotation-driven/&gt; ``` 5. **创建Controller**: - 在`com.example.springmvcexample.controller`包下创建一个新的Java类,如`HelloWorldController.java`。 - 在这个类中,使用`@Controller`注解标记它...

    spring mvc

    `&lt;mvc:annotation-driven/&gt;` 配置主要用于启用 Spring MVC 的注解驱动功能。这包括但不限于: - **自动绑定**:支持自动将 HTTP 请求参数绑定到控制器方法的参数上。 - **数据转换**:支持自动的数据类型转换。 - **...

    spring mvc 简单demo

    Spring MVC 是一个基于Java的轻量级Web应用框架,它为构建RESTful应用程序提供了强大的支持。这个简单的Demo将向我们展示如何使用Spring MVC创建一个基本的Web应用。在这个过程中,我们将涉及控制器、视图解析器、...

    spring mvc项目创建

    Spring MVC 是一个基于 Java 的轻量级 Web 开发框架,它是 Spring 框架的一部分,主要用于构建 MVC(Model-View-Controller)模式的 Web 应用程序。在本项目创建过程中,我们将深入探讨如何配置一个基本的 Spring ...

    Spring MVCSpring MVC基础.ppt

    Spring MVC 是一个轻量级的、基于Java的Web应用程序开发框架,它是Spring框架的一部分,主要用于构建Web应用的模型-视图-控制器(MVC)架构。Spring MVC的设计目标是简化开发,提高可测试性,并促进良好的编程实践。...

    Spring MVC异步模式

    import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Controller; @Controller public class TestController { @Async public void asyncTask() { // 耗时操作 ...

    Spring MVC 基础实例,能明白SpringMVC是怎么回事

    Spring MVC 是一个强大的Java Web开发框架,用于构建可维护、高性能和灵活的Web应用程序。它作为Spring框架的一部分,提供了一种模型-视图-控制器(MVC)架构,简化了处理HTTP请求和响应的复杂性。在这个基础实例中...

    Spring MVC REST Demo

    Spring MVC 是一个强大的Java框架,用于构建Web应用程序,而REST(Representational State Transfer)是一种软件架构风格,常用于创建Web服务。"Spring MVC REST Demo"是一个示例项目,旨在展示如何在Spring MVC框架...

    spring mvc(整合了json)

    &lt;bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"&gt; &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /&gt; ...

    Spring MVC整合Mybatis

    - **Spring MVC配置**:配置`&lt;mvc:annotation-driven&gt;`启用注解驱动,配置视图解析器如`InternalResourceViewResolver`。 **5. 实例化Mapper** 在Service层,可以通过@Autowired注解自动注入Mapper接口,例如: ```...

    最新maven搭建的Spring mvc的demo

    &lt;mvc:annotation-driven /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; ``` 4. **编写Controller**:使用Spring MVC的@Controller注解创建控制器类,通过@...

Global site tag (gtag.js) - Google Analytics