zoukankan      html  css  js  c++  java
  • 02SpringMvc_springmvc快速入门小案例(XML版本)

    这篇文章中,我们要写一个入门案例,去整体了解整个SpringMVC。

    先给出整个项目的结构图:

        第一步:创建springmvc-day01这么一个web应用

        第二步:导入springioc,springweb , springmvc相关的jar包

        第三步:在/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="WebApp_ID" version="2.5">
      <display-name>SpringMvc_10day_self</display-name>
      <servlet>
      <!--这个名字可以随便取得,但是这个名字取了之后,以后在 WEB-INF下面创建SpirngMVC的配置文件是,命名必须以这个开头,
      
      所以这里取名叫做DispatcherServlet,那么之后的xml文件取名必须为DispatcherServlet-servlet.xml(一个字都不能差)
      
      -->
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
     </servlet>
     <servlet-mapping>
       <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
     
     </servlet-mapping>
     
      <welcome-file-list>
       
        <welcome-file>index.jsp</welcome-file>
      
      </welcome-file-list>
    </web-app>

    第四步:创建HelloAction控制器类:

    /**
     * Create by 沈晓权
     * Create on 2016年8月6日下午2:01:49
     */
    package com.guigu.shen.Action;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    
    public class HelloAction implements Controller {
    
    
        @Override
        public ModelAndView handleRequest(HttpServletRequest arg0,
                HttpServletResponse arg1) throws Exception {
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("message","这是我得第一个"
                + "SpringMvc应用程序");
        modelAndView.setViewName("/jsp/success.jsp");
        return modelAndView;
        
            
            
        
        }
    
    }

    第五步:在/WebRoot/下创建jsp/success.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        Success. <br>
        ${message} 
      </body>
    </html>

    第六步:在/WEB-INF/创建DispatcherServlet-servlet.xml配置文件,xml头部信息与spring.xml相同

           注意:该配置文件的命名规则:web.xml文件中配置的<servlet-name>的值-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:context="http://www.springframework.org/schema/context"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xsi:schemaLocation="
          
          http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-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/tx
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    >
         <!-- 控制器(程序员) -->
    <bean name="/hello.action" class="com.guigu.shen.Action.HelloAction"></bean>
      






    <!-- 映射器(框架)
    BeanNameUrlhanderMapping的作用是将上面那个bean标签的name属性当做URL请求,
    这个映射器配不配都可以,就算不配,系统也会自己去创建的
    --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>







    <!-- 适配器(框架)
    SimpleControllerHandlerAdapter的作用是查找实现了Colltroler接口的Action类
    
    
      这个适配器配不配都可以,就算不配,系统也会自己去创建的
    -->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>






    <!-- 视图解析器(框架)

    InternalResourceViewResolver的作用是通过ModelAndView对象中封装的视图名找到真正的jsp页面路径。比如说
    modelAndView.setViewName("/jsp/success.jsp")这里只是封装了一个"/jsp/success.jsp"这么一个字符串,就由InternalResourceViewResolver
    去把他解析成真正的路径。
    
    
    这个适配器配不配都可以,就算不配,系统也会自己去创建。
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean> </beans>

    最后一步:

    在地址栏输入:http://127.0.0.1:8080/SpringMvc_10day_self/hello.action

    运行结果如下:

    以上就是一个入门小案例。

    思考一些问题:SpringMVC创建的Action是单例模式呢还是什么?

       验证一下:

    修改代码如下:

    /**
     * Create by 沈晓权
     * Create on 2016年8月6日下午2:01:49
     */
    package com.guigu.shen.Action;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    /*
     * Create by 沈晓权
     * Create on 2016年8月6日下午2:01:49
     */
    public class HelloAction implements Controller {
    /**
     * 
     */
    public HelloAction() {
    
    System.out.print("创建了一个Action");
    }
        @Override
        public ModelAndView handleRequest(HttpServletRequest arg0,
                HttpServletResponse arg1) throws Exception {
            System.out.println("this is HelloAction handleRequest");
            ModelAndView modelAndView=new ModelAndView();
             modelAndView.addObject("message","这是我得第一个"
                + "SpringMvc应用程序");
        modelAndView.setViewName("/jsp/success.jsp");
        return modelAndView;
        
        }
    
    }

    运行结果为:

    创建了一个Action    2016-8-6 15:25:22 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler

    信息: FrameworkServlet 'DispatcherServlet': initialization completed in 523 ms
    this is HelloAction handleRequest
    this is HelloAction handleRequest
    this is HelloAction handleRequest

    运行结果表明这就是一个单例模式。

    构造器只会走一次,就是只创建一个对象,但是public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1)这个方法就会执行n多次。

  • 相关阅读:
    # 20172307 2017-2018-2 《程序设计与数据结构》实验4报告
    20172307 2017-2018-2 《程序设计与数据结构》实验3报告
    20172307 2017-2018-2 《程序设计与数据结构》第10周学习总结
    软件工程之美40讲——最佳实践:小团队如何应用软件工程?
    软件工程之美39讲——项目总结:做好项目复盘,把经验变成能力
    软件工程之美38讲——日志管理:如何借助工具快速发现和定位产品问题 ?
    软件工程之美37讲——遇到线上故障,你和高手的差距在哪里?
    软件工程之美35讲——版本发布:软件上线只是新的开始
    软件工程之美36讲——DevOps工程师到底要做什么事情?
    20172330 2018-2019-1 《程序设计与数据结构》实验一报告
  • 原文地址:https://www.cnblogs.com/shenxiaoquan/p/5744296.html
Copyright © 2011-2022 走看看