初次接触spring MVC项目,通过一段时间的学习,本文介绍一种以纯注解的方法去配置spring MVC环境,让那些配置的.xml文件统统见鬼吧。

什么是Spring MVC

  Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1,Struts2等。

版本

本人使用64位的eclipse,版本是eclipse-jee-neon-R-win32-x86_64,下载地址:http://pan.baidu.com/s/1cfETtk;若有32位系统的同学请自行去官网下载。

其他的软件参数:

Tomcat: 8.0.44(下载地址:http://pan.baidu.com/s/1skF1PrJ)

JDK:1.8.0_60(下载地址:http://pan.baidu.com/s/1eSmYoRg)

Maven:3.5.0(下载地址:http://pan.baidu.com/s/1mhItVHQ)

Spring-framework: 4.0.4.RELEASE

可自行百度安装教程。

1)新建项目:

file-new-other后,在Maven中选择Maven Project,如下图所示,

点Next,如下图所示

在这里Use default Workspace  location可能不同,不用管,直接点Next,如下图:

选择maven-archetype-webapp 1.0,点Next进入输入项目名的步骤。如下图

其中Actifact Id是项目名。点Finish完成项目创建。

项目建好之后,目录结构如下:

注:此时可能会提示index.jsp文件报错,此时右击项目名选择-Build Path-Configure Build Path,在Libraries下选择Add Library-Server Runtime后,选择安装的tomcat版本。

2)导入jar包

我们基于Spring mvc框架进行开发,需要依赖一下的spring jar包:

  • spring-aop-4.2.5.RELEASE.jar
  • spring-beans-4.2.5.RELEASE.jar
  • spring-context-4.2.5.RELEASE.jar
  • spring-core-4.2.5.RELEASE.jar
  • spring-expression-4.2.5.RELEASE.jar
  • spring-web-4.2.5.RELEASE.jar
  • spring-webmvc-4.2.5.RELEASE.jar
  • commons-logging-1.1.1.jar(用来打印log)

此时打开pom.xml文件导入上述的jar包

pom.xml文件的内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wj.test</groupId>
<artifactId>springmvc</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<org.springframework.version>4.0.4.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!--Spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency> </dependencies>
<build>
<finalName>springmvc</finalName>
</build>
</project>

这个时候,就该进行Spring配置了。按传统方式来的话,首先要去web.xml写一堆配置,然后建立个管理beab的Beans.xml,管理spring mvc 的xml,再写一坨一坨Bean。就是先进一点的(也就是很多人说的0配置),也就是自己的业务Bean不用写进xml了,还是很麻烦。

而我这里讲的方式,则是完全不修改任何web.xml代码,不写一行XML代码的方式。

3)配置文件及编写代码

首先,在项目立建立一个

SpringMvcConfig.java文件:
package com.wj.test.cfg;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
  * 取代Beans.xml,纯注解配置各种BEAN
  * @author wj
  *
  */

@Configuration
@EnableWebMvc
@ComponentScan(
value = "com.wj.test", includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)
}) public class SpringMvcConfig extends WebMvcConfigurerAdapter
{
@Bean
public ViewResolver viewResolver()
{
//InternalResourceViewResolver;配置视图解析器
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setOrder(1);
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".jsp");
return resolver;
}
}

@Configuration注解就是告诉Spring这个是一个配置文件类,这里配置的Bean要交给Spring去管理。这个就是用来取代Beans.xml这种文件的。

@ComponentScan("com.csonezp")这个注解就是配置包扫描用的,不必多说了

@EnableWebMvc ,启用Spring MVC支持

这里面配置了一个Bean就是JSP的视图解析器。这可以对应到XML文件里面去的。找一个传统Spring项目,用xml文件对比着我这个类去看,会对这种配置方式有更深的了解。

下面要建立一个WebInitializer类继承WebApplicationInitializer,在这里添加一个servlet。这一步是用来取代在web.xml中添加servlet的步骤

package com.wj.test.cfg;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

/**
  * 取代在web.xml中添加servlet的步骤
  * @author wj
  *
  */

public class WebAppInitializer implements WebApplicationInitializer
{ public void onStartup(final ServletContext sc) throws ServletException { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(SpringMvcConfig.class);
rootContext.setServletContext(sc); DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext); ServletRegistration.Dynamic dynamic = sc.addServlet("dispatcherServlet", dispatcherServlet);
dynamic.setLoadOnStartup(1);
dynamic.addMapping("/");
}
}

写到这里就已经完成了,现在写一个测试的代码试试。

UserDao类:

package com.wj.test.mod;

public class UserDao
{
private String name;
private String hello;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getHello()
{
return hello;
}
public void setHello(String hello)
{
this.hello = hello;
}
}

Controller类:

package com.wj.test.web;

