16
votes

I have problem with IntelliJ 2016.1.3 and Spring Web MVC integration. Steps I've made:

  1. File -> New -> Project... -> Maven (no archetype)
  2. GroupId = test ArtifactId = app
  3. Project name = App and Finish.
  4. I added to pom.xml < packaging > war < /packaging >
  5. I added to pom.xml dependencies

    &ltdependency&gt
            &ltgroupId&gtorg.springframework&lt/groupId&gt
            &ltartifactId&gtspring-webmvc&lt/artifactId&gt
            &ltversion&gt4.1.6.RELEASE&lt/version&gt
    &lt/dependency&gt
    &ltdependency&gt
            &ltgroupId&gtjavax.servlet&lt/groupId&gt
            &ltartifactId&gtjstl&lt/artifactId&gt
            &ltversion&gt1.2&lt/version&gt
    &lt/dependency&gt
    &ltdependency&gt
            &ltgroupId&gtjavax.servlet&lt/groupId&gt
            &ltartifactId&gtjavax.servlet-api&lt/artifactId&gt
            &ltversion&gt3.1.0&lt/version&gt
            &ltscope&gtprovided&lt/scope&gt
    &lt/dependency&gt
    
  6. Next I added modules into project (right click on project name -> Add Framework Support... ). I selected Spring MVC and Download (Configure... - selected all items).

  7. I created controller class HomeController.class

    package test.app;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HomeController {
        @RequestMapping(value="/")
        public String test()
        {
            return "test";
        }
    }
    
  8. I created webapp\WEB-INF and put there web.xml

    &ltweb-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                                     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt
        &ltservlet&gt
            &ltservlet-name&gtWebServlet&lt/servlet-name&gt
            &ltservlet-class&gtorg.springframework.web.servlet.DispatcherServlet&lt/servlet-class&gt
            &ltinit-param&gt
                &ltparam-name&gtcontextConfigLocation&lt/param-name&gt
                &ltparam-value&gt/WEB-INF/dispatcher-servlet.xml&lt/param-value&gt
            &lt/init-param&gt
        &lt/servlet&gt
    
    
    &ltservlet-mapping&gt
        &ltservlet-name&gtWebServlet&lt/servlet-name&gt
        &lturl-pattern&gt/&lt/url-pattern&gt
    &lt/servlet-mapping&gt
    
    &lt/web-app&gt
  9. Into webapp\WEB-INF I put dispatcher-servlet.xml

    &lt?xml version="1.0" encoding="UTF-8"?&gt
    &ltbeans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        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-4.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"&gt
    
    
    
    &ltmvc:annotation-driven /&gt
    &ltcontext:component-scan base-package="test.app" /&gt
    
    &ltbean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt
        &ltproperty name="prefix" value="/WEB-INF/views/" /&gt
        &ltproperty name="suffix" value=".jsp" /&gt
    &lt/bean&gt
    
    &lt/beans&gt
  10. Finally I added test.jsp file into webapp\WEB-INF\views. In addition I had to add module dependency (F4 -> modules -> dependencies -> + -> library -> from maven -> typed javax.servlet:jstl:1.2)

  11. Next step should be run application. I had to edit configurations (down arrow next to green arrow) -> + -> TomcatServer -> Local and I got warning No artifacts marked for deployment. Unfortunatelly I can't fix this problem. I have Fix button but after I press this I get Deployment tab and don't what to do.

Please help me with deployment configuration and tell me is my way of creating spring web application in IntelliJ good or have you got another better way. I need step by step tutorial because I watched some movies on youtube and I saw options I haven't in my Intellij or they are hidden and I can't find them. Best regards

3

3 Answers

5
votes

If you have configured everything the right way, you should have a +-Sign at the upper right of your deployment-tab. After pressing it, you should be offered a tooltip with 1-2 options:

  • Artifact...
  • External Source...

You usually would select the deployment artifact of your current project by choosing "Artifact...".

HTH

2
votes

In step 11. when you receive the warning then

  1. Goto the Deployment tab Press the '+' button.
  2. context menu with options 'Artifact...' and 'External Source..' is displayed.
  3. select 'Artifact...' and a dialog will be displayed with two option 'project_name:war' and 'project_name:war exploded' is displayed.
  4. Select 'project_name:war exploded' option, the warning will be resolved.
2
votes

