java IntelliJ + Spring Web MVC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37987193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ + Spring Web MVC
提问by kmmm
I have problem with IntelliJ 2016.1.3 and Spring Web MVC integration. Steps I've made:
我对 IntelliJ 2016.1.3 和 Spring Web MVC 集成有问题。我所做的步骤:
- File -> New -> Project... -> Maven (no archetype)
- GroupId = test ArtifactId = app
- Project name = App and Finish.
- I added to pom.xml < packaging > war < /packaging >
I added to pom.xml dependencies
<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>
Next I added modules into project (right click on project name -> Add Framework Support... ). I selected Spring MVC and Download (Configure... - selected all items).
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"; } }
I created webapp\WEB-INF and put there 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>
</web-app><servlet-mapping> <servlet-name>WebServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Into webapp\WEB-INF I put 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">
</beans><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>
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)
- 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.
- 文件 -> 新建 -> 项目... -> Maven(无原型)
- GroupId = 测试 ArtifactId = 应用
- 项目名称 = 应用程序和完成。
- 我在pom.xml中加入<package>war</packaging>
我添加到 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>
接下来我将模块添加到项目中(右键单击项目名称 -> 添加框架支持...)。我选择了 Spring MVC 和下载(配置... - 选择了所有项目)。
我创建了控制器类 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"; } }
我创建了 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>
</web-app><servlet-mapping> <servlet-name>WebServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
进入 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">
</beans><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 -> 本地,我收到警告没有标记部署的工件。不幸的是,我无法解决这个问题。我有“修复”按钮,但在我按下此按钮后,我会看到“部署”选项卡,但不知道该做什么。
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
请帮助我进行部署配置,并告诉我是我在 IntelliJ 中创建 Spring Web 应用程序的好方法还是您有其他更好的方法。我需要一步一步的教程,因为我在 youtube 上看了一些电影,我看到了我的 Intellij 中没有的选项,或者它们被隐藏了,我找不到它们。最好的祝福
采纳答案by Florian Stendel
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:
如果您以正确的方式配置了所有内容,您应该在部署选项卡的右上角有一个 + 号。按下后,您应该会看到一个带有 1-2 个选项的工具提示:
- Artifact...
- External Source...
- 神器...
- 外部源...
You usually would select the deployment artifact of your current project by choosing "Artifact...".
您通常会通过选择“工件...”来选择当前项目的部署工件。
HTH
HTH
回答by Asif Khokhar
In step 11. when you receive the warning then
在步骤 11. 当您收到警告时
- Goto the Deployment tab Press the '+' button.
- context menu with options 'Artifact...' and 'External Source..' is displayed.
- select 'Artifact...' and a dialog will be displayed with two option 'project_name:war' and 'project_name:war exploded' is displayed.
- Select 'project_name:war exploded' option, the warning will be resolved.
- 转到部署选项卡按“+”按钮。
- 显示带有选项“工件...”和“外部源...”的上下文菜单。
- 选择“Artifact...”,将显示一个对话框,其中包含两个选项“project_name:war”和“project_name:war purged”。
- 选择'project_name:war purged' 选项,警告将被解决。
回答by kmmm
There is complete step-by-step tutorial how to create spring web mvc project in IntelliJ.
有完整的分步教程如何在 IntelliJ 中创建 spring web mvc 项目。
- File -> New -> Project -> Maven (uncheck 'Create from archetype') -> Next.
- Type your GroupId and ArtifactId. For example GroupId = 'test', ArtifactId = 'app' and click Next.
- Type Project name. For example Project name = 'WebApp' and click Finish.
- Right click on your project name and choose 'Add Framework Support...'. Then select 'Spring MVC' and 'Download' option. Click OK.
In pom.xml file add new dependency and packaging property like in code below
<?xml version="1.0" encoding="UTF-8"?> <project 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"> <modelVersion>4.0.0</modelVersion>
</project><groupId>test</groupId> <artifactId>app</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.5.RELEASE</version> </dependency> </dependencies>
In upper right corner of IntelliJ window you will see information panel 'Maven projects need to be imported'. Click 'Import changes'.
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"; } }
In web/WEB-INF/dispatcher-servlet.xml file paste code below
<?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">
</beans><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>
In web/WEB-INF directory create new directory 'views' and move there file index.jsp from web directory.
In file index.jsp paste some html code into body section. For example index.jsp file code is placed below
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <p>HELLO WORLD</p> </body> </html>
In web.xml file change url-pattern property value from *.form into /. Now web.xml file should contains code like below.
<?xml version="1.0" encoding="UTF-8"?> <web-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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
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.
- 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.
- 文件 -> 新建 -> 项目 -> Maven(取消选中“从原型创建”)-> 下一步。
- 输入您的 GroupId 和 ArtifactId。例如 GroupId = 'test', ArtifactId = 'app' 并单击 Next。
- 输入项目名称。例如,项目名称 = 'WebApp' 并单击完成。
- 右键单击您的项目名称并选择“添加框架支持...”。然后选择“Spring MVC”和“下载”选项。单击确定。
在 pom.xml 文件中添加新的依赖项和打包属性,如下面的代码所示
<?xml version="1.0" encoding="UTF-8"?> <project 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"> <modelVersion>4.0.0</modelVersion>
</project><groupId>test</groupId> <artifactId>app</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.5.RELEASE</version> </dependency> </dependencies>
在 IntelliJ 窗口的右上角,您将看到信息面板“需要导入 Maven 项目”。单击“导入更改”。
在 src/main/java 中创建新包,例如“test.app”并将新的 java 文件 TestController.java 与您的控制器(下面的代码)放在一起。
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"; } }
在 web/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">
</beans><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>
在 web/WEB-INF 目录中创建新目录 'views' 并将文件 index.jsp 从 web 目录移动到那里。
在文件 index.jsp 中,将一些 html 代码粘贴到正文部分。比如index.jsp文件代码放在下面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <p>HELLO WORLD</p> </body> </html>
在 web.xml 文件中,将 url-pattern 属性值从 *.form 更改为 /。现在 web.xml 文件应该包含如下代码。
<?xml version="1.0" encoding="UTF-8"?> <web-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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
现在右键单击您的项目名称并选择“打开模块设置...”。选择 Modules -> your application name -> Web 并在 WebResourceDirectory 窗口中将您的 Web 资源目录更改为 directory\WebApp\web,其中目录是您计算机上 IntelliJ 项目的位置。然后单击应用和确定。
- 现在我们必须编辑运行配置。按绿色箭头旁边的向下箭头并选择编辑配置...然后单击绿色 +。选择 Tomcat 服务器 -> 本地。在“应用程序服务器”部分选择您的主 Tomcat 目录。您将在窗口底部看到警告。单击“修复”并选择“WebApp:war eploded”或类似内容。这取决于您的应用程序名称。然后单击应用和确定。
That's all :) Now you can press green arrow and see your first web application site in your favourite web browser. Good luck!
这就是全部:) 现在您可以按绿色箭头并在您喜欢的 Web 浏览器中查看您的第一个 Web 应用程序站点。祝你好运!