我正在使用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)
Controller的主要方法没有被执行。根据另一个类似的案例和Spring引用,我也尝试在AppInitializer类中切换Servlet Mapping路由,如下所示:
@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'
有人可以帮我找出我的代码有什么问题吗?谢谢。
答案 0 :(得分:0)
您忘记将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) { ...}