Intellij14搭建Maven的SpringMVC记录

IDEA这玩意儿真是人少坑多,搭一个极简的SpringMVC又花了大半天时间
参考文章来一发先:真是最精简的内容了,这里先只介绍springMVC返回Json数据的搭建,mybatis和spring的整合在下一篇博客中给出。
在Intellij中创建项目的步骤是这样的:

  1. 创建一个空的Maven项目
  2. pom.xml中加入依赖,Intellij会提示打开自动下载依赖的,下载依赖。

        <properties>
            <org.springframework-version>4.2.2.RELEASEorg.springframework-version>
            <jackson.version>2.6.3jackson.version>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-coreartifactId>
                <version>${org.springframework-version}version>
            dependency>
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webartifactId>
                <version>${org.springframework-version}version>
            dependency>
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-webmvcartifactId>
                <version>${org.springframework-version}version>
            dependency>
    
    
            
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-coreartifactId>
                <version>${jackson.version}version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>${jackson.version}version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-annotationsartifactId>
                <version>${jackson.version}version>
            dependency>
        dependencies>
    
    project>
  3. 这时作为一个空的Maven项目,还不能部署到服务器上,在项目上右键,选择第二项:添加框架支持(我打了一个中文补丁,如果是英文版看意思对就行),选择第一项Web Application
    Intellij14搭建Maven的SpringMVC记录_第1张图片
    这时Intellij会自动帮你设置好 FacetsArtificts
    Intellij14搭建Maven的SpringMVC记录_第2张图片
    Intellij14搭建Maven的SpringMVC记录_第3张图片
    src同级目录下会出现web文件夹,此时可以作为服务器程序部署到Tomcat上了
    Intellij14搭建Maven的SpringMVC记录_第4张图片
  4. 下载Tomcat,配置到Intellij中,把项目放入Tomcat
    Intellij14搭建Maven的SpringMVC记录_第5张图片
    添加Tomcat
    Intellij14搭建Maven的SpringMVC记录_第6张图片
    添加Artifact
    Intellij14搭建Maven的SpringMVC记录_第7张图片
    正常是这样的,最上面的name是服务器名,可以自己随便取,无影响
    Intellij14搭建Maven的SpringMVC记录_第8张图片
  5. 此时配置的内容大致已经完成了,在web/WEB-INF目录的web.xml文件中添加配置

     <web-app id="WebApp_ID" version="2.4"
             xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
        <display-name>Spring Web MVC Applicationdisplay-name>
    
    
        <servlet>
            <servlet-name>mvc-dispatcherservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <load-on-startup>1load-on-startup>
        servlet>
    
        <servlet-mapping>
            <servlet-name>mvc-dispatcherservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
        <context-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xmlparam-value>
        context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
        listener>
    
    web-app>
  6. 在相同目录下创建mvc-dispatcher-servlet.xml文件,添加配置,这里的配置和上面给的参考链接的稍有不同,包括pom.xml也是的,可能因为它的spring版本较低,我用了spirng4的,导致按照他的配置不成功,要返回Json的话还是需要添加我这里的配置的。

     <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans     
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        
        <context:component-scan base-package="xxx.xxx.controller" />
    
        <mvc:annotation-driven />
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="supportedMediaTypes" value="application/json;charset=UTF-8"/>
        bean>
    
    beans>
  7. 在src目录下创建xxx.xxx.controller,要和上面的配置文件中对应
  8. 创建HelloController.java内容如下

     package com.gcoreinc.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping("/home")
    public class HelloController {
    
        @RequestMapping("/hello")
        @ResponseBody
        public String sayHello(){
            return "Hello World!";
        }
    }
  9. 至此要写的内容也完成了,运行tomcat,然后发现启动服务器报错,这是因为没有把Maven依赖作为web项目依赖,如下操作,这个坑太深了,以至于我掉下去之后花了好几个小时才爬出来,以此铭记
    Intellij14搭建Maven的SpringMVC记录_第9张图片
    新建文件夹,取名为lib
    Intellij14搭建Maven的SpringMVC记录_第10张图片
    Intellij14搭建Maven的SpringMVC记录_第11张图片
    选中lib文件夹,把所有的maven依赖加进去
    Intellij14搭建Maven的SpringMVC记录_第12张图片
    Intellij14搭建Maven的SpringMVC记录_第13张图片

  10. 大功告成,此时可以正常启动服务器,打开路径:
    http://localhost:8080/TestSpringMVC/home/hello
    就能看到最最亲切的内容了[“Hello”,”World”,”!”]
    最后记住!每次修改pom.xml下的依赖后,记得要更新Artifact下的lib中的依赖!(似乎Intellij14.1版本可以自动更新了,我没有尝试过)

你可能感兴趣的:(Java,spring,mybatis)