zoukankan      html  css  js  c++  java
  • springMVC部署

      一、导入springMVC所需要的jar包

         下载地址:http://repo.spring.io/release/org/springframework/spring/

          

    二、springMVC框架配置

      1、

              1.1 常规web.xml配置

    1  <servlet>
    2       <servlet-name>spring</servlet-name>
    3       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    4       <load-on-startup>1</load-on-startup>
    5   </servlet>
    6   <servlet-mapping>
    7       <servlet-name>spring</servlet-name>
    8       <url-pattern>*.do</url-pattern>
    9   </servlet-mapping>

           1.2 spring配置文件不在WebRoot/WEB-INF下创建时,web.xml 文件配置:

     <!-- spring mvc 核心servlet -->
      <servlet>
          <servlet-name>applicationContext</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          
         <!-- 加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。 -->
             <init-param>
                 <param-name>contextConfigLocation</param-name>
                 <param-value>classpath:applicationContext.xml</param-value>
             </init-param>
         <load-on-startup>1</load-on-startup>
         
      </servlet>
      <servlet-mapping>
          <servlet-name>applicationContext</servlet-name>
          <url-pattern>/</url-pattern>
      </servlet-mapping>

      2、(在WebRoot/WEB-INF下)创建spring配置文件

      注意:此配置文件名称必须为 :servletName-servlet.xml  (servletName即为web.xml中核心servlet的name),此处名称为spring-servlet.xml。 如果不在WebRoot/WEB-INF下创建,则需要在web.xml中作相应配置,否则文件找不到,此时web.xml配置参考 1.2 spring配置文件不在WebRoot/WEB-INF下创建时,web.xml 文件配置。

    spring-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:aop="http://www.springframework.org/schema/aop"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context" 
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
                   http://www.springframework.org/schema/aop 
                   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                   http://www.springframework.org/schema/mvc 
                   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-3.0.xsd
                http://www.springframework.org/schema/tx 
                http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">   
                
          <!--1 MVC注解支持  -->
          <mvc:annotation-driven />
          <!--2 注解扫描目录-->
          <context:component-scan base-package="com.sxdx"></context:component-scan>
          <!--3 处理静态文件 -->
          <mvc:default-servlet-handler />
          
          <!-- 视图解析器 -->
          <!-- <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
               <property name="prefix" value="/pages/" />
               <property name="suffix" value=".jsp" />  
         </bean> -->
         
         
    </beans> 

      3、创建Controller控制器(Action) 

     1 package com.sxdx.hello;
     2 import javax.servlet.http.HttpServletRequest;
     3 import javax.servlet.http.HttpServletResponse;
     4 import org.springframework.stereotype.Controller;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.servlet.ModelAndView;
     7 
     8 @Controller
     9 public class demo {
    10     @RequestMapping(value="/demo1")
    11     public String demo1(HttpServletRequest request,
    12                 HttpServletResponse response){
    13         System.out.println("demo1===");
    14         return "";
    15     }
    16 }

       4、浏览器访问 http://localhost:8080/spring01/demo1.do 后台输出demo1===

  • 相关阅读:
    bzoj 2527: [Poi2011]Meteors 整体二分
    bzoj 2738 矩阵乘法
    bzoj 3110 K大数查询
    bzoj 3262 陌上花开
    cogs 577 蝗灾 CDQ分治
    bzoj 1101 zap
    bzoj 2005
    bzoj 3518 Dirichlet卷积
    bzoj 1257
    最优贸易 [NOIP 2009]
  • 原文地址:https://www.cnblogs.com/Garnett-Boy/p/5106492.html
Copyright © 2011-2022 走看看