• SpringMVC(2)—SpringMVC整合Spring的HelloWorld


    一、这是一个SpringMVC框架的案例HelloWorld
    功能:HelloWorld

    二、SpringMVC运行流程
    1.流程

        请求——>springDispatcherServlet的url-pattern检查SpringMVC中是否存在对应的映射
        无:检查是否配置的<mvc:default-servlet-handler/>
            无:404, 控制台打印 No mapping found for HTTP request with URI[/xx/xx] in DispatcherServlet
            有:寻找目标资源
        有:——>HandlerMapping获取HandlerExceptionChain对象——>获取HandlerAdapter对象——>调用拦截器的PreHandle方法
    ——>调用目标Handler的目标方法得到ModelAndView对象——>调用拦截器的postHandle方法,有无异常:
        有:由HandlerExceptionResolver组件处理异常,得到新的ModelAndView对象,——>无:———>由ViewResolver组件根据ModelAndView对象得到实际的view——>渲染视图——>调用拦截器的afterCompletion方法 
    

    三、在Spring的环境下使用SpringMVC
    问题:1.需要进行Spring整合SpringMVC吗
    2.还是否需要加入SpringIOC容器
    3.是否还要在web.xml文件中配置启动Spring IOC容器的的ContextLoaderListener
    答案:
    1. 需要: 通常情况下, 类似于数据源, 事务, 整合其他框架都是放在 Spring 的配置文件中(而不是放在 SpringMVC 的配置文件中).
    实际上放入 Spring 配置文件对应的 IOC 容器中的还有 Service 和 Dao.
    2. 不需要: 都放在 SpringMVC 的配置文件中. 也可以分多个 Spring 的配置文件, 然后使用 import 节点导入其他的配置文件

    问题:
    若 Spring 的 IOC 容器和 SpringMVC 的 IOC 容器扫描的包有重合的部分, 就会导致有的 bean 会被创建 2 次.
    答案:
    方法1. 使 Spring 的 IOC 容器扫描的包和 SpringMVC 的 IOC 容器扫描的包没有重合的部分,若分模块开发这种方法可能会不太合适
    方法2. 使用 exclude-filter 和 include-filter 子节点来规定只能扫描的注解

    所以使用方法2来解决SpringMVC整合Spring时,出现一个注解被两次扫描的问题
    步骤
    1.在SpringMVC配置文件中,配置扫描@Controller这个注解
    2.在Spring配置文件中配置不扫描@Controller这个注解

    注意:
    **SpringMVC 的 IOC 容器中的 bean 可以来引用 Spring IOC 容器中的 bean.
    返回来呢 ? 反之则不行. Spring IOC 容器中的 bean 却不能来引用 SpringMVC IOC 容器中的 bean!**

    四、代码
    1.添加jar包

    classmate-0.8.0.jar
    com.springsource.net.sf.cglib-2.2.0.jar
    com.springsource.org.aopalliance-1.0.0.jar
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    commons-fileupload-1.2.1.jar
    commons-io-2.0.jar
    commons-logging-1.1.1.jar
    commons-logging-1.1.3.jar
    hibernate-validator-5.0.0.CR2.jar
    hibernate-validator-annotation-processor-5.0.0.CR2.jar
    jackson-annotations-2.1.5.jar
    jackson-core-2.1.5.jar
    jackson-databind-2.1.5.jar
    jboss-logging-3.1.1.GA.jar
    jstl.jar
    spring-aop-4.0.0.RELEASE.jar
    spring-aspects-4.0.0.RELEASE.jar
    spring-beans-4.0.0.RELEASE.jar
    spring-context-4.0.0.RELEASE.jar
    spring-core-4.0.0.RELEASE.jar
    spring-expression-4.0.0.RELEASE.jar
    spring-jdbc-4.0.0.RELEASE.jar
    spring-orm-4.0.0.RELEASE.jar
    spring-tx-4.0.0.RELEASE.jar
    spring-web-4.0.0.RELEASE.jar
    spring-webmvc-4.0.0.RELEASE.jar
    standard.jar
    validation-api-1.1.0.CR1.jar

    2.页面
    index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    
        <a href="helloworld">Hello World</a>
    
    </body>
    </html>

    success.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h3>Hello World!</h3>
    </body>
    </html>

    other.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h3>Other page!</h3>
    </body>
    </html>

    3.Controller
    HelloWorld.java

    package com.Spring.mvc;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloWorld {
    
        @Autowired
        private UserService userService;
    
        public HelloWorld() {
            System.out.println("Hello World Constructor..."); 
        }
    
    
        @RequestMapping("/helloworld")
        public String hello(){
            System.out.println("Hello World!");
            return "success";
        }
    
    
    }
    

    4.Service
    UserService.java

    package com.Spring.mvc;
    
    import org.springframework.stereotype.Service;
        //SpringMVC 的 IOC 容器中的 bean 可以来引用 Spring IOC 容器中的 bean,反之则不行
        /*@Autowired
        private HelloWorld helloWorld;*/
    @Service
    public class UserService {
    
        public UserService() {
            System.out.println("UserService Constructor...");
        }
    }
    

    5.配置文件
    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">
      <display-name>Demo20_SpringMVC_HelloWorld</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    
    <!-- 1.配置Spring的配置文件 -->  
    
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:bean.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    
    <!-- 2.配置SpringMVC的配置文件 --> 
      <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    </web-app>

    bean.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    <!-- Spring IOC 容器 -->
    
    <!-- 1.自动扫描的包 -->
        <!-- <context:component-scan base-package="com.Spring.mvc"></context:component-scan> -->
        <context:component-scan base-package="com.Spring.mvc" >
            <context:exclude-filter type="annotation" 
                expression="org.springframework.stereotype.Controller"/>
            <context:exclude-filter type="annotation" 
                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
        </context:component-scan>
    
    <!-- 2.配置数据源,整合其他框架,事务等  -->    
    
    
    </beans>
    

    springmvc.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:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    <!-- Spring MVC的IOC容器 -->
    
    <!-- 1.自动解析的包 -->
        <!-- <context:component-scan base-package="com.Spring.mvc"></context:component-scan> -->
        <context:component-scan base-package="com.Spring.mvc" use-default-filters="false">
            <context:include-filter type="annotation" 
                expression="org.springframework.stereotype.Controller"/>
            <context:include-filter type="annotation" 
                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
        </context:component-scan>
    
    
    
    <!-- 2.视图解析器 -->    
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    
    <!-- 3.为了能够访问,没有Mapping映射的页面,添加了<mvc:default-servlet-handler/>
        但是使用了这个配置之后@RequestMapping映射作用失效,所以又配置这个<mvc:annotation-driven></mvc:annotation-driven>
    --> 
        <mvc:default-servlet-handler/>
        <mvc:annotation-driven></mvc:annotation-driven>
    
    
    <!-- 4.为了整合Spring IOC容器,需要在web.xml中配置启动Spring IOC容器的Listener -->    
    
    
    </beans>
    
  • 相关阅读:
    SCSI contrller的几种类型的区别
    ScaleIO与XtremSW Cache如何集成呢?
    如何强制使用某一大小的包去ping某个IP地址?
    如何查看ETW Trace?
    图像卷积与滤波的一些知识点(转)
    tensorflow serving 编写配置文件platform_config_file的方法
    python在linux的报错集锦
    某些数组和字符串类型转换(转)
    系统安装-007 CentOS7yum源添加、删除及其yum优化(转)
    Error:Failed to resolve: android.arch.core:common:1.1.0
  • 原文地址:https://www.cnblogs.com/tengpengfei/p/10453965.html
Copyright © 2020-2023  润新知