Apache Karaf WAR文件部署无法正常工作。 404-在网络浏览器上找不到

时间:2018-10-11 01:16:56

标签: maven spring-mvc apache-karaf karaf karaf-maven-plugin

我正在IntelliJ中使用SpringMVC创建maven应用程序。我引用了this链接来创建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″>-->
<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文件复制到/deploy的{​​{1}}目录中。这会自动将我的捆绑软件部署在karaf中。执行以下命令后,它显示Karaf捆绑软件已部署。

.war

但是当我打开http://localhost:8181/spring-maven/时,它显示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] 。我的机器上没有tomcat。我没有弄错我要去的地方。请指导我解决这个问题。

谢谢。

1 个答案:

答案 0 :(得分:0)

但是,Karaf不像Tomcat那样执行组件扫描。 删除这2行 <context:component-scan base-package="YourPackageName" /> <mvc:annotation-driven />添加 <context:annotation-config />中的HelloWeb-servlet.xml为我工作。