gpt4 book ai didi

java - SpringMVC 无法在 Netbeans 8.2 中工作(maven 构建)

转载 作者:行者123 更新时间:2023-11-30 02:38:46 25 4
gpt4 key购买 nike

我是 J2EE 和 Spring 框架的新手。我只是想开始使用 Maven 在 Netbeans v.8.2 中使用这个框架,但是当我将 Spring 添加到组合中时,它甚至没有部署在 GlassFish 4 上。

我做了:

File > new Project > Maven project > Web Application

因为我想立即添加 Spring。

然后我在/WEB-INF中添加了一个spring-conf.xml文件来配置Spring:

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

这是我的 Controller :

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";
}
}

我还附上了我的项目树的屏幕截图。 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

那么,我错过了什么?谢谢!

最佳答案

按如下方式调整 spring-conf.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: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 文件中删除以下内容:

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

并在 web.xml 中修改您的请求映射,如下所示: <url-pattern>/*</url-pattern>

关于java - SpringMVC 无法在 Netbeans 8.2 中工作(maven 构建),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42370802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com