教你一步步搭建ssm框架,第一步实现springmvc下的页面跳转 - 2018
原创    java_world    发表于:2018-04-25 17:14:24
  阅读 :1.8k+   收藏   编辑

第一步:先实现springmvc下的页面跳转

第一:eclipse创建maven 项目,打开pom.xml先添加相关依赖,

本着少补的思想,启动时,缺啥补啥,ssm框架,无非就spring,mybaits,commons,mysql,json,log,http,c3p0等这些依赖先加上,

参考如下:

<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>com.xx</groupId>
    <artifactId>app</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>app Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- mysql s -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.17</version>
        </dependency>
        <!-- mysql s -->

        <!-- spring s -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <!-- spring e -->


        <!-- common s -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>commons-jxpath</groupId>
            <artifactId>commons-jxpath</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.3.3</version>
        </dependency>

        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.6</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.3</version>
        </dependency>


        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
        </dependency>


        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.10</version>
        </dependency>
        <!-- common e -->

        <!-- json s -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.1</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.7</version>
        </dependency>

        <!-- json e -->


        <!-- log s -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <!-- log e -->


        <!-- http s -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient-cache</artifactId>
            <version>4.3.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.5</version>
        </dependency>
        <!-- http e -->

        <!-- c3p0 s -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>mchange-commons-java</artifactId>
            <version>0.2.10</version>
        </dependency>
        <!-- c3p0 e -->

        <!-- 其他 s -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>
        <!-- 其他 e -->


    </dependencies>
    <build>
        <finalName>app</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port><!-- 端口号 -->
                    <path>/app</path><!-- 访问项目路径 -->
                    <uriEncoding>utf-8</uriEncoding>
                    <server>tomcat7</server>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

第二:classpath创建spring_context.xml ,spring_mvc_context.xml,res.properties,log4j.xml,mapping_spring.xml相关配置文件

log4j.xml:记录日志
res.properties:配置文件
spring_context.xml:spring环境统一入口
spring_mvc_context.xml:springmvc环境统一入口

为了是整体结构可读,我们使用spring的import将mapping_spring.xml文件导入过来

参考如下:

spring_context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
            http://www.springframework.org/schema/cache
            http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="locations">
              <list>
                <value>classpath:res.properties</value>
            </list>
          </property>
    </bean>

    <context:annotation-config />

     <!-- 配置扫描路径 -->
     <context:component-scan base-package="com.faceghost.app">
         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
     </context:component-scan>



</beans>

spring_mvc_context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
            http://www.springframework.org/schema/cache
            http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 启用MVC注解 -->
    <mvc:annotation-driven />

    <!--对静态资源文件的访问 -->
    <mvc:resources mapping="/statics/**" location="/statics/" />

    <!-- 指定Sping组件扫描的基本包路径 -->
    <context:component-scan base-package="com.faceghost.app.controller"> 
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <bean id="mappingJacksonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->
            </list>
        </property>
    </bean>



    <!-- 文件上传 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8" />
        <property name="maxUploadSize" value="1048576000000" />
        <property name="maxInMemorySize" value="40960" />
    </bean>

    <!-- spring url mapping -->
    <import resource="spring/mapping_spring.xml" />


</beans>

res.properties

空文件

log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <!-- ===================================================================== -->
    <!-- 定义全局输出适配器 -->
    <!-- ===================================================================== -->
    <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}]-[%c]-[%t] %x - %m%n" />
        </layout>
    </appender>

    <appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="/usr/local/serv.log" />
        <param name="Append" value="true" />
        <param name="DatePattern" value="yyyy-MM-dd'.log'" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m%n" />
        </layout>
    </appender>

    <!-- Mybatis and SQL -->
    <logger name="org.apache.ibatis" additivity="true">
        <level value="DEBUG" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="org.apache.ibatis.io" additivity="true">
        <level value="ERROR" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="org.apache.ibatis.logging" additivity="true">
        <level value="ERROR" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="org.quartz" additivity="true">
        <level value="ERROR" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="org.mybatis.spring" additivity="true">
        <level value="ERROR" />
        <appender-ref ref="consoleAppender" />
    </logger>
    <logger name="java.sql" additivity="true">
        <level value="DEBUG" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="java.sql.Connection" additivity="true">
        <level value="DEBUG" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="org.apache.http" additivity="true">
        <level value="ERROR" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="java.sql.Statement" additivity="true">
        <level value="DEBUG" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="java.sql.PreparedStatement" additivity="true">
        <level value="DEBUG" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <!-- Spring -->
    <logger name="org.springframework" additivity="true">
        <level value="ERROR" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="org.aspectj" additivity="true">
        <level value="ERROR" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="com.mchange.v2" additivity="true">
        <level value="ERROR" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="net.sf.ehcache" additivity="true">
        <level value="ERROR" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <logger name="org.apache.shiro" additivity="true">
        <level value="ERROR" />
        <appender-ref ref="consoleAppender" />
    </logger>


    <!-- ===================================================================== -->
    <!-- 根日志记录器 -->
    <!-- ===================================================================== -->
    <root>
        <level value="DEBUG" />
        <appender-ref ref="consoleAppender" />
        <!-- <appender-ref ref="file"/> -->
    </root>
</log4j:configuration>

mapping_spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
            http://www.springframework.org/schema/cache
            http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <!-- viewResolver -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/ui/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="viewMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >

        <property name="defaultHandler">  
           <!--  UrlFilenameViewController 会将 "/index" 这样的请求映射成名为 "index" 的视图   -->
            <bean name="index" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />  
        </property>   
    </bean>


</beans>

第三:接下来,就是web.xml,添加初始化的一些参数,加载spring_context.xml,spring_mvc_context.xml文件,

参考如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
        <context-param>
            <param-name>webAppRootKey</param-name>
            <param-value>app.root</param-value>
        </context-param>

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring_context.xml</param-value>
        </context-param>
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>classpath:log4j.xml</param-value>
        </context-param>

        <!-- 定义log -->
        <listener>
            <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>

        <!-- 定义springcontext -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

       <!-- 设置编码 -->
        <filter>
            <filter-name>encoding</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

        <!-- 定义springmvc -->
        <servlet>
            <servlet-name>springContext</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring_mvc_context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springContext</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>


        <!-- 定义超时时间 -->
        <session-config>
            <session-timeout>30</session-timeout>
        </session-config>

</web-app>

第四:注意mapping_spring.xml,我们将统一的请求都转发到/WEB-INF/ui/下,所有,在WEB-INF目录下,我们

创建ui目录,并创建一个hi.jsp ,随便写点东西,
在mapping_spring.xml配置文件中,我们还配置了SimpleUrlHandlerMapping,它会将 “/hi” 这样的请求映射成名为 “index” 的视图,
也只直接访问/WEB-INF/ui/的hi文件,

第五:启动maven项目,浏览器访问http://localhost:8080/app/hi

看是不是显示hi.jsp页面的内容

评论
条评论