SpringMVC 是现在广泛应用的框架结构,我也只是一个初学者,一遍学习一遍梳理整合,如有错误,希望大神指点,别误人。

MVC :Model-View-Control

框架性质的C 层要完成的主要工作:封装web 请求为一个数据对象、调用业务逻辑层来处理数据对象、返回处理数据结果及相应的视图给用户。

现在我们先说一下Spring的插件安装,我用的是Eclipse4.5.2版本

如果你不知道自己用的什么版本的eclipse,在你eclipse的安装目录,打开:

(直接拖到浏览器上就可以打开)

然后打开eclipse,如图:

 

 

在这个地址中填上:http://dist.springsource.com/release/TOOLS/update/e4.5.2/

在4.5.2处改成你自己的版本就好。安装好,创建文件的时候就会有这个:

(一会儿会用到)

创建个动态项目工程,导入以下几个jar包:

然后,我们需要在WEB-INF文件下创建一个上面的那个.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:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        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-4.0.xsd">
    
    <context:component-scan base-package="com.ysu.controller"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

然后我们再配置以下web.xml文件,和配置servlet很像:

<servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--     <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param> -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

下面是我练习的一个例子,包含Ant风格及PathVariable变量、rest请求风格、POST请求转化为PUT请求。上传了一个.java 文件一个jsp文件:

package com.ysu.controller;

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

@Controller
public class AController {
    private static final String SUCCESS="success";
    
    @RequestMapping(value="/testAnt/{id}")
    public String testAnt(@PathVariable(value="id") Integer id){
        System.out.println("++++++++++"+id);
        
        return SUCCESS;
    }
    
    @RequestMapping("/sayHello")
    public String sayHello(){
        return SUCCESS;
    }
    @RequestMapping(value="/order/{id}",method=RequestMethod.GET)
    public String getOrderById(@PathVariable(value="id") Integer id){
        System.out.println("----------"+id);
        return SUCCESS;
        
    }
    @RequestMapping(value="/order/{id}",method=RequestMethod.POST)
    public String getOrderById1(@PathVariable(value="id") Integer id){
        System.out.println("~~~~~~~~~~"+id);
        return SUCCESS;
        
    }
    @RequestMapping(value="/order/{id}",method=RequestMethod.PUT)
    public String getOrderById2(@PathVariable(value="id") Integer id){
        System.out.println("************"+id);
        return SUCCESS;
    
        
    }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <a href="${pageContext.request.contextPath}/sayHello">GoGoGo</a>
    <a href="${pageContext.request.contextPath}/testAnt">GoGoGo</a>
    <a href="${pageContext.request.contextPath}/order/123">GetGoGoGo</a>
    <form action="${pageContext.request.contextPath}/order/123" method="post">
        <input type="submit" value="submit"/>
    </form>
    <form action="${pageContext.request.contextPath}/order/123" method="post">
        <input type="submit" value="submit"/>
        <input type="hidden" name="_method" value="put"/>
    </form>
</body>
</html>