import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.wj.test.mod.UserDao; /**
* Created by wj
*/
@RestController
public class TestController
{
@RequestMapping("/test")
public UserDao getToken(HttpServletRequest request)
{
UserDao user = new UserDao();
user.setName(request.getParameter("name"));
user.setHello("How are you?");
return user; }
}

在这还需要导入三个关于json的.jar包,整个项目所需要的.jar包如下:

运行项目,在浏览器中输入http://localhost:8080/springmvc/test?name=God,最后的运行结果如下:

整个项目的结构图如下:

本文到此结束。

maven 纯注解一步一步搭建Spring Mvc项目(入门)的更多相关文章

  1. 搭建spring mvc项目

    在之前搭建maven项目这篇的基础上继续集成,引入spring mvc支持 一.添加jar包引用 修改pom.xml文件,加入:(其他关联的jar包maven会自动引用) <!-- 项目属性 - ...

  2. 使用IDEA,Eclispe搭建Spring Boot项目

    如何创建一个Spring Boot项目?这里使用maven来进行依赖管理,根据常用的IDE,可以使用IDEA.Eclipse.或者访问官方网站搭建. 项目搭建环境准备 JDK:1.8 MAVEN:3. ...

  3. IDEA 创建Spring MVC项目搭建

    概述 IntelliJ IDEA是一款更加集成智能的开发工具,相对Myeclipse开发而言,使用起来相对更加的方便:初步手动使用IDEA搭建Spring MVC项目,现将操作流程整理记录如下. 环境 ...

  4. Myeclipse下使用Maven搭建spring boot项目

    开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...

  5. 基于 intellij IDEA 快速搭建Spring Boot项目

           在<一步步搭建 Spring Boot maven 框架的工程>一文中,已经介绍了如何使用Eclipse快速搭建Spring Boot项目.由于最近将开发工具由Eclipse ...

  6. 从零开始学 Java - 搭建 Spring MVC 框架

    没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...

  7. 使用JavaConfig和注解方式实现零xml配置的Spring MVC项目

    1. 引言 Spring MVC是Spring框架重要组成部分,是一款非常优秀的Web框架.Spring MVC以DispatcherServlet为核心,通过可配置化的方式去处理各种web请求. 在 ...

  8. 零基础搭建 spring mvc 4 项目(本文基于 Servlet 3.0)

    作者各必备工具的版本如下: Tomcat:apache-tomcat-7.0.63 (下载链接) Java EE - Eclipse:Luna Service Release 1 v4.4.1 (下载 ...

  9. Spring Boot入门(一):搭建Spring Boot项目

    从本篇博客开始,我们开始进入Spring Boot的世界,它的出现使Spring的开发变得更加简洁,因此一经推出受到众多程序员的喜爱. 作为Spring Boot系列的第一篇博客,我们先来讲解下如何搭 ...

随机推荐

  1. CodeForcesGym 100753E Change of Scenery

    Change of Scenery Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...

  2. POJ 1811 大整数素数判断 Miller_Rabin

    #include <cstdio> #include <cstring> #include <cmath> #include <ctime> #incl ...

  3. noip模拟赛 运

    [问题背景]zhx 和妹子们玩数数游戏.[问题描述]仅包含 4 或 7 的数被称为幸运数.一个序列的子序列被定义为从序列中删去若干个数, 剩下的数组成的新序列.两个子序列被定义为不同的当且仅当其中的元 ...

  4. kafka 在阿里云部署

    https://blog.csdn.net/chenyulancn/article/details/79499401 https://www.cnblogs.com/yangtianle/p/8761 ...

  5. 源码分析-react3-创建dom

    React.createElement class Welcome extends React.Component { constructor(){ super() this.state={ test ...

  6. OSX: node中安装zeromq

    1. brew install pkg-config2. brew install zmq3. export PKG_CONFIG_PATH="/usr/local/lib/pkgconfi ...

  7. CI session 类的用法

    最近使用codeingiter框架,发现默认的session 不是很好用,以下是用法总结:使用的是2.0.2的版本 1.扩展自带的session类:application/libraries/MY_s ...

  8. JConsole使用手冊具体解释

    一篇Sun项目主页上介绍JConsole使用的文章,前段时间性能測试的时候大概翻译了一下以便学习,今天整理一下发上来.有些地方也不知道怎么翻,就保留了原文,可能还好理解点.呵呵,水平有限,翻的不好,大 ...

  9. [Vue @Component] Pass Props Between Components with Vue Slot Scope & renderless component

    Components with slots can expose their data by passing it into the slot and exposing the data using  ...

  10. 输入两个整数n 和m,从数列1,2,3.......n 中任意取几个数, 使其和等于m ,要求将当中全部的可能组合列出来

    中兴面试题之中的一个.难度系数中. 题目描写叙述例如以下:输入两个整数n 和m,从数列1,2.3.......n 中任意取几个数, 使其和等于m ,要求将当中全部的可能组合列出来. 逻辑分析: 1.比 ...