zoukankan      html  css  js  c++  java
  • SpringMvc_Hello,SpringMVC!案例

    step1.导包: spring-webmvc

    step2.添加spring配置文件。

    step3.配置DispatcherServlet。(web.xml)

      <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

      <!--
              初始化参数:用于指定spring配置文件的位置和文件名。
              注:
                  DispatcherServlet的初始化方法会启动Spring容器,所以需要知道spring
                  的配置文件的位置。
           -->
          <init-param>
              <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>

    step4.写Controller。

    public class HelloController implements Controller {

        public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
            System.out.println("handleRequest()");
            /*
             * ModelAndView有两个常用的构造器:
             * ModelAndView(String viewName)
             * ModelAndView(String viewName,Map data)
             * 注:
             *     viewName:视图名。
             * data:数据
             */
            return new ModelAndView("hello");
        }
        
    }

    step5.写jsp。

    <h1>Hello,SpringMVC!</h1>

    step6.配置HandlerMapping和ViewResolver。

    <?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:jdbc="http://www.springframework.org/schema/jdbc"  
        xmlns:jee="http://www.springframework.org/schema/jee"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
        
        <!-- 配置HandlerMapping -->
        <!-- 告诉DispatcherServlet(前端控制器)请求路径与Controller的对应关系 -->
        <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/hello.do">hc</prop>
                </props>
            </property>
        </bean>
        <!-- 配置Controller(处理器 )-->
        <bean id="hc" class="controller.HelloController"></bean>
        <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        
        
    </beans>

  • 相关阅读:
    ~/.fvwm/.fvwm2rc
    我的.Xresources
    getopt得用法
    C语言编程好习惯(持续更新)
    关于stm32的USB学习笔记之usbcore.c
    Fvwm.desktop内容
    XP下JDK不能安装的解决办法
    以后网络上的好东东,在这里加个链接,呵呵!太丰富了!
    ISO/OSI七层参考模型
    VC 和 MFC 的一些常见问题
  • 原文地址:https://www.cnblogs.com/LWQ168/p/7406157.html
Copyright © 2011-2022 走看看