この記事では、Mavenを使用してEclipseの詳細な手順を紹介し、参照のためにSpring MVCを構築します。特定のコンテンツは次のとおりです
1。環境構成
a)。 Java 1.7
b)。 Eclipse luna
c)。 Maven3.2.5
d).Spring 4.1.4
2。Mavenプロジェクトを作成します
a)。 Eclipse、file-> new-> project-> maven-> mavenプロジェクトを開きます
b)。次のステップ
c)。作成されたプロジェクトをWebApp、次のステップとして選択します
d)。プロジェクトのグループIDとアーティファクトIDに記入します。一般的に、グループIDはドメイン名の逆の順序を書き込み、Artifact IDはプロジェクト名を書き込みます。最後のポイントを終了します。
e)。最初に構築した後、プロジェクトディレクトリ構造は次のとおりです
f)。一般的なプロジェクトディレクトリでは、Java Resourcesディレクトリには、SRC/Main/Java、SRC/Main/Test/Java、およびSRC/Main/Test/Resourcesの3つのソースフォルダーがあり、手動で作成する必要があります。次の手順では、これらの3つのディレクトリを埋める方法について説明します。
3.基本的なプロジェクト設定を変更します
a)。プロジェクト名を右クリック - >プロパティ - > javaビルドパスをクリックし、ソースタグをクリックします。
b)。 hello/src/main/java(行方不明)およびhello/src/test/java(行方不明)。一般的なプロジェクトディレクトリでは、Java Resourcesディレクトリには、ソースフォルダーSRC/Main/Test/Resourcesもあります。不足しているものを最初に削除してから、それらを再作成し、不足しているものを直接作成します。キーを右クリックして削除して追加します。
c)。それを完全に変更すると、効果は下の図に示すとおりです
d)。次に、ライブラリの構成を変更すると、JREはバージョン1.7を使用します。 JREシステムライブラリを選択します - >編集してバージョンを変更します。
e)。構成を順番に変更し、もう一度エクスポートします。主なことは、これら4つのディレクトリの表示順序を調整し、好きな注文を調整することです
f)。次に、プロジェクトファセットを変更し、最初にJavaを1.7に変更します。
ここでは、動的Webモジュールを3.0に直接変更することはできません。プロジェクトディレクトリで.settingsフォルダーを開く必要があります。org.eclipse.wst.common.project.facet.core.xmlを開き、次の変更を行います。
<インストールFACET = "JST.WEB"バージョン= "3.0"/>
Eclipeを再起動すると、変更が有効になることがわかります。
4.日食のMaven構成
a).window-> Properties-> maven、起動時のダウンロードリポジトリインデックスの更新を確認してください
5。単純なスプリングMVC構成
a)。プロジェクトでpom.xmlファイルを開き、[依存関係]タブをクリックして[追加]をクリックして追加して新しい依存関係を追加します
b)。依存関係グループIDとArtifact IDがわかっている場合は、直接入力できます。明確でない場合は、キーワードを入力してクエリをするか、http://search.maven.orgで検索できます。
c)。追加する必要がある依存関係は、Spring-Webmvc、バージョン4.1.4です。リリース。完全なpom.xmlファイルコンテンツは次のとおりです。
<Project XMLNS = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apach/4.0. http://maven.apache.org/maven-v4_0_0.xsd "> <modelversion> 4.0.0 </modelversion> <groupid> com.springstudy </groupid> <artifactid> sudy </artifactid> <packaging> war </packgen> </sudnapshot> <url> http://maven.apache.org </url> <properties> <spring.version> 4.1.4.Release </spring.version> </properties> <dependencies> <shiplency> <shipid> junit </groupid> <artifactid> junit </scop> </scop> </scop <Dependency> GroupId> org.SpringFrameWork </groupId> <artifactid> spring-webmvc </artifactid> <version> $ {spring.version} </version> </dependency> </dependencies> <build> <finalName> sudy </finalName> </build> </build> </build>d)。 SRC/Main/WebApp/web-inf/web.xmlファイルを開き、最後に次のように変更します。
<?xml version = "1.0" encoding = "utf-8"?> <web-app xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/xml/nns/javaee xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id =" Study "バージョン=" 2.5 "> <display-name> Archetype作成されたWebアプリケーション</display-name> <説明<param-value> classpath:/configs/spring-*。xml </param-value> </context-param> <! - spring reastin-> <リスナー> <リスナー> <リスナークラス> org.springframework.web.context.contextloaderlistener </ristener-class> </<! <servlet-name> dispatcher </servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet </servlet-class> <! - スプリングMVCの構成ファイル名とパスをカスタマイズ - > <init-param> <param-name> contextconfiglocation </param-name> <param-value> classpath:configs/spring-servlet.xml </param-value> </init-on-startup> 1 </load-on-startup> </servlet> <! - spring mvc request suffix-> <servlet-mapping> <servet-name> dispatcher </servlet-name> <urltern> <Welcome-FileList> <Welcome-File> index.jsp </welcome-file> </welcome-file-list> </web-app>
e)。 Java Resources/SCR/Main/Resourcesディレクトリに構成フォルダーを作成して、web.xmlで宣言された構成パスを保存します
f)。 Java Resources/SCR/Main/Resources/Configs DirectoryでSpring-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:jee = "http://www.springframework.org/schema/jee" xmlns:context = "http://www.springframework.org/schema/context" xmlns:p = "http://www.springframework XMLNS:MVC = "http://www.springframework.org/schema/mvc" xmlns:util = "http://www.springframework.org/schema/util" xsi:schemalocation = " http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context-4.tpring http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd "> <コンテキスト:annotation-config/> <コンテキスト:コンポーネント - パッケージ=" com.springstudy. <MVC:Annotation-driven/> <MVC:リソースマッピング= "/styles/**" location = "/styles/"/> <mvc:resources mapping = "/scripts/**" location = "/scripts/"/> <MVC:リソースマッピング= "/画像/**"場所= " value = "/web-inf/views/"/> <プロパティname = "suffix" value = "。jsp"/> </bean> </beans>
g)。コントローラーパッケージを作成します。 Spring-servlet.xmlファイルでは、パスが指定されています。
h)。 SRC/Main/WebApp/Web-INFディレクトリで、Viewsファイルを作成します。 spring-servlet.xmlファイルで、ビューファイルパスが指定されています
私)。最初のコントローラーファイルhellocontroller.javaを作成すると、完全なファイルコンテンツは次のとおりです。
パッケージcom.springstudy.controller; org.springframework.stereotype.controllerをインポートします。 org.springframework.web.bind.annotation.requestmappingをインポートします。 Import org.springframework.web.servlet.modelandview; @controller public class hellocontroller {@requestmapping( "/hello")public modelandview hello(){modelandview mv = new modelandview(); mv.addobject( "spring"、 "spring mvc"); mv.setViewName( "Hello"); MVを返します。 }}j)。 src/main/webapp/web-inf/views/hello.jspファイルを追加すると、コンテンツは次のとおりです。
<!doctype html> <html> <head> <meta charset = "utf-8"> <title> sprint hello </title> </head> <body> hello $ {spring}! </body> </html>6.プロジェクトをTomcatに公開します
a)。 EclipseにTomcat 7を追加します。
b)。 Tomcatが追加された後、ダブルクリックして、[概要]タブでサーバーの場所の設定を設定します。
私。 [Tomcatのインストールを使用]を選択します(Tomcatのインストールの制御を取ります)
ii。展開パスのコンテンツを次のように変更します。WebApps
iii.save
c)。 Tomcatを右クリックし、追加して削除してください…、研究を追加します
d)。 Tomcatを開始します。
e)。 http:// localhost:8080/study/hello in the browserで開くと、アクセスが成功しました!下の図に示すように:
操作は終わりました!
上記は、Mavenを使用してSpring MVCを構築するEclipseのすべての内容です。私はそれがあなたに参照を与えることができることを願っています、そしてあなたがwulin.comをもっとサポートできることを願っています。