簡體   English   中英

將Spring MVC Web應用程序部署到JBoss時,“請求的資源不可用”

[英]“The requested resource is not available” when deploying Spring MVC webapp to JBoss

我正在使用Spring MVC部署到JBoss 6.4服務器來啟動Web應用程序,但是每當我嘗試打開主頁時,都會得到以下信息:

JBWEB000065: HTTP Status 404 - /iph/

JBWEB000309: type JBWEB000067: Status report
JBWEB000068: message /iph/
JBWEB000069: description JBWEB000124: The requested resource is not available.

JBoss Web/7.5.7.Final-redhat-1

這是我第一次使用Maven和注釋驅動的配置,我在StackOverflow上閱讀了非常相似的案例,但我不知道問題出在哪里。

這是我的pom.xml:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>mx.com.companyname.iph</groupId>
  <artifactId>iph</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>iph</name>
  <url>http://maven.apache.org</url>

  <properties>
      <maven.compiler.source>1.7</maven.compiler.source>
      <maven.compiler.target>1.7</maven.compiler.target>
      <springframework.version>4.3.13.RELEASE</springframework.version>
      <hibernate.version>4.3.6.Final</hibernate.version>
  </properties>

  <dependencies>
    <!-- JUnit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- PostgreSQL -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.1.4.jre7</version>
    </dependency>

    <!-- Java Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${springframework.version}</version>
    </dependency>

    <!-- Hibernate ORM & Validator -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.4.2.Final</version>
    </dependency>

  </dependencies>
  <build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <warName>iph</warName>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <finalName>iph</finalName>
  </build>
</project>

這是我的AppConfig類:

package mx.com.companyname.iph.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "mx.com.companyname.iph")
public class AppConfig {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

}

這是我的AppInitializer類:

package mx.com.companyname.iph.configuration;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

}

這是我的Controller類的代碼:

package mx.com.companyname.iph.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class MainController {

    @RequestMapping("/")
    public String main(HttpServletRequest request, Model model) {
        return "index";
    }

}

我的JSP視圖文件位於src / main / webapp / WEB-INF / view / index.jsp上

<%@ page language="java" contentType="text/html; UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h2>Hello world!</h2>
</body>
</html>

控制台日志顯示無錯誤。 甚至以下塊的第一行也似乎正確地將“ /”路由映射到了我的Controller的main方法:

17:15:18,748 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 51) Mapped "{[/]}" onto public java.lang.String mx.com.companyname.iph.controller.MainController.main(javax.servlet.http.HttpServletRequest,org.springframework.ui.Model)
17:15:18,803 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 51) Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Thu Dec 28 17:15:17 CST 2017]; root of context hierarchy
17:15:18,920 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 51) Root WebApplicationContext: initialization completed in 1339 ms
17:15:18,921 INFO  [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/iph]] (ServerService Thread Pool -- 51) Initializing Spring FrameworkServlet 'dispatcher'
17:15:18,921 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 51) FrameworkServlet 'dispatcher': initialization started
17:15:18,923 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 51) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Thu Dec 28 17:15:18 CST 2017]; parent: Root WebApplicationContext
17:15:18,924 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 51) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
17:15:18,950 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 51) FrameworkServlet 'dispatcher': initialization completed in 29 ms
17:15:19,006 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS015859: Implementado "iph.war" (runtime-name : "iph.war")
17:15:19,011 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Interfaz de administración http escuchando en http://127.0.0.1:9990/management
17:15:19,012 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Consola de administración escuchando en http://127.0.0.1:9990
17:15:19,012 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss EAP 6.4.0.GA (AS 7.5.0.Final-redhat-21) inició en 5133ms - Inició 617 de 655 servicios (63 servicios son perezosos, pasivos o por demanda)

控制器的主要方法未執行。 根據另一個類似的情況和Spring參考,我還嘗試如下切換AppInitializer類中的Servlet映射路由:

@Override
protected String[] getServletMappings() {
    return new String[] {"/*"};
}

Spring成功地將請求發送到Controller的main方法,但是404錯誤仍然存​​在,並且控制台上還有另一個錯誤:

17:20:23,997 WARN  [org.springframework.web.servlet.PageNotFound] (http-localhost/127.0.0.1:8080-2) No mapping found for HTTP request with URI [/iph/WEB-INF/view/index.jsp] in DispatcherServlet with name 'dispatcher'

有人可以幫助我找出我的代碼有什么問題嗎? 謝謝。

您忘記了從WebMvcConfigurerAdapter擴展AppConfig類, WebMvcConfigurerAdapter ,您可能必須在AppConfig類中添加此重寫方法:

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

    configurer.enable();

}

另外,您還有一個WebApplicationInitializer的實現配置,它等效於web.xml配置文件:

import javax.servlet.ServletContext;    
import javax.servlet.ServletException;    
import javax.servlet.ServletRegistration;    
import org.springframework.web.WebApplicationInitializer;    
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;    
import org.springframework.web.servlet.DispatcherServlet;

public class WebServletConfiguration implements WebApplicationInitializer{

    public void onStartup(ServletContext ctx) throws ServletException {    
        AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();    
        webCtx.register(AppConfig.class);    
        webCtx.setServletContext(ctx);    
        ServletRegistration.Dynamic servlet = ctx.addServlet("dispatcher", new DispatcherServlet(webCtx));    
        servlet.setLoadOnStartup(1);    
        servlet.addMapping("/");

    }

}

在這種情況下,您不需要編寫的AppInitializer類,可以將其刪除。 另外,您需要在控制器中為該方法指定請求方法:

@RequestMapping(path= "/",method=RequestMethod.GET)
    public String main(HttpServletRequest request, Model model) { ...} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM