• springMVC初探--环境搭建和第一个HelloWorld简单项目


    注:此篇为学习springMVC时,做的笔记整理.

    MVC框架要做哪些事情?

      a,将url映射到java类,或者java类的方法上

      b,封装用户提交的数据

      c,处理请求->调用相关的业务处理—>封装响应的数据

      d,将响应的数据进行渲染

    springMVC框架有什么优点?

            性能比struts2好

            简单便捷易学,能进行更简洁的web层开发

            天生和spring无缝集成

            使用约定优于配置

            能够进行简单junit测试

            支持restful风格

            异常处理,本地化国际化,数据验证,拦截器

       提供一套强大的JSP标签库,简化JSP开发

    简单了解springMVC的结构和流程

      这是在网上找到的一张springmvc的结构流程图:

        

    环境搭建和hello springMVC简单案例

    1.配置文件开发(非注解方式)

      a.导入相关jar包 

    commons-logging-1.1.3.jar

    spring-beans-4.2.5.RELEASE.jar

    spring-context-4.2.5.RELEASE.jar

    spring-context-support-4.2.5.RELEASE.jar

    spring-core-4.2.5.RELEASE.jar

    spring-expression-4.3.0.RELEASE.jar

    spring-web-4.2.5.RELEASE.jar

    spring-webmvc-4.2.5.RELEASE.jar

    spring-aop-4.3.0.RELEASE.jar

      b..在web.xml中配置DispatcherServlet

    <!-- 配置分发器Dispatcher -->
      <servlet>
          <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <!-- 随系统启动 -->
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>*.do</url-pattern>
      </servlet-mapping>

      c.添加Springmvc配置文件,默认在WEB-INF目录下添加,springmvc-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"
        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.xsd">
            <!-- 配置HandlerMapping -->
        <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
        <!-- 配置HandleAdapter -->
        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
        <!-- 配置渲染器 -->
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <!-- 结果视图的前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <!-- 结果视图的后缀 -->
            <property name="suffix" value=".jsp"/>
        </bean>
        <!-- 配置请求和处理器 -->
        <bean name="/hello.do" class="com.wang.controler.HelloControler"></bean>
    
    
    </beans>

      d.编写HelloControler.java

    package com.wang.controler;
    
    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 HelloControler implements Controller{
    
        @Override
        public ModelAndView handleRequest(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            ModelAndView mv=new ModelAndView();
            mv.addObject("msg","hello springmvc");
            System.out.println("111");
            mv.setViewName("hello");
            return mv;
        }
        
    }

     在WEB-INF目录下新建hello.jsp,简单写一句表达式${msg},浏览器中测试即可.

      使用注解方式

      a.导jar包(相同)

      b.在web.xml中配置DispatcherServlet(这次配置一个contextConfigLocation参数,以将mvc.xml放在src目录下):

    <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath: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>

      c.在src目录下新建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: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.xsd">
            <!-- 配置渲染器 -->
        <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <!-- 结果视图的前缀 -->
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <!-- 结果视图的后缀 -->
            <property name="suffix" value=".jsp"/>
        </bean>
        <!-- 配置需要扫描的包(重点) -->
        <context:component-scan base-package="com.wang.controller"/>
    </beans>

      d.src目录下新建HelloController.jar(重点):

    package com.wang.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class HelloController {
        @RequestMapping("/hello")
        public ModelAndView hello(HttpServletRequest req,HttpServletResponse res){
            ModelAndView mv=new ModelAndView();
            mv.addObject("msg", "hello world");
            mv.setViewName("hello");
            return mv;
        }
    }

    参考博文http://jinnianshilongnian.iteye.com/blog/1594806

  • 相关阅读:
    java抽象类怎么实例化了及C++中抽象类的构造函数
    树及其遍历
    C++中类的多态与虚函数的使用
    C++中#if #ifdef 的作用
    用链表实现两大数相乘
    C++中的链表类的设计
    IPicture总结
    c++标准库中,含有链表的类list
    TCP/IP、Http的区别
    C语言单链表实现19个功能完全详解
  • 原文地址:https://www.cnblogs.com/fingerboy/p/5682426.html
Copyright © 2020-2023  润新知