13、书中例子说明(补1)(spring笔记)

对于书中给出的Spittr例子,亲自试验过程中发现了很多问题,这里记录一下。

首先给出pox.xml


    4.0.0
    win.iot4yj
    spring-web
    war
    0.0.1-SNAPSHOT
    SpringInAction Maven Webapp
    http://maven.apache.org

    
    
        
        
            central
            Central Repository
            https://repo.maven.apache.org/maven2
            default
            
                false
            
        

        
        
            alibaba-opensource
            alibaba-opensource
            http://code.alibabatech.com/mvn/releases/
            default
        
        
            alibaba-opensource-snapshot
            alibaba-opensource-snapshot
            http://code.alibabatech.com/mvn/snapshots/
            default
        
    

    
        UTF-8
    

    


        
        
        
            org.aspectj
            aspectjweaver
            1.7.0
        
        
            
            org.springframework
            spring-core
            4.2.5.RELEASE
        
        
            
            org.springframework
            spring-context
            4.2.5.RELEASE
        
        
            org.springframework
            spring-jdbc
            4.2.5.RELEASE
        
        
            org.springframework
            spring-beans
            4.2.5.RELEASE
        
        
            org.springframework
            spring-web
            4.2.5.RELEASE
        
        
            org.springframework
            spring-expression
            4.2.5.RELEASE
        
        
            org.springframework
            spring-orm
            4.2.5.RELEASE
        
        
            org.springframework
            spring-webmvc
            4.2.5.RELEASE
        

        
            org.springframework
            spring-test
            4.2.5.RELEASE
        

        
        
            jstl
            jstl
            1.2
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
        
            javax.servlet
            jsp-api
            2.0
            provided
        

        
        
            junit
            junit
            4.10
            test
        
        
            org.mockito
            mockito-all
            2.0.2-beta
        
    


    
        spring-web
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.5.1
                
                    1.8
                    1.8
                    UTF-8
                
            
            
            org.apache.maven.plugins
                maven-war-plugin
                2.6
                
                    spring-web
                    false
                
            
        
        
            
                
                
                    org.apache.tomcat.maven
                    tomcat7-maven-plugin
                    2.2
                    
                        8888
                        
                    
                
            
        
    

说明:

  • 这里我们使用注解的方式进行配置,所以将web.xml删除。但是删除之后会出现一个问题,就是web.xml is missing and is set to true。这里需要配置maven-war-plugin这个插件来解决。
  • 项目结构如下:


    13、书中例子说明(补1)(spring笔记)_第1张图片
    1

    相关代码在之前已经给出,只是报名有些变化,实际内容基本不变。下面给出有变化的代码

RootConfig.java

@Configuration
@ComponentScan(basePackageClasses = { SpitterWebInitializer.class }, excludeFilters = {
        @Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfig {

}

WebConfig.java

@Configuration
@EnableWebMvc // 启用Spring MVC
@ComponentScan(basePackageClasses={HomeController.class}) // 启用组件扫描
public class WebConfig extends WebMvcConfigurerAdapter {
......
}
  • 刚开始在webapp目录下有一个index.jsp文件,而tomcat7-maven-plugin插件是这样配置的:

    org.apache.tomcat.maven
    tomcat7-maven-plugin
    2.2
    
      8888
      /
    

然后在使用地址localhost:8888/spring-web/访问的时候就是访问不到,这是因为配置了属性,所以应该使用localhost:8888/直接访问项目。于是这里我先将此属性给注销掉了,然后再次使用前面的地址访问,但是原本应该进入到home.jsp页面,但是却默认访问到了index.jsp。当我把index.jsp删除掉时就能进入到对应的controller中了。一般我们是在web.xml中配置欢迎页面为index.jsp的,但是这里我使用注解,并没有配置此页面,为什么还是默认访问此页面?(不懂)可能是Tomcat默认配置的?

  • 最后在试验的时候可能还需要将工程的web容器版本改为3.0及以上。相关修改方法请查看开发工具文集中的相关笔记。

你可能感兴趣的:(13、书中例子说明(补1)(spring笔记))