web-dev-qa-db-ja.com

IntelliJ + Spring Web MVC

IntelliJ 2016.1.3とSpring Web MVCの統合に問題があります。私が作った手順:

  1. ファイル->新規->プロジェクト...-> Maven(アーキタイプなし)
  2. GroupId =テストArtifactId =アプリ
  3. プロジェクト名= App and Finish。
  4. Pom.xmlに追加しました<パッケージング> war </ packaging>
  5. 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> 
    
  6. 次に、プロジェクトにモジュールを追加しました(プロジェクト名を右クリック-> Add Framework Support ...)。私はSpring MVCとダウンロードを選択しました(構成...-すべてのアイテムを選択しました)。

  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を配置しました

    &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> 
     
    
    &ltservlet-mapping&gt
        &ltservlet-name&gtWebServlet&lt/servlet-name&gt
        &lturl-pattern&gt/&lt/url-pattern&gt
    &lt/servlet-mapping&gt
    
     
     </ web-app> 
    
  9. 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">
    
     
    
    &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
    
     
     </ beans> 
     
    
  10. 最後に、test.jspファイルをwebapp\WEB-INF\viewsに追加しました。さらに、モジュールの依存関係を追加する必要がありました(F4->モジュール->依存関係-> +->ライブラリ-> mavenから->型付きjavax.servlet:jstl:1.2)

  11. 次のステップは、アプリケーションを実行する必要があります。構成(緑の矢印の横にある下矢印)-> +-> TomcatServer->ローカルを編集する必要があり、警告が表示されました。残念ながら、この問題は解決できません。 [修正]ボタンがありますが、これを押すと[展開]タブが表示され、何をすべきかはわかりません。

デプロイメント構成を手伝ってください。IntelliJでSpring Webアプリケーションを作成するための私の方法であるか、別のより良い方法を教えてください。 YouTubeでいくつかの映画を見て、Intellijにないオプションを表示したり、オプションが非表示になっていて見つからないため、ステップバイステップチュートリアルが必要です。宜しくお願いします

15
kmmm

すべてを正しく設定している場合は、展開タブの右上に+-記号が表示されているはずです。それを押すと、1〜2のオプションのツールチップが表示されます。

  • アーティファクト...
  • 外部ソース...

通常、「アーティファクト...」を選択して、現在のプロジェクトのデプロイメントアーティファクトを選択します。

HTH

4
Florian Stendel

手順11.で警告を受け取ったら

  1. 「配置」タブに移動します「+」ボタンを押します。
  2. オプション「Artifact ...」および「External Source ..」を含むコンテキストメニューが表示されます。
  3. 「アーティファクト...」を選択すると、ダイアログが表示され、「project_name:war」と「project_name:war expanded」の2つのオプションが表示されます。
  4. 'project_name:war expanded'オプションを選択すると、警告が解決されます。
2
Asif Khokhar

IntelliJでSpring Web MVCプロジェクトを作成するための完全なステップバイステップのチュートリアルがあります。

  1. ファイル->新規->プロジェクト-> Maven(「アーキタイプから作成」のチェックを外します)->次へ。
  2. GroupIdとArtifactIdを入力します。たとえば、GroupId = 'test'、ArtifactId = 'app'とし、[次へ]をクリックします。
  3. プロジェクト名を入力します。たとえば、プロジェクト名= 'WebApp'で、[完了]をクリックします。
  4. プロジェクト名を右クリックして、「フレームワークサポートの追加...」を選択します。次に、「Spring MVC」と「ダウンロード」オプションを選択します。 OKをクリックします。
  5. 以下のコードのように、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> 
     
    
    &ltgroupId&gttest&lt/groupId&gt
    &ltartifactId&gtapp&lt/artifactId&gt
    &ltversion&gt1.0-SNAPSHOT&lt/version&gt
    &ltpackaging&gtwar&lt/packaging&gt
    
    &ltdependencies&gt
        &ltdependency&gt
            &ltgroupId&gtorg.springframework&lt/groupId&gt
            &ltartifactId&gtspring-webmvc&lt/artifactId&gt
            &ltversion&gt4.2.5.RELEASE&lt/version&gt
        &lt/dependency&gt
    &lt/dependencies&gt
    
     
     </ project> 
    
  6. IntelliJウィンドウの右上隅に、「Mavenプロジェクトをインポートする必要があります」という情報パネルが表示されます。 [変更をインポート]をクリックします。

  7. 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"; 
    } 
    } 
    
  8. 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">
    
     
    
    &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
    
     
     </ beans> 
    
  9. Web/WEB-INFディレクトリに新しいビュー「views」を作成し、そこにファイルindex.jspをwebディレクトリから移動します。

  10. ファイル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> 
    
  11. 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> 
    
  12. 次に、プロジェクト名を右クリックして、「モジュール設定を開く...」を選択します。モジュール->アプリケーション名-> Webを選択し、WebResourceDirectoryウィンドウで、Webリソースディレクトリをdirectory\WebApp\webに変更します。ここで、ディレクトリは、コンピューター上のIntelliJプロジェクトの場所です。次に、「適用」および「OK」をクリックします。

  13. 次に、実行構成を編集する必要があります。緑の矢印の横にある下矢印を押し、[構成の編集...]を選択してから、緑の+をクリックします。 Tomcatサーバー->ローカルを選択します。 「アプリケーションサーバー」セクションで、メインのTomcatディレクトリを選択します。ウィンドウの下部に警告が表示されます。 [修正]をクリックして、「WebApp:war eploded」または類似のものを選択します。アプリケーション名によって異なります。次に、「適用」および「OK」をクリックします。

それだけです:)これで、緑色の矢印を押して、お気に入りのWebブラウザーで最初のWebアプリケーションサイトを表示できます。幸運を!

1
kmmm