spring - 找不到文件 Spring 项目的问题

标签 spring spring-mvc websphere

我正在尝试启动并运行一个简单的 Spring 项目,但它似乎不起作用。没有错误。尝试改变各种东西但没有运气。下面是类

AppInitializer.java

package com.spring.config;

import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class AppInitializer implements WebApplicationInitializer {  

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.scan("com.spring");

    // Manages the lifecycle of the root application context
    servletContext.addListener(new ContextLoaderListener(rootContext));

    // Declare dispatcher servlet. Handles requests into the application
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");



}



}

WebAppContextConfig.java

   package com.spring.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.spring")
public class WebAppContextConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver configureInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
        registry.addResourceHandler("/vendor/**").addResourceLocations("/vendor/**");
    }

}

AppConfig.java`

package com.spring.config;

import org.springframework.context.annotation.Configuration;

@Configuration

public class AppConfig {

}

HelloController.java

package com.spring.controller;

   import org.springframework.stereotype.Controller;
   import org.springframework.web.bind.annotation.RequestMapping;
   import org.springframework.web.bind.annotation.RequestMethod;
   import org.springframework.web.bind.annotation.ResponseBody;

@Controller
  public class HelloController {

@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public String showIndex() {
    return "Hello world";
}

@RequestMapping(value = "/statuscheck", method = RequestMethod.GET)
public @ResponseBody
String getStatusCheck() {
    return "SUCCESS";
}



  }

pom.xml

我得到了 spring-context (4.3.0-RELEASE)、spring-webmvc (4.3.0-RELEASE)、javax.servlet-api、jSTL 作为依赖项。

我正在使用 websphere 8.5.5.9

我试过检查这个网址:

http://localhost:8080/SpringProject/statuscheck

我得到了

[WARNING ] SRVE0190E: File not found: /statuscheck

EDIT-1

我在 server.xml 中有这个

  <webApplication id="SpringProject" contextRoot = "SpringProject" location="SpringProject.war" name="SpringProject"/>

EDIT-2

我在 pom.xml 中得到了这段代码。所以我没有 web.xml

<plugin>
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-war-plugin</artifactId> 
<version>2.4</version> 
   <configuration>      
<warSourceDirectory>src/main/webapp</warSourceDirector‌​y>    
<warName>SpringProject</warName> 
<failOnMissingWebXml>false</failOnMissingWebXml> 
   </configuration> 
</plugin> 

EDIT-3

这是日志文件。我在日志中没有看到任何问题

[3/20/17 15:29:25:666 CDT] 00000001 id=         com.ibm.ws.kernel.launch.internal.FrameworkManager           I CWWKE0002I: The kernel started after 4.637 seconds
[3/20/17 15:29:32:064 CDT] 0000001d id=         com.ibm.ws.security.ready.internal.SecurityReadyServiceImpl  I CWWKS0007I: The security service is starting...
[3/20/17 15:29:32:089 CDT] 0000001d id=         ibm.ws.security.registry.basic.internal.DynamicBasicRegistry W CWWKS3103W: There are no users defined for the BasicRegistry configuration of ID basic.
[3/20/17 15:29:32:524 CDT] 0000001d id=         com.ibm.ws.security.jaspi.AuthConfigFactoryWrapper           I CWWKS1655I: The default Java Authentication SPI for Containers (JASPIC) AuthConfigFactory class com.ibm.ws.security.jaspi.ProviderRegistry is being used because the Java security property authconfigprovider.factory is not set. 
[3/20/17 15:29:32:555 CDT] 0000001d id=         com.ibm.ws.app.manager.internal.monitor.DropinMonitor        A CWWKZ0058I: Monitoring dropins for applications. 
[3/20/17 15:29:32:585 CDT] 0000003c id=         com.ibm.ws.security.ready.internal.SecurityReadyServiceImpl  I CWWKS0008I: The security service is ready.
[3/20/17 15:29:32:586 CDT] 0000003c id=         com.ibm.ws.security.token.ltpa.internal.LTPAKeyCreator       I CWWKS4105I: LTPA configuration is ready after 0.245 seconds.
[3/20/17 15:29:32:615 CDT] 0000003f id=         com.ibm.ws.tcpchannel.internal.TCPChannel                    I CWWKO0219I: TCP Channel defaultHttpEndpoint has been started and is now listening for requests on host 127.0.0.1  (IPv4: 127.0.0.1) port 8080.
[3/20/17 15:29:34:449 CDT] 0000001d id=         com.ibm.ws.sib.utils.ras.SibMessage                          I  CWSID0108I: JMS server has started.  
[3/20/17 15:29:34:487 CDT] 0000001d id=         com.ibm.ws.tcpchannel.internal.TCPChannel                    I CWWKO0219I: TCP Channel wasJmsEndpoint480 has been started and is now listening for requests on host 127.0.0.1  (IPv4: 127.0.0.1) port 7276.
[3/20/17 15:29:34:619 CDT] 0000001d id=         com.ibm.ws.cache.ServerCache                                 I DYNA1001I: WebSphere Dynamic Cache instance named baseCache initialized successfully.
[3/20/17 15:29:34:620 CDT] 0000001d id=         com.ibm.ws.cache.ServerCache                                 I DYNA1071I: The cache provider default is being used.
[3/20/17 15:29:34:620 CDT] 0000001d id=         com.ibm.ws.cache.CacheServiceImpl                            I DYNA1056I: Dynamic Cache (object cache) initialized successfully.
[3/20/17 15:29:35:318 CDT] 0000005d id=         com.ibm.ws.http.internal.VirtualHostImpl                     A CWWKT0016I: Web application available (default_host): http://localhost:8080/SpringProject/
[3/20/17 15:29:35:322 CDT] 0000005d id=         com.ibm.ws.app.manager.AppMessageHelper                      A CWWKZ0001I: Application SpringProject started in 0.302 seconds.
[3/20/17 15:29:35:328 CDT] 00000027 id=         com.ibm.ws.kernel.feature.internal.FeatureManager            A CWWKF0012I: The server installed the following features: [mdb-3.2, localConnector-1.0, webProfile-7.0, json-1.0, jaxrs-2.0, appSecurity-2.0, jpa-2.1, jaspic-1.1, distributedMap-1.0, jaxb-2.2, jaxws-2.2, ssl-1.0, jdbc-4.1, managedBeans-1.0, jsf-2.2, appClientSupport-1.0, jacc-1.5, ejbHome-3.2, wasJmsClient-2.0, cdi-1.2, wasJmsSecurity-1.0, jaxrsClient-2.0, ejbRemote-3.2, javaMail-1.5, batch-1.0, j2eeManagement-1.1, websocket-1.1, el-3.0, beanValidation-1.1, ejbPersistentTimer-3.2, jca-1.7, wasJmsServer-1.0, servlet-3.1, ejb-3.2, jsp-2.3, jndi-1.0, jsonp-1.0, concurrent-1.0, ejbLite-3.2, jcaInboundSecurity-1.0, javaee-7.0].
[3/20/17 15:29:35:329 CDT] 00000027 id=         com.ibm.ws.kernel.feature.internal.FeatureManager            A CWWKF0011I: The server server2 is ready to run a smarter planet.
 [3/20/17 15:29:36:368 CDT] 0000006b id=         com.ibm.ws.cache.CacheServiceImpl                            I DYNA1056I: Dynamic Cache (object cache) initialized successfully.
[3/20/17 15:29:44:733 CDT] 00000045 id=         ibm.ws.transport.iiop.security.AbstractCsiv2SubsystemFactory E CWWKS9582E: The [defaultSSLConfig] sslRef attributes required by the orb element with the defaultOrb id have not been resolved within 10 seconds. As a result, the applications will not start. Ensure that you have included a keyStore element and that Secure Sockets Layer (SSL) is configured correctly. If the sslRef is defaultSSLConfig, then add a keyStore element with the id defaultKeyStore and a password.

这是我的项目结构

enter image description here

EDIT-4 我有以下项目方面

enter image description here

我有jdk 7

enter image description here

最佳答案

据我所知,你应该告诉 spring 在哪里调整配置类; 您是否尝试过以下操作:

package com.spring.config;
public class AppInitializer implements WebApplicationInitializer {  

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Create the 'root' Spring application context
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.scan("com.spring");
    rootContext.setConfigLocations(YOUR SPRING CFG FULL QUALIFIED CLASSES );

    // Manages the lifecycle of the root application context
    servletContext.addListener(new ContextLoaderListener(rootContext));

    // Declare dispatcher servlet. Handles requests into the application
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher",
            new DispatcherServlet(rootContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
}
}

试着在这里看看AnnotationConfigWebApplicationContext在这里 AbstractRefreshableConfigApplicationContext

关于spring - 找不到文件 Spring 项目的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42848344/

相关文章:

java - 使用请求范围时的线程安全代码

java - Spring MVC3 @SessionAttributes 和@ModelAttribute 会自动从请求中获取值

java - 通过 JVM 参数相对路径加载 SSL 配置

java - Spring batch FileItemWriter 没有在正确的路径创建文件

java - JPA 事件监听器不工作

java - 获取请求仅适用于尾部斜杠(spring REST 注释)

java - Spring JUnit测试中WEB-INF/spring/webmvc-config.xml配置如何正确

java - OpenEntityManagerInViewFilter批注配置

spring-mvc - 通过 Spring MVC 对移动客户端进行 RESTful 身份验证

java: Websphere Singleton 存在于多个 EAR 的 WAR 中?