IntelliJ + Spring Web MVC

kmm*_*mmm 14 java spring spring-mvc intellij-idea maven

我有IntelliJ 2016.1.3和Spring Web MVC集成的问题.我做的步骤:

  1. 文件 - >新建 - >项目... - > Maven(无原型)
  2. GroupId = test ArtifactId = app
  3. 项目名称=应用程序和完成.
  4. 我添加到pom.xml <packaging> war </ packaging>
  5. 我添加到pom.xml依赖项

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
    </dependency>
    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
    </dependency>
    
  6. 接下来,我将模块添加到项目中(右键单击项目名称 - >添加框架支持...).我选择了Spring MVC和Download(配置... - 选中所有项目).

  7. 我创建了控制器类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. 我创建了webapp\WEB-INF并将web.xml放在那里

    <web-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">
        <servlet>
            <servlet-name>WebServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
            </init-param>
        </servlet>
    
    
    &ltservlet-mapping&gt
        &ltservlet-name&gtWebServlet&lt/servlet-name&gt
        &lturl-pattern&gt/&lt/url-pattern&gt
    &lt/servlet-mapping&gt
    
    Run Code Online (Sandbox Code Playgroud) </ web的应用>
  9. 进入webapp\WEB-INF我把dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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"
        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">
    
    
    
    &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
    
    Run Code Online (Sandbox Code Playgroud) </豆>
  10. 最后,我将test.jsp文件添加到webapp\WEB-INF\views中.另外我不得不添加模块依赖(F4 - > modules - > dependencies - > + - > library - > from maven - > typed javax.servlet:jstl:1.2)

  11. 下一步应该运行应用程序.我不得不编辑配置(绿色箭头旁边的向下箭头) - > + - > TomcatServer - > Local我收到警告没有标记为部署的工件.不幸的是,我无法解决这个问题.我有修复按钮,但在我按下这个后,我得到部署选项卡,不要做什么.

请帮我部署配置并告诉我是在IntelliJ中创建spring web应用程序的方法还是你有另一种更好的方法.我需要一步一步的教程,因为我在youtube上观看了一些电影,我看到了我在Intellij中没有的选项,或者它们被隐藏了,我找不到它们.最好的祝福

小智 5

如果您以正确的方式配置了所有内容,则部署选项卡的右上角应该有一个 + 号。按下后,您应该会看到一个带有 1-2 个选项的工具提示:

  • 神器...
  • 外部源...

您通常会通过选择“工件...”来选择当前项目的部署工件。

HTH