• spring: spittr实例 构建简单的web应用


    我的环境是: jdk8, spirng4

    之前照者书上说的做了,不得成功,于是网上百度,不得其然。

    后来看到一篇文章,甚是所感。https://segmentfault.com/q/1010000007921684/a-1020000007922842

    单词拼错了,无地自容……
    我的包名是spittr
    但是我的自动扫描写的是spitter,
    
    晕,忘了给视图解析器加@Bean了
    
    或者很多注解写成一排了
    

      

    于是乎鼓捣重来

    SpittrWebAppInitializer 
    package spittr.config;
    
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
    
    	 @Override
    	 protected Class<?>[] getRootConfigClasses() {
    		 //根容器
    		 return new Class<?>[] { RootConfig.class };
    	    }
    
    	    @Override
    	    protected Class<?>[] getServletConfigClasses() { 
    	    	//Spring mvc容器
    	    	return new Class<?>[] { WebConfig.class };
    	    }
    
    	    @Override
    	    protected String[] getServletMappings() { 
    	    	//DispatcherServlet映射,从"/"开始
    	    	return new String[] { "/" };
    	    }
    
    }
    

      RootConfig

    package spittr.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    
    @Configuration
    @ComponentScan(
    		basePackages = {"spittr"},
            excludeFilters = {
            		@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)
            		}
    		)
    public class RootConfig {
    }
    

      WebConfig

    package spittr.config;
    
    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.DefaultServletHandlerConfigurer;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;
    
    @Configuration
    @EnableWebMvc
    @ComponentScan("spittr.web")
    public class WebConfig extends WebMvcConfigurerAdapter{
    	
    	@Bean
        public ViewResolver viewResolver() { //配置JSP视图解析器
    	InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            //可以在JSP页面中通过${}访问beans
            resolver.setExposeContextBeansAsAttributes(true);
            return resolver;
        }
    
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable(); //配置静态文件处理
        }
    }
    

      

    homeController

    package spittr.web;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    @Controller
    public class HomeController {
    	
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String home() {
            return "home";
        }
        
    }
    

      

    目录如下:

    运行结果:

  • 相关阅读:
    FAST for SharePoint如何重置Index
    SharePoint 2007有性能问题? 先试试这篇.
    "Cannot generate SSPI context"
    记录一个用过的SQL脚本(select * into)
    IISRESET为什么有时候要带个noforce参数?
    SharePoint跟权限有关的Object Model Class
    DotNET也谈组合强命名破解FlyGrid 1.5.0.31963 for VS2003
    应同学之邀,破解一个软件,学习逆向工程,文章如下
    win2003下建立*.*到asp.net的映射(安装CNBlogsDotText用)
    MS.Net CLR 扩展PE结构分析
  • 原文地址:https://www.cnblogs.com/achengmu/p/8334664.html
Copyright © 2020-2023  润新知