我正在尝试启动并运行一个简单的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</warSourceDirectory>
<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.
这是我的项目结构
EDIT-4 我有以下项目方面
我有jdk 7
答案 0 :(得分:1)
据我所知,你应该告诉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
答案 1 :(得分:1)
这可能会导致问题,请更改
@RequestMapping(value = "/statuscheck", method = RequestMethod.GET)
public @ResponseBody
String getStatusCheck() {
return "SUCCESS";
}
要
@RequestMapping(value = "/statuscheck", method = RequestMethod.GET)
@ResponseBody
public String getStatusCheck() {
return "SUCCESS";
}