Spring 4.2.4.RELEASE MVC 学习笔记 - 4.1(咋个办呢 zgbn)

41 篇文章 0 订阅
20 篇文章 0 订阅

Spring 4.2.4.RELEASE MVC 学习笔记


    我点休息一下放松放松,但还是比较无聊,所以追加4.5小结吧,没别的内容,就是把学习的项目里面一些配置文件的代码放进来,提供给大家参考。

pom.xml

/framework_spring/pom.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <modelVersion>4.0.0</modelVersion>

    <parent>
        <!-- 因为是 _total项目的子项目 -->
        <groupId>cn.vfire.frameword</groupId>
        <artifactId>total</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>framework_spring</artifactId>
    <packaging>war</packaging>
    <name>framework_spring Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <org_springframework_version>4.2.4.RELEASE</org_springframework_version>
    </properties>

    <dependencies>

        <!-- 导入junit jar包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- 导入jsp servlet规范jar包 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>

        <!-- 导入spring框架依赖jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org_springframework_version}</version>
        </dependency>
        <dependency>
            <!-- 该jar用于spring 支持 junit测试 -->
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org_springframework_version}</version>
        </dependency>
        <!-- spring-context-support,该jar引入能使spring mvc非常好的对freemark的支持 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${org_springframework_version}</version>
        </dependency>

        <!-- 导入freemarker视图解析框架jar与sprig集成 -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.22</version>
        </dependency>


    </dependencies>


    <build>

        <finalName>framework_spring</finalName>

        <!-- 修改maven编译输出目录 -->
        <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
        <testOutputDirectory>src/main/webapp/WEB-INF/classes</testOutputDirectory>


        <plugins>
            <!-- 添加一个mavne的插件,作用是在我通过maven发布的时候,能将依赖的jar复制一份到我指定的目录下。(个人比较懒) -->
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-lib-src-webapps</id>
                        <phase>package</phase>
                        <configuration>
                            <tasks>
                                <delete dir="src/main/webapp/WEB-INF/lib" />
                                <copy todir="src/main/webapp/WEB-INF/lib">
                                    <fileset dir="${project.build.directory}\${project.build.finalName}\WEB-INF\lib">
                                        <include name="*" />
                                    </fileset>
                                </copy>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

web.xml

/framework_spring/src/main/webapp/WEB-INF/web.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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="framework.spring" version="2.5">

    <display-name>framework_spring</display-name>

    <!-- 加载Spring容器配置,设置Spring容器加载配置文件路径 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring/applicationContext-spring-*.xml</param-value>
    </context-param>

    <!-- Spirng 全局请求字符集过滤器 -->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置spring mvc 支持 -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/spring/spring-mvc-*.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

applicationContext-spring-config.xml

/framework_spring/src/main/resources/config/spring/applicationContext-spring-config.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="   
           http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-4.2.xsd  
           http://www.springframework.org/schema/mvc   
           http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

</beans>

spring-mvc-servlet.xml

/framework_spring/src/main/resources/config/spring/spring-mvc-servlet.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="   
           http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-4.2.xsd  
           http://www.springframework.org/schema/mvc   
           http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <!-- 开启spring 上下文注解支持 -->
    <context:annotation-config />

    <!-- 把标记了@Controller注解的类转换为bean -->
    <context:component-scan base-package="cn.vfire.framework" />

    <!-- 开启spring mvc注解支持 -->
    <mvc:annotation-driven />

    <!-- 如果当前请求为“/”时,则转发到“/helloworld/index” -->
    <mvc:view-controller path="/" view-name="forward:/index.jsp" /> 

    <!-- 设置默认的Servlet来响应静态文件 -->
    <mvc:resources mapping="/resource" location="/resource" />

    <!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 -->
    <mvc:default-servlet-handler />

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

</beans>

spring-mvc-freemarker.xml

/framework_spring/src/main/resources/config/spring/spring-mvc-freemarker.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="   
           http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-4.2.xsd  
           http://www.springframework.org/schema/mvc   
           http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/views/ftl" />
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">0</prop>
                <prop key="default_encoding">UTF-8</prop>
                <prop key="number_format">0.##########</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="classic_compatible">true</prop>
                <prop key="template_exception_handler">ignore</prop>
            </props>
        </property>
    </bean>

    <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean" />

</beans>

spring-mvc-view-resolver.xml

/framework_spring/src/main/resources/config/spring/spring-mvc-view-resolver.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="   
           http://www.springframework.org/schema/beans   
           http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   
           http://www.springframework.org/schema/context   
           http://www.springframework.org/schema/context/spring-context-4.2.xsd  
           http://www.springframework.org/schema/mvc   
           http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">

    <!-- 默认视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="2" />
        <property name="contentType" value="text/html; charset=UTF-8" />
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- FreeMarker视图解析器,使用FreeMarker视图解析器时,不需要对prefix属性做配置,如果配置了prefix返回会有500或406错误。-->
    <!-- 因为在FreeMarker配置时已经指定了模版存放目录。 -->
    <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="order" value="0" />
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
        <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
        <property name="cache" value="true" />
        <property name="suffix" value=".ftl" />
        <property name="contentType" value="text/html; charset=UTF-8" />
    </bean>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值