【发布时间】:2016-10-25 12:38:12
【问题描述】:
IntelliJ 2016.1.3 和 Spring Web MVC 集成有问题。 我已经采取的步骤:
- 文件 -> 新建 -> 项目... -> Maven(无原型)
- GroupId = 测试 ArtifactId = 应用程序
- 项目名称 = 应用并完成。
- 我在pom.xml中添加了
war -
我添加到 pom.xml 依赖项
<依赖> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </依赖> <依赖> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <版本>1.2</版本> </依赖> <依赖> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <版本>3.1.0</版本> <scope>提供</scope> </依赖> 接下来我将模块添加到项目中(右键单击项目名称 -> 添加框架支持...)。我选择了 Spring MVC 并下载(配置... - 选择了所有项目)。
-
我创建了控制器类 HomeController.class
包test.app; 导入 org.springframework.stereotype.Controller; 导入 org.springframework.web.bind.annotation.RequestMapping; @控制器 公共类 HomeController { @RequestMapping(value="/") 公共字符串测试() { 返回“测试”; } } -
我创建了 webapp\WEB-INF 并将 web.xml 放在那里
<web-app 版本="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 名称>WebServlet</servlet 名称> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init 参数> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </初始化参数> </servlet>
</网络应用><servlet-mapping> <servlet-name>WebServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> -
进入 webapp\WEB-INF 我将 dispatcher-servlet.xml 放入
<?xml 版本="1.0" 编码="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">
</豆子><mvc:annotation-driven /> <context:component-scan base-package="test.app" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> 最后我将 test.jsp 文件添加到 webapp\WEB-INF\views 中。另外我必须添加模块依赖项(F4 -> 模块 -> 依赖项 -> + -> 库 -> 来自 maven -> 键入 javax.servlet:jstl:1.2)
- 下一步应该是运行应用程序。我必须编辑配置(绿色箭头旁边的向下箭头)-> + -> TomcatServer -> 本地,我收到警告没有标记为部署的工件。不幸的是我无法解决这个问题。我有“修复”按钮,但按下此按钮后,我得到了“部署”选项卡,但不知道该做什么。
请帮助我进行部署配置,并告诉我我在 IntelliJ 中创建 Spring Web 应用程序的方式好还是你有另一种更好的方式。我需要一步一步的教程,因为我在 youtube 上看了一些电影,我看到了我的 Intellij 中没有的选项,或者它们被隐藏了,我找不到它们。 最好的问候
【问题讨论】:
标签: java spring maven spring-mvc intellij-idea