Intellij IDEA搭建maven+注解SpringMVC框架

本文主要介绍了简单搭建SpringMVC的框架,并未涉及一些原理的讲解,以后会陆续补全,希望可以和大家一起交流进步

本文用到的环境相关版本介绍

本文的tomcat会以maven插件的形式集成至maven

  • maven3.2.5
  • Intellij IDEA 15
  • jdk1.8
  • *Spring3.2.4
  • tomcat-maven-plugin2.1

一、用maven+Intellij IDEA创建web项目

1.首先新打开IDEA,新建一个project

Intellij IDEA搭建maven+注解SpringMVC框架_第1张图片

2.next之后填写一些名称,此处略掉,建完项目之后maven会下载一些依赖,完成之后工程目录如下图:

Intellij IDEA搭建maven+注解SpringMVC框架_第2张图片

二、添加jar包依赖

1.在maven仓库中添加一些jar包依赖

<dependencies>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>3.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>3.2.4.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-context-supportartifactId>
            <version>3.2.4.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aspectsartifactId>
            <version>3.2.4.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>3.2.4.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>3.2.4.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>3.2.4.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>3.2.4.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>3.2.4.RELEASEversion>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>3.2.4.RELEASEversion>
        dependency>
        

        <dependency>
            <groupId>org.apache.tomcatgroupId>
            <artifactId>tomcat-catalinaartifactId>
            <version>7.0.39version>
            <scope>providedscope>
        dependency>

        <dependency>
            <groupId>org.apache.tomcatgroupId>
            <artifactId>tomcat-coyoteartifactId>
            <version>7.0.39version>
            <scope>providedscope>
        dependency>

    dependencies>

2.将tomcat作为插件放到build节点中

<build>
        <finalName>testfinalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-war-pluginartifactId>
                <version>2.1.1version>
            plugin>
            <plugin>
                <groupId>org.apache.tomcat.mavengroupId>
                <artifactId>tomcat7-maven-pluginartifactId>
                <version>2.1version>
                <configuration>
                    <port>80port>
                    <path>/path>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.1version>
                <configuration>
                    <target>1.7target>
                    <source>1.7source>
                    <encoding>UTF-8encoding>
                    <compilerArguments>
                        <verbose/>
                        <bootclasspath>${java.home}\lib\rt.jarbootclasspath>
                    compilerArguments>
                configuration>
            plugin>
        plugins>
    build>

三、进行SpringMVC配置

1.首先在resource目录下新建一个spring的配置文件springmvc.xml,文件图片和内容如下

Intellij IDEA搭建maven+注解SpringMVC框架_第3张图片


<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">

    
    <context:component-scan base-package="com.myspring.test.controller">context:component-scan>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
beans>

2.在web.xml配置前端控制器,并加载springmvc.xml的配置文件,内容如下



<web-app>
    <display-name>Archetype Created Web Applicationdisplay-name>


    
    <servlet>
        <servlet-name>SpringMVCservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        
        
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
    servlet>

    <servlet-mapping>
        <servlet-name>SpringMVCservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>



web-app>

3.至此,文件配置完毕,下面我们用注解写一个方法测试一下,测试类放在springmvc.xml配置的扫描的包中,此处包名要与配置文件中扫描的包名一致
Intellij IDEA搭建maven+注解SpringMVC框架_第4张图片

测试类中的代码:

@Controller
public class TestController {
    @RequestMapping(value = "/admin")
    @ResponseBody
    public String testSpring(){
        return "My testSpring";
    }
}

在maven插件中启动tomcat
Intellij IDEA搭建maven+注解SpringMVC框架_第5张图片

在浏览器中访问方法上面的映射地址(@RequestMapping(value = “/admin”)),发现了我们返回的字符串!
Intellij IDEA搭建maven+注解SpringMVC框架_第6张图片

至此,用Intellij IDEA搭建maven+注解开发SpringMVC的简单框架已经构建完成!

你可能感兴趣的:(Spring)