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>

最新文章

  1. 关于Visual Studio 未能加载各种Package包的解决方案
  2. 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)
  3. vs快捷键大全
  4. gdb调试方法
  5. print_r、echo、var_dump三者的区别
  6. PHP读文件的一个乱码问题
  7. 多线程基础 (八)NSOperation相关
  8. SSN 社会安全号码
  9. &lt;转&gt;linux 下stm32开发环境安装
  10. onitemcommand 的作用以及onitemdatabound的具体作用
  11. servlet中web.xml配置
  12. Jquery的学习:基础核心!
  13. 查看numpy.ndarray的数据类型
  14. 天梯赛-L1-018. 大笨钟
  15. Word设置多级标题
  16. 斯坦福大学公开课机器学习:梯度下降运算的特征缩放(gradient descent in practice 1:feature scaling)
  17. tensorflow的assgin方法
  18. 使用css实现移动端导航条滚动
  19. dpkg卸载deb包
  20. 第三课:JAVA反射机制

热门文章

  1. Ubuntu Kylin 14.04LTS 开机后卡在登陆界面,可以进入字符界面,或者登陆后鼠标不显示但是管用
  2. eclipse导入tomcat源码
  3. 对比Node.js和Python 帮你确定理想编程解决方案!
  4. WOJ 1542 Countries 并查集转化新点+最短路
  5. 吴裕雄--天生自然 JAVASCRIPT开发学习: 闭包
  6. MySQL索引(一)
  7. jsp的appilication.getInitParameter()方法无法获取到值的问题
  8. h5-渐变的基本描述
  9. EOF是什么?(笔记)
  10. 深入分析Java反射(六)-反射调用异常处理