IntelliJ 2016.1.3とSpring Web MVCの統合に問題があります。私が作った手順:
Pom.xml依存関係に追加しました
&ltdependency> &ltgroupId&gtorg.springframework </ groupId> &ltartifactId&gtspring-webmvc </ artifactId> &ltversion&gt4.1.6.RELEASE </ version> </ dependency> &ltdependency> &ltgroupId&gtjavax.servlet </ groupId> &ltartifactId&gtjstl </ artifactId> &ltversion&gt1.2 </ version> </ dependency> &ltdependency> &ltgroupId&gtjavax.servlet </ groupId> &ltartifactId&gtjavax.servlet-api </ artifactId> &ltversion&gt3.1.0 </ version> &ltscope&gtprovided </ scope> </ dependency>
次に、プロジェクトにモジュールを追加しました(プロジェクト名を右クリック-> Add Framework Support ...)。私は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を配置しました
&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 "> &ltservlet> &ltservlet-name&gtWebServlet </ servlet-name> &ltservlet-class&gtorg.springframework.web.servlet.DispatcherServlet </ servlet-class> &ltinit-param> &ltparam-name&gtcontextConfigLocation </ param-name> &ltparam-value> /WEB-INF/dispatcher-servlet.xml </ param-値> </ init-param> </ servlet>
<servlet-mapping>
<servlet-name>WebServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</ web-app>
Webapp\WEB-INFに、dispatcher-servlet.xmlを配置します
<?xml version = "1.0" encoding = "UTF-8"?> &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">
<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>
</ beans>
最後に、test.jspファイルをwebapp\WEB-INF\viewsに追加しました。さらに、モジュールの依存関係を追加する必要がありました(F4->モジュール->依存関係-> +->ライブラリ-> mavenから->型付きjavax.servlet:jstl:1.2)
デプロイメント構成を手伝ってください。IntelliJでSpring Webアプリケーションを作成するための私の方法であるか、別のより良い方法を教えてください。 YouTubeでいくつかの映画を見て、Intellijにないオプションを表示したり、オプションが非表示になっていて見つからないため、ステップバイステップチュートリアルが必要です。宜しくお願いします
すべてを正しく設定している場合は、展開タブの右上に+-記号が表示されているはずです。それを押すと、1〜2のオプションのツールチップが表示されます。
通常、「アーティファクト...」を選択して、現在のプロジェクトのデプロイメントアーティファクトを選択します。
HTH
手順11.で警告を受け取ったら
IntelliJでSpring Web MVCプロジェクトを作成するための完全なステップバイステップのチュートリアルがあります。
以下のコードのように、pom.xmlファイルに新しい依存関係とパッケージプロパティを追加します
<?xml version = "1.0" encoding = "UTF-8"?> &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"> &ltmodelVersion&gt4.0.0 </ modelVersion>
<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>
</ project>
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"?> &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">
<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>
</ beans>
Web/WEB-INFディレクトリに新しいビュー「views」を作成し、そこにファイルindex.jspをwebディレクトリから移動します。
ファイルindex.jspで、HTMLコードを本文セクションに貼り付けます。たとえば、index.jspファイルのコードは以下に配置されます
<%@ page contentType = "text/html; charset = UTF-8" language = "Java"%> &lthtml> &lthead> &lttitle> $ Title $ </ title> </ head> &ltbody> &ltp&gtHELLO WORLD </ p> </ body> </ html>
Web.xmlファイルで、url-patternプロパティの値を* .formから/に変更します。これで、web.xmlファイルに以下のようなコードが含まれるはずです。
<?xml version = "1.0" encoding = "UTF-8"?> &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"> &ltcontext-param> &ltparam-name&gtcontextConfigLocation </ param-name> &ltparam-value> /WEB-INF/applicationContext.xml </ param-value> </ context-param> &ltlistener> &ltlistener-class&gtorg.springframework.web.context.ContextLoaderListener </ listener-class> </ listener> &ltservlet> &ltservlet-name&gtdispatcher </ servlet-name> &ltservlet-class&gtorg.springframework.web.servlet.DispatcherServlet </ servlet-class> &ltload-on-startup&gt1 </ load-on-startup> </ servlet> &ltservlet-mapping> &ltservlet -name&gtdispatcher </ servlet-name> &lturl-pattern>/</ url-pattern> </ servlet-mapping> </ web-app>
次に、プロジェクト名を右クリックして、「モジュール設定を開く...」を選択します。モジュール->アプリケーション名-> Webを選択し、WebResourceDirectoryウィンドウで、Webリソースディレクトリをdirectory\WebApp\webに変更します。ここで、ディレクトリは、コンピューター上のIntelliJプロジェクトの場所です。次に、「適用」および「OK」をクリックします。
それだけです:)これで、緑色の矢印を押して、お気に入りのWebブラウザーで最初のWebアプリケーションサイトを表示できます。幸運を!