美文网首页
13、书中例子说明(补1)(spring笔记)

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

作者: yjaal | 来源:发表于2017-05-29 10:00 被阅读42次

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

首先给出pox.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>win.iot4yj</groupId>
    <artifactId>spring-web</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringInAction Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <!-- 指定Maven仓库 -->
    <repositories>
        <!-- 这是maven中央仓库 -->
        <repository>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <!-- 下面两个是阿里提供的私服 ,可能速度会快点 -->
        <repository>
            <id>alibaba-opensource</id>
            <name>alibaba-opensource</name>
            <url>http://code.alibabatech.com/mvn/releases/</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>alibaba-opensource-snapshot</id>
            <name>alibaba-opensource-snapshot</name>
            <url>http://code.alibabatech.com/mvn/snapshots/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>


        <!-- spring4 -->
        <!-- aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <!-- spring核心包 -->
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <!-- spring上下文,必须 -->
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <!-- 下面三个是基本web项目都需要的 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- 测试所需 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>2.0.2-beta</version>
        </dependency>
    </dependencies>


    <build>
        <finalName>spring-web</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warname>spring-web</warname>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <port>8888</port>
                        <!-- <path>/</path> -->
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

说明:

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


    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插件是这样配置的:
<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
      <port>8888</port>
      <path>/</path>
    </configuration>
</plugin>

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

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

相关文章

网友评论

      本文标题:13、书中例子说明(补1)(spring笔记)

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