Spring MVC annotation configuration

1. Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>springMvcDemo</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2. spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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">

    <mvc:annotation-driven />

    <context:component-scan base-package="com.jnl.springMvcDemo.controller" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

3. NavigationController.java

package com.jnl.springMvcDemo.controller;

  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.servlet.ModelAndView;

  @Controller
  public class NavigationController {
     @RequestMapping(value="/navigation",method=RequestMethod.GET)
     public ModelAndView GetNavigations(){
         ModelAndView mv = new ModelAndView();
         mv.setViewName("result");
         return mv;
     }
 }

4. Add a jsp file with name "result" in folder "views"

5. visit http://localhost:8080/springMvcDemo/navigation

(The highlighted words mean  value of RequestMapping in NavigationController.java)

Spring MVC annotation configuration

时间: 2024-08-07 22:58:10

Spring MVC annotation configuration的相关文章

Spring MVC xml configuration

1. Web.xml id="WebApp_ID" version="3.0"> <display-name>springMvcDemo</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name

Spring MVC 学习笔记 spring mvc Schema-based configuration

Spring mvc 目前支持5个tag,分别是 mvc:annotation-driven,mvc:interceptors,mvc:view-controller, mvc:resources和mvc:default-servlet-handlermvc:annotation-driven   注册 DefaultAnnotationHandlerMapping 和AnnotationMethodHandlerAdapter两个bean,及一系列缺省的messageconverter(需要c

[Spring MVC] - Annotation验证

使用Spring MVC的Annotation验证可以直接对view model的简单数据验证,注意,这里是简单的,如果model的数据验证需要有一些比较复杂的业务逻辑性在里头,只是使用annotation做验证是比较难的. 以下是使用Spring MVC自带的annotation验证,加上自定义的一个@Tel的annotation验证例子,此例子具有: 1.支持多语言(国际化) 2.对默认数据先进行转化,比如int.date类型如果传入空值时,会抛异常,默认给定值 先看配置: 1.web.xm

Spring MVC配置静态资源和资源包教程

1- 介绍 这篇教程文章是基于: Spring 4 MVC 2- 创建一个项目 File/New/Other.. 输入: Group ID: com.yiibai Artifact ID: SpringMVCResource Package: com.yiibai.springmvcresource 项目被创建以后如下: 不要担心有错误消息在项目被创建时.原因是,我们还没有声明 Servlet 库. 注意: Eclipse 4.4(Luna)在创建 Maven 项目结构时可能是有错误的.需要修复

Spring MVC文件上传教程

1- 介绍 这篇教程文章是基于 Spring MVC来实现文件的上传功能,这里主要是实现两个功能:1.上传单个文件并将其移动到对应的上传目录:2.一次上传多个文件并将它们存储在指定文件夹下,接下来我们一步步地实现. 2- 创建工程 File/New/Other.. 输入: Group ID: com.yiibai Artifact ID: SpringMVCFileUpload Package: com.yiibai.springmvcfileupload 这样,工程就被创建了,如下图所示: 不

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestPar

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable Pankaj July 4, 2014 Spring @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotat

Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable--转载

原文地址: @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.bind.annotation.RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be appl

Spring - MVC Framework 教程

Spring - MVC Framework Tutorial 太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. Spring - MVC Framework Tutorial Advertise

[转] Spring MVC sample application for downloading files

http://www.codejava.net/frameworks/spring/spring-mvc-sample-application-for-downloading-files n this article, we are going to show you how to implement file download functionality in a Spring MVC application. The solution is similar to the one descri