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===

转载于:https://www.cnblogs.com/Garnett-Boy/p/5106492.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值