IntelliJ + Spring Web MVC
I have problem with IntelliJ 2016.1.3 and Spring Web MVC integration. Steps I've made:
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 webappWEB-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 webappWEB-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 webappWEB-INFviews. In addition I had to add module dependency (F4 -> modules -> dependencies -> + -> library -> from maven -> typed javax.servlet:jstl:1.2)
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
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:
You usually would select the deployment artifact of your current project by choosing "Artifact...".
HTH
In step 11. when you receive the warning then
There is complete step-by-step tutorial how to create spring web mvc project in IntelliJ.
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 directoryWebAppweb where directory is location of your IntelliJ project on your computer. 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!
链接地址: http://www.djcxy.com/p/39072.html上一篇: 春季Web流程无法解析服务bean?