2016-12-15 266 views
0

我正尝试将我们的应用程序迁移到使用Spring 4功能。所以我正在慢慢尝试删除XML配置(web.xml,applicationContext.xml,dispatcher-servlet.xml)和替换为Java注释。我花了很多时间在这个问题上,我也找不到在stackoverflow中的解决方案。我得到下面的错误:在名为'dispatcher'的DispatcherServlet中未找到具有URI的HTTP请求的映射。

org.springframework.web.servlet.PageNotFound - [] [行号 - 1136]没有在DispatcherServlet的找到HTTP请求与URI [/ WP/xhStatus]与名称映射“调度”。 POST请求“http://localhost:8080/WP/xhStatus”导致404(null);调用错误处理程序 org.springframework.web.client.HttpClientErrorException:404空

从这个错误信息,我想看看是否PageNotFound在Spring API包org.springframework.web.servlet在可用下面的网址,但该包中没有这样的类或可用接口: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/package-summary.html

我只是添加以下两类WebAppInitializer.java & WebConfig.java删除部分代码web.xmldispatcher-servlet.xml

WebAppInitializer.java

package com.mycompany.wp.web.config; 

public class WebAppInitializer implements WebApplicationInitializer { 

@Override 
public void onStartup(ServletContext servletContext) throws ServletException { 
    AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); 
    applicationContext.setConfigLocations("com.mycompany.wp.web.config.WebConfig"); 
    servletContext.addListener(new ContextLoaderListener(applicationContext)); 

    servletContext.setInitParameter("contextConfigLocation", "/WEB-INF/applicationContext.xml"); 

    ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("dispatcher", new DispatcherServlet(applicationContext)); 
    dispatcherServlet.setLoadOnStartup(1); 
    dispatcherServlet.addMapping("/"); 
} 

} 

WebConfig.java

package com.mycompany.wp.web.config; 

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages="com.mycompany.wp") 
public class WebConfig extends WebMvcConfigurerAdapter{ 

@Bean 
public InternalResourceViewResolver viewResolver() { 
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
    viewResolver.setViewClass(JstlView.class); 
    viewResolver.setPrefix("/views/"); 
    viewResolver.setSuffix(".jsp"); 
    return viewResolver; 
} 

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
} 
} 

除去的web.xml这一部分:

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/applicationContext.xml,/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> 

而且我也删除以下部分从调度-servlet.xml中

<mvc:resources mapping="/resources/**" location="/resources/" /> 

<context:component-scan base-package="com.mycompany.wp"/> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/views/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

现在调度-servlet.xml中只有一个入口:

<mvc:annotation-driven /> 

我仍然在我的应用程序中拥有所有XML文件(web.xml,dispatcher-servlet.xml & applicationContext.xml),因为它们还有一些其他信息。但是,applicationContext.xml中没有更改。

我想调用REST服务(Spring MVC控制器)与请求映射为/xhStatus,我没有改变任何东西。之前,我的REST服务可以通过上面提到的URL访问,但现在我无法访问。

在转移到Java配置之前,我的应用程序曾经用于XML配置。 DispatcherServlet可能无法找到我的Spring Controller。

如果有人能够帮助我,那将会很棒。谢谢。

回答

0

我修复了这个问题。我在犯了一个错误WebAppInitializer.java

我用:

applicationContext.setConfigLocations("com.mycompany.wp.web.config.WebConfig"); 

现在我更换了符合:

applicationContext.register(com.mycompany.wp.web.config.WebConfig.class); 

它工作得很好。

相关问题