将servlet-context.xml转换为ServletConfig.java

时间:2016-05-30 05:41:13

标签: spring-mvc configuration

我正在尝试将我的servlet-context.xml文件转换为Servlet Config.java,并且没有编译时错误。但在运行时,我的服务器显示/ product /没有请求映射。 我的web.xml是

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
        <init-param>
            <param-name>contextConfigLocatation</param-name>
            <param-value>com.mds.test.ServetConfig</param-value>
        </init-param>

        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping> 
</web-app>

我的ServletConfig是:

package com.mds.test;

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.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages={"com.mds.test"})
@EnableWebMvc
public class ServletConfig extends WebMvcConfigurerAdapter{

    @Bean
    public ViewResolver internalResourceViewer(){

        InternalResourceViewResolver irvr= new InternalResourceViewResolver();
        irvr.setPrefix("/WEB-INF/views/");
        irvr.setSuffix(".jsp");
        return irvr;
    }

    @Bean(name="multipartResolver")
    public ExtendedMultipartResolver resolver(){
        return new ExtendedMultipartResolver();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // TODO Auto-generated method stub
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }   
}

你能说出我错过的东西吗?提前致谢

1 个答案:

答案 0 :(得分:0)

对不起,伙计们,我做了一个很小但很混乱的错误。我写错了类名。类名应该是com.mds.test.ServletConfig而不是web.xml中的com.mds.test.ServetConfig

所以大家可以把它当作从xml到java文件的简单转换器。