1

我正在使用IntelliJ创建SpringMVC应用程序。maven我参考了这个链接来创建 SpringMVC maven 应用程序。这里,是一个代码

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>temp-maven</groupId>
    <artifactId>spring-maven</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <properties>
        <spring.version>5.1.0.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>

    </dependencies>
</project>

web.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!--<web-app xmlns=”http://java.sun.com/xml/ns/j2ee"-->
        <!--xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"-->
        <!--xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"-->
        <!--version=”2.4″&gt;-->
<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">
    <display-name>HelloWorld Application</display-name>

    <description>
        This is a simple web application for karaf deployment test.
    </description>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

我想在Karaf容器上部署这个应用程序的 WAR。要生成.war文件,我在项目目录中执行以下命令。

mvn compile
mvn package

.war会在/target目录中生成。之后,我将生成的.war文件复制/deploykaraf. 这会自动将我的包部署在Karaf. 在执行以下命令时,它显示.war捆绑包已部署。

karaf@root()> web:list

ID  │ State       │ Web-State   │ Level │ Web-ContextPath │ Name
────┼─────────────┼─────────────┼───────┼─────────────────┼──────────────────────────────
151 │ Active      │ Deployed    │ 80    │ /spring-maven   │ spring-maven (1.0.0.SNAPSHOT)

karaf@root()> http:list

ID  │ Servlet               │ Servlet-Name             │ State       │ Alias               │ Url
────┼───────────────────────┼──────────────────────────┼─────────────┼─────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
59  │ TomcatResourceServlet │ /system/console/res:/res │ Deployed    │ /system/console/res │ [/system/console/res/*]
59  │ KarafOsgiManager      │ ServletModel-6           │ Deployed    │ /system/console     │ [/system/console/*]
151 │ TomcatResourceServlet │ default                  │ Deployed    │ /spring-maven/      │ [/spring-maven/]
151 │ DispatcherServlet     │ dispatcher               │ Deployed    │                     │ [/spring-maven/]
151 │ JspServletWrapper     │ jsp                      │ Deployed    │                     │ [/spring-maven/*.jsp, /spring-maven/*.jspx, /spring-maven/*.jspf, /spring-maven/*.xsp, /spring-maven/*.JSP, /spring-maven/*.JSPX, /spring-maven/*.JSPF, /spring-maven/*.XSP]

但是当我打开http://localhost:8181/spring-maven/时,它显示HTTP Status 404 – Not Found. Apache Tomcat/8.5.32. 我的机器上没有tomcat。我没有找到我要去的地方。请在这个问题上指导我。

谢谢你。

4

1 回答 1

0

However, Karaf not perform component-scan like Tomcat. Remove these 2 lines <context:component-scan base-package="YourPackageName" /> <mvc:annotation-driven /> and adding <context:annotation-config /> in HelloWeb-servlet.xml worked for me.

于 2019-03-04T21:47:10.183 回答