2017-02-21 12 views
1

私はJ2EEとSpringフレームワークが初めてです。 私が作った私はMavenを使用してのNetBeans v.8.2にこのフレームワークを採用し始めたいが、私はミックスに春を追加するときに、それもGlassFishの4SpringMVCがNetbeans 8.2(mavenビルド)で動作しません

にデプロイしません:

File > new Project > Maven project > Web Application

私は2番目の瞬間にSpringを追加したいので。

は、その後、私は春を形作るために 春-conf.xmlファイルを追加して、/ WEB-INFに:

<?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"> 

    <!-- declaration of dispatcher servlet (Single servlet of Spring MVC) -->  
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-conf.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <!-- servlet-mapping of dispatcher servlet (Single servlet of Spring MVC) --> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/spring-mvc/*</url-pattern> 
    </servlet-mapping> 
    <!-- end of servlet-mapping of dispatcher servlet (Single servlet of Spring MVC --> 
</web-app> 

はこれがある:これは私の配備記述子

<?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:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 

    <context:component-scan base-package="alex.mawashi" /> 
    <mvc:annotation-driven /> 

</beans> 

です私のpom.xmlすべての依存関係が正しくダウンロードされたファイル:

私はまた私のプロジェクトツリーのスクリーンショットを添付し

package alex.mawashi.myspringmvcexample; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Controller 
public class SpringFirstController { 

    @RequestMapping(value = "/gimmeResponseBody") 
    @ResponseBody 
    public String sayHello() { 
     return "<p> Ciao! Questa risposta è direttamente ottenuta con il @ResponseBody! </p>"; 
    } 

    @RequestMapping(value = "/") 
    @ResponseBody 
    public String kimoNO() { 
     return "jspPage"; 
    } 
} 

<?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> 
<groupId>alex.mawashi</groupId> 
<artifactId>MySpringMVCExample</artifactId> 
<version>1.0-SNAPSHOT</version> 
<packaging>war</packaging> 
<name>MySpringMVCExample</name> 

<properties> 
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-web-api</artifactId> 
     <version>7.0</version> 
     <scope>provided</scope> 
    </dependency> 
    <!-- spring MVC dependencies--> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>4.3.6.RELEASE</version> 
     <exclusions> 
      <exclusion> 
       <groupId>org.springframework</groupId> 
       <artifactId>spring-web</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
    <!-- end of spring MVC dependencies--> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
       <compilerArguments> 
        <endorseddirs>${endorsed.dir}</endorseddirs> 
       </compilerArguments> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>2.3</version> 
      <configuration> 
       <failOnMissingWebXml>false</failOnMissingWebXml> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <version>2.6</version> 
      <executions> 
       <execution> 
        <phase>validate</phase> 
        <goals> 
         <goal>copy</goal> 
        </goals> 
        <configuration> 
         <outputDirectory>${endorsed.dir}</outputDirectory> 
         <silent>true</silent> 
         <artifactItems> 
          <artifactItem> 
           <groupId>javax</groupId> 
           <artifactId>javaee-endorsed-api</artifactId> 
           <version>7.0</version> 
           <type>jar</type> 
          </artifactItem> 
         </artifactItems> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

これは私のコントローラです。 enter image description here

それも展開していないと、これは誤りです:

Grave: Exception while loading the app

Grave: Undeployment failed for context /MySpringMVCExample

Grave: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 60; The prefix "context" for the element "context:component-scan" is not associated.

これはGitHubの上の小さなプロジェクトへのリンクです:それではhttps://github.com/alessandroargentieri/MySpringMCVExample

、私が何をしないのですか?ありがとう!

答えて

2

次のようにあなたの春-conf.xmlを調整します。

<exclusions> 
    <exclusion> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
    </exclusion> 
</exclusions> 

とのあなたの要求のマッピングを変更します。

また
<?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.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 


<context:component-scan base-package="alex.mawashi" /> 
    <mvc:annotation-driven /> 

</beans> 

を、POMファイルから以下を削除することを推奨しますweb.xmlは次のようになります: <url-pattern>/*</url-pattern>

+0

ありがとう、これは今のところエラーです:Grave:例外while loadin gアプリケーション:java.lang.IllegalStateException:ContainerBase.addChild:start:org.apache.catalina.LifecycleException:org.apache.catalina.LifecycleException:org.xml.sax.SAXParseException; lineNumber:17; columnNumber:60; cvc-complex-type.2.4.c:対応するジョリー文字は厳密ですが、要素 "context:component-scan"の宣言を見つけることはできません。 –

+0

XSDファイルのバージョンが一致していることを確認してください。私の例では3.0を使用していましたが、おそらくもっと新しいものを使用していました。 4.0。それもエラーを引き起こす可能性があります。 – SaWo

+0

私は4.0より3.0を試しましたが残念なことに結果はありません –

関連する問題