通常,模型负责封装应用程序数据在视图层展示。视图仅仅只是展示这些数据,不包含任何业务逻辑。控制器负责接收来自用户的请求,并调用后台服务(manager或者dao)来处理业务逻辑。处理后,后台业务层可能会返回了一些数据在视图层展示。控制器收集这些数据及准备模型在视图层展示。MVC模式的核心思想是将业务逻辑从界面中分离出来,允许它们单独改变而不会相互影响。
搭建springmvc,首先在web.xml中配置:
<!-- 配置请求处理由springmvc框架处理 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 创建springmvc容器的配置文件,否则默认去WEB-INF/springmvc-servlet.xml (<servlet-name>-servlet.xml)-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 浏览器不支持put,delete等method,由该filter将/blog?_method=delete转换为标准的http delete方法 -->
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
接着配置springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.yc.springmvc"/>
</beans>
客户端:
<%@ 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>hello SpringMVC</title>
</head>
<body>
<h2>Hello SpringMVC+restful</h2>
<form action="user/1234" method="get">
<button> GET REQUEST</button>
</form><br>
<form action="user?name=admin&&pass=a" method="post">
<button> POST REQUEST</button>
</form><br>
<form action="user/1234?name=admin&&pass=123" method="post">
<input type="hidden" name="_method" value="PUT"/>
<button> PUR REQUEST</button>
</form><br>
<form action="user/1234?name=admin&&pass=123" method="post">
<input type="hidden" name="_method" value="DELETE"/>
<button> DELETE REQUEST</button>
</form>
</body>
</html>
再然后我们就是编写控制层的代码:
@Controller("helloHandler")
@RequestMapping("user") //请求资源的前缀
public class HelloHandler {
//取数据
@RequestMapping(value="{id}",method=RequestMethod.GET)
public String methodGet(@PathVariable("id")String id){
System.out.println("取到参数id==>"+id);
return "/success.jsp";
}
//插入数据
@RequestMapping(value="",method=RequestMethod.POST)
public String methodPost(User user){
System.out.println("取到参数user==>"+user.toString());
return "/success.jsp";
}
//更新数据put
@RequestMapping(value="{id}",method=RequestMethod.PUT)
public String methodPut(@PathVariable("id")String id , User user){
System.out.println("取到参数user==>"+user.toString());
return "/success.jsp";
}
//删除数据deletes
@RequestMapping(value="{id}",method=RequestMethod.DELETE)
public String methodDELETE(@PathVariable("id")String id, User user){
System.out.println("取到参数ID==>"+id);
return "/success.jsp";
}
}
Restful:一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件。符合REST约束风格和原则的应用程序或设计就是RESTful。
请求路径 请求方法 作用
-/user/1 HTTP GET 得到id为1的user
-/user/1 HTTP DELETE 删除id为1的user
-/user/1 HTTP PUT 更新id为1的user
-/user HTTP POST 新增user
restful的准则或者说要求为:
1.网络上的所有事物都被抽象为资源(resource);
2.每个资源对应一个唯一的资源标识(resource identifier);(URL)
3.通过通用的连接器接口(generic connector interface)对资源进行操作;(HTTP)
4.对资源的各种操作不会改变资源标识;
5.所有的操作都是无状态的(stateless)。
这里控制层代码我是用Restful风格,体现在
1.利用@RequestMapping 指定要处理请求的URI模板和HTTP请求的动作类型
2.利用@PathVariable将URI请求模板中的变量映射到处理方法参数上
3.利用Ajax,在客户端发出PUT、DELETE动作的请求