spring 入门案列

来源:互联网 发布:php server 编辑:程序博客网 时间:2024/06/10 07:36

http://download.csdn.net/download/qq_34125999/9970998  所需要的 包



  1 案列视图  

       

2 配置 web.xml

<!-- 配置  Spring MVC 的前端控制器,DispatcherServlet 用来拦截用户的请求 -->
  <servlet>
        <!-- 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:spring/spring-config.xml</param-value>
        </init-param>
        <!-- 启动时立即加载 -->
        <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- 映射声明 -->
  <servlet-mapping>
     <!-- 请求对应的名称 -->
     <servlet-name>springmvc</servlet-name>
     <!-- 监听所有的请求 -->
     <url-pattern>/</url-pattern>
  </servlet-mapping>

3 配置 spring-config

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd ">

    <!--自动扫面 com.demo.controller 下的类,看是否有相关的注解类    -->
    <context:component-scan base-package="com.demo.controller"/>


    <!-- 配置视图解析器-->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>


4 写测试 controller


package com.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class test {

    @RequestMapping(value="/hello")
    public String hello(){

        System.out.println("hello");
        return "index";
    }

}


5 运行


 


原创粉丝点击