我是J2EE和Spring框架的新手。 我只是想開始採用使用Maven Netbeans中v.8.2這個框架,但是當我增加了Spring的組合,它甚至不部署在GlassFish 4SpringMVC在Netbeans 8.2中不工作(maven build)
我做:
File > new Project > Maven project > Web Application
,因爲我想在第二時間加入Spring。
然後我添加了一個彈簧conf.xml中文件configurate春,在/ WEB-INF:
<?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>
這是我部署描述符:
<?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>
這是我的pom.xml文件,其中所有依賴關係已正確下載:
<?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>
這是我的控制器:
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";
}
}
,它甚至沒有部署,這是錯誤:
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
那麼,我缺少什麼?謝謝!
謝謝,完成但現在這是錯誤:Grave:Exception in loadin g app: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」的聲明。 –
你能確保XSD文件版本匹配嗎?在我的例子中,我使用的是3.0,但也許你正在使用更新的例如4.0。這也會導致錯誤。 – SaWo
我已經試過了4.0而不是3.0,結果很不幸 –