美文网首页
Spring 3 MVC hello world example

Spring 3 MVC hello world example

作者: lovePython | 来源:发表于2015-08-19 10:47 被阅读66次

5. Spring @JavaConfig

SpringWebConfig.java

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

@EnableWebMvc //mvc:annotation-driven
@Configuration
@ComponentScan({ "com.mkyong.web" })
public class SpringWebConfig extends WebMvcConfigurerAdapter { 
    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) {   
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
    } 
    
    @Bean 
    public InternalResourceViewResolver viewResolver() { 
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();  
        viewResolver.setViewClass(JstlView.class); 
        viewResolver.setPrefix("/WEB-INF/views/jsp/"); 
        viewResolver.setSuffix(".jsp"); 
        return viewResolver; 
    } 
}

XML equivalent.

<beans xmlns="http://www.springframework.org/schema/beans" 
            xmlns:context="http://www.springframework.org/schema/context" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:mvc="http://www.springframework.org/schema/mvc" 
            xsi:schemaLocation="http://www.springframework.org/schema/beans  
                                             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
                                             http://www.springframework.org/schema/mvc 
                                             http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
                                             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 
    <context:component-scan base-package="com.mkyong.web" /> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix"> 
            <value>/WEB-INF/views/jsp/</value> 
        </property> 
        <property name="suffix"> 
            <value>.jsp</value> 
        </property>
    </bean> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 
    <mvc:annotation-driven /> 
</beans>

6. Servlet 3.0+ Container

Create a ServletInitializer class by extending AbstractAnnotationConfigDispatcherServletInitializer, the Servlet 3.0+ container will pick up this class and run it automatically. This is the replacement for web.xml.

MyWebInitializer.java

package com.mkyong.servlet3;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import com.mkyong.config.SpringWebConfig;
public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {  
    @Override 
    protected Class<?>[] getServletConfigClasses() { 
        return new Class[] { 
            SpringWebConfig.class 
        }; 
    } 

    @Override 
    protected String[] getServletMappings() { 
        return new String[] { "/" }; 
    } 

    @Override 
    protected Class<?>[] getRootConfigClasses() { return null; }
}

相关文章

网友评论

      本文标题:Spring 3 MVC hello world example

      本文链接:https://www.haomeiwen.com/subject/cwcgqttx.html