zoukankan      html  css  js  c++  java
  • springMVC 框架的xml配置文件的说明

    springMVC框架xml文件配置的说明,直接上代码

    我们介绍四个xml文件配置以及xml内容的理解:application.xml、spring-mvc.xml、pom.xml 和 web.xml

    application.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:context="http://www.springframework.org/schema/context"  
        xmlns:aop="http://www.springframework.org/schema/aop"  
        xsi:schemaLocation="    
               http://www.springframework.org/schema/beans    
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
               http://www.springframework.org/schema/aop    
               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
               http://www.springframework.org/schema/context    
               http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
                   
           <!-- 自动扫描注解的bean -->  
        <context:component-scan base-package="com.oracle.service" />  
                   
    </beans>

    spring为基于注解注入bean提供四个注解,再将组件交给spring来管理,在application.xml文件里,添加自动扫描需要实例化bean的包的路径

    @Component:当对组件的层次难以定位的时候使用这个注解
    @Controller:表示控制层的组件
    @Service:表示业务逻辑层的组件
    @Repository:表示数据访问层的组件
    <context:component-scan base-package="com.oracle.service" /> 

    spring-mvc.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:c="http://www.springframework.org/schema/c"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
            <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->  
        <!-- 支持JSON数据格式 -->
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
            <property name="messageConverters" >  
                <list>  
                    <ref bean="mappingJacksonHttpMessageConverter" />  
                </list>  
            </property>  
        </bean>  
        <bean id="mappingJacksonHttpMessageConverter"  
            class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
        </bean> 
         <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
            <property name="prefix" value="/WEB-INF/views/" />  
            <property name="suffix" value=".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->  
        </bean> 
        <context:component-scan base-package="com.oracle.controller" /> 
        </beans>

    spring-mvc.xml和application.xml也可以合并成一个xml,spring-mvc.xml中配置了Jason数据格式、对视图模型的解析和对controller层的自动扫描(注入bean)

    pom.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>oracle.com</groupId>
      <artifactId>MavenPro</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>MavenPro Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <properties>  
            <!-- spring版本号 -->  
            <spring.version>3.2.8.RELEASE</spring.version>  
            <!-- log4j日志文件管理包版本 -->  
            <slf4j.version>1.6.6</slf4j.version>  
            <log4j.version>1.2.12</log4j.version>  
            <!-- junit版本号 -->  
            <junit.version>4.12</junit.version>  
            <!-- mybatis版本号 -->  
            <mybatis.version>3.2.1</mybatis.version>  
        </properties>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>  
        <!-- 添加Spring依赖 -->  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-core</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-webmvc</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-context</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-context-support</artifactId>  
                <version>${spring.version}</version>  
            </dependency>  
             <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-web</artifactId>  
                <version>${spring.version}</version>  
            </dependency>
            <!--spring单元测试依赖 -->  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-test</artifactId>  
                <version>${spring.version}</version>  
                <scope>test</scope>  
            </dependency>
            <!--添加Jason依赖 --> 
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>1.9.13</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.6.3</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.6.3</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.6.3</version>
            </dependency>     
            
      </dependencies>
      <build>
        <finalName>MavenPro</finalName>
        <plugins>   
              <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                   <configuration>
                      <path>/mavenPro</path>
                      <port>8081</port>
                      <server>tomcat7</server>
                   </configuration>
            </plugin>
        </plugins>
      </build>
    </project>

    pom.xml  文件配置是添加jar包及其所依赖jar包和一些组件(tomcat)

    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" 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_3_0.xsd"
        id="WebApp_ID" version="3.0">
    
     <!-- 加载Spring的配置文件 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application.xml</param-value>
        </context-param>
        
        <listener>
            <description>spring监听器</description>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
      <!-- 防止spring内存溢出监听器 -->
        <listener>
            <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
        </listener>
        <!-- spring mvc servlet -->
        <servlet>
            <description>spring mvc servlet</description>
            <servlet-name>springMvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <description>spring mvc 配置文件</description>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springMvc</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
        <filter>  
            <description>字符集过滤器</description>  
            <filter-name>encodingFilter</filter-name>  
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
            <init-param>  
                <description>字符集编码</description>  
                <param-name>encoding</param-name>  
                <param-value>UTF-8</param-value>  
            </init-param>  
        </filter>  
        <filter-mapping>  
            <filter-name>encodingFilter</filter-name>  
            <url-pattern>/*</url-pattern>  
        </filter-mapping>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file> 
            <welcome-file>index.html</welcome-file> 
        </welcome-file-list>  
    </web-app>

    配置了application.xml和spring-mvc.xml之后,javaEE还不知道这两个文件的存在,web.xml就是实现这样的功能,其中还配置了监听器和过滤器,监听器是监听JavaEE对application.xml和spring-mvc.xml加载情况,过滤器是在启动项目的时候,过滤字符集,防止前后台交互的时候,出现中文乱码。

    再讲一下pom.xml 中插件tomcat的一些配置

    右键项目名,选择“Run As” =>“Run Configuration”,如下图:

     弹出以下窗口,我们配置Main 、JRE和Source这三个即可,在Main中,WorkSpace中选择当前项目,Goal中,填写XXX:run,

     JRE中选择JDK

    Resouce中add“Workspace”,点击应用,点击Run即可。

    运行结果:

     

    在浏览器中输入该地址,即可进入欢迎页了,截图如下:

  • 相关阅读:
    修改root密码
    环境的搭建(四)--------samba服务
    16.统一异常处理(上)编写自定义处理函数
    15.集成限流功能到我们的服务中(花操作必看)
    14.go内置的rate包学习2(有花操作,必看)
    13.go内置的rate包学习1
    12.使用负载均衡的方式调用服务(随机方式)
    11.使用负载均衡的方式调用服务(轮询方式)
    10.根据命令行参数注册多个服务
    9.consul获取服务实例,调用测试
  • 原文地址:https://www.cnblogs.com/WQX-work24/p/9946516.html
Copyright © 2011-2022 走看看