There is complete step-by-step tutorial how to create spring web mvc project in IntelliJ.

  1. File -> New -> Project -> Maven (uncheck 'Create from archetype') -> Next.
  2. Type your GroupId and ArtifactId. For example GroupId = 'test', ArtifactId = 'app' and click Next.
  3. Type Project name. For example Project name = 'WebApp' and click Finish.
  4. Right click on your project name and choose 'Add Framework Support...'. Then select 'Spring MVC' and 'Download' option. Click OK.
  5. In pom.xml file add new dependency and packaging property like in code below

    &lt?xml version="1.0" encoding="UTF-8"?&gt
    &ltproject 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/xsd/maven-4.0.0.xsd"&gt
        &ltmodelVersion&gt4.0.0&lt/modelVersion&gt
    
    
    &ltgroupId&gttest&lt/groupId&gt
    &ltartifactId&gtapp&lt/artifactId&gt
    &ltversion&gt1.0-SNAPSHOT&lt/version&gt
    &ltpackaging&gtwar&lt/packaging&gt
    
    &ltdependencies&gt
        &ltdependency&gt
            &ltgroupId&gtorg.springframework&lt/groupId&gt
            &ltartifactId&gtspring-webmvc&lt/artifactId&gt
            &ltversion&gt4.2.5.RELEASE&lt/version&gt
        &lt/dependency&gt
    &lt/dependencies&gt
    
    &lt/project&gt
  6. In upper right corner of IntelliJ window you will see information panel 'Maven projects need to be imported'. Click 'Import changes'.

  7. In src/main/java create new package, for example 'test.app' and place there new java file TestController.java with your controller (code below).

    package test.app;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    
    @Controller
    public class TestController {
        @RequestMapping(value="/")
        public String test()
        {
            return "index";
        }
    }
    
  8. In web/WEB-INF/dispatcher-servlet.xml file paste code below

    &lt?xml version="1.0" encoding="UTF-8"?&gt
    &ltbeans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           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-4.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"&gt
    
    
    
    &ltmvc:annotation-driven /&gt
    &ltcontext:component-scan base-package="test.app" /&gt
    
    &ltbean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt
        &ltproperty name="prefix" value="/WEB-INF/views/" /&gt
        &ltproperty name="suffix" value=".jsp" /&gt
    &lt/bean&gt
    
    &lt/beans&gt
  9. In web/WEB-INF directory create new directory 'views' and move there file index.jsp from web directory.

  10. In file index.jsp paste some html code into body section. For example index.jsp file code is placed below

    &lt%@ page contentType="text/html;charset=UTF-8" language="java" %&gt
    &lthtml&gt
      &lthead&gt
        &lttitle&gt$Title$&lt/title&gt
      &lt/head&gt
      &ltbody&gt
        &ltp&gtHELLO WORLD&lt/p&gt
      &lt/body&gt
    &lt/html&gt
    
  11. In web.xml file change url-pattern property value from *.form into /. Now web.xml file should contains code like below.

    &lt?xml version="1.0" encoding="UTF-8"?&gt
    &ltweb-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1"&gt
        &ltcontext-param&gt
            &ltparam-name&gtcontextConfigLocation&lt/param-name&gt
            &ltparam-value&gt/WEB-INF/applicationContext.xml&lt/param-value&gt
        &lt/context-param&gt
        &ltlistener&gt
            &ltlistener-class&gtorg.springframework.web.context.ContextLoaderListener&lt/listener-class&gt
        &lt/listener&gt
        &ltservlet&gt
            &ltservlet-name&gtdispatcher&lt/servlet-name&gt
            &ltservlet-class&gtorg.springframework.web.servlet.DispatcherServlet&lt/servlet-class&gt
            &ltload-on-startup&gt1&lt/load-on-startup&gt
        &lt/servlet&gt
        &ltservlet-mapping&gt
            &ltservlet-name&gtdispatcher&lt/servlet-name&gt
            &lturl-pattern&gt/&lt/url-pattern&gt
        &lt/servlet-mapping&gt
    &lt/web-app&gt
    
  12. Now right click your project name and select 'Open Module Settings...'. Choose Modules -> your application name -> Web and change in WebResourceDirectory window your web resource directory into directory\WebApp\web where directory is location of your IntelliJ project on your computer. Then click Apply and OK.

  13. Now we must edit run configurations. Press down arrow next to green arrow and select Edit Configurations... Then cilck green +. Choose Tomcat Server -> Local. In 'Application Server' section choose your main Tomcat directory. You will see Warning in bottom part of the window. Click Fix and select 'WebApp:war eploded' or something similar. It depends on your application name. Then click Apply and OK.

That's all :) Now you can press green arrow and see your first web application site in your favourite web browser. Good luck!