SpringMVC之Controler层开发
controler层作用:
- 根据前端请求(url)从后端找到对应的资源进行显示(或业务操作逻辑进行操作),将view层和model层串联起来的“桥梁”。
步骤:
- 根据需求设计需要的URL(根据Restful规范)
- 配置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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置spring mvc -->
<!--1,开启springmvc注解模式
a.自动注册DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
b.默认提供一系列的功能:数据绑定,数字和日期的format@NumberFormat,@DateTimeFormat
c:xml,json的默认读写支持-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--2.静态资源默认servlet配置-->
<!--
1).加入对静态资源处理:js,gif,png
2).允许使用 "/" 做整体映射
-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--3.配置JSP 显示ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 4.扫描web相关的controller -->
<context:component-scan base-package="cn.xiaofang.web"></context:component-scan>
</beans>
- 对每个URL地址设计方法,调用指定的service层的函数
- 使用Spring MVC注解方式来给方法指定对应的URL(@RequestMapping()、@ResponseBody)
控制层设计技巧:
- Restful接口运用(规范设计URL)
- SpringMVC使用技巧(通过注解实现URL与静态资源和业务方法之间的映射)
- 前端交互分析过程
细节:
应用的概念介绍:
- Restful