-
SpringMVC运行流程查看全部
-
url设计列子查看全部
-
web.xml
Servlet 容器與 Spring MVC 配置<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <display-name>Archetype Created Web Application</display-name> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>seckill-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置springMVC需要加载的配置文件 spring-dao.xml, spring-service.xml, spring-web.xml Mybatis -> spring -> springMVC --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>seckill-dispatcher</servlet-name> <!-- 默认匹配所有的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
spring-mvc.xml<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"> <!-- 配置springMVC --> <!-- 1、開啟springMVC註解模式 --> <!-- 簡化配置: (1)自動註冊DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter (2)提供一系列的功能:數字和日期的格式化 @NumberFormat @DateTimeFormat XML和JSON的數據支持 --> <mvc:annotation-driven/> <!-- servlet-mapping 映射路徑:"/" --> <!-- 2、靜態資源默認servlet配置 1、加入對靜態資源的處理: js、gif、png 2、允許使用"/"作為整體映射 --> <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相關的bean --> <context:component-scan base-package="org.seckill.web"/> </beans>
查看全部 -
投影片~~~~~~
查看全部 -
投影片~~~~~~~~~
查看全部 -
投影片~~~~~~~
查看全部 -
@RestController @RequestMapping("/seckill") public class SecKillController { Logger logger = LoggerFactory.getLogger(getClass()); @Autowired SecKillService secKillService; @RequestMapping(name = "/list",method = RequestMethod.GET) public String list(Model model){ List<SecKill> list = secKillService.getSecKillList(); model.addAttribute("list",list); return "list"; } @RequestMapping(value = "/{seckillId}/detail",method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long seckillId,Model model){ if(seckillId == null){ return "redirect:/seckill/list"; } SecKill secKill = secKillService.getById(seckillId); if(secKill==null){ return "redirect:/seckill/list"; } model.addAttribute("secKill",secKill); return "detail"; } @RequestMapping(value = "/{seckillId}/exposer" ,method = RequestMethod.POST ,produces = {"application/json;charset=UTF-8"}) @ResponseBody public SecKillResult<Exposer> exposer(Long seckillId){ SecKillResult<Exposer> result; try { Exposer exposer = secKillService.exportSecKillUrl(seckillId); result = new SecKillResult<Exposer>(true,exposer); }catch (Exception e){ logger.error(e.getMessage(),e); result = new SecKillResult<Exposer>(false,e.getMessage()); } return result; } @RequestMapping(value = "/{seckillId}/{md5}/execution" ,method = RequestMethod.POST ,produces = {"application/json;charset=UTF-8"}) @ResponseBody public SecKillResult<SecKillExecution> execute(@PathVariable("seckillId") Long seckillId ,@PathVariable("md5") String md5 ,@CookieValue(value = "killPhone",required = false) Long phone){ if(null==phone){ return new SecKillResult<SecKillExecution>(false,"未注册"); } //SecKillResult<SecKillExecution> result; try{ SecKillExecution secKillExecution = secKillService.executesSecKill(seckillId,phone,md5); return new SecKillResult<SecKillExecution>(true,secKillExecution); }catch(SecKillCloseException e){ SecKillExecution secKillExecution = new SecKillExecution(seckillId, SecKillStatEnum.END); return new SecKillResult<SecKillExecution>(false,secKillExecution); }catch (RepeatKillException e){ SecKillExecution secKillExecution = new SecKillExecution(seckillId, SecKillStatEnum.REPEAT_KILL); return new SecKillResult<SecKillExecution>(false,secKillExecution); } catch (Exception e){ logger.error(e.getMessage(),e); SecKillExecution secKillExecution = new SecKillExecution(seckillId, SecKillStatEnum.INNER_ERROR); return new SecKillResult<SecKillExecution>(false,secKillExecution); } } @RequestMapping(value = "/time/now",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"}) @ResponseBody public SecKillResult<Long> time(){ Date now = new Date(); return new SecKillResult<>(true,now.getTime()); } }
查看全部 -
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" 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. 配置MVC注解模式--> <!--简化配置: (1)自动注册DefaultAnnotationHandlerMapping, AnnotationMethodHandlerAdapter (2)提供一系列功能:数据绑定, 数字和日期的format @NumberFormat,@DateTimeFormat, xml,json默认读写支持--> <mvc:annotation-driven/> <!--servlet-mapping映射路径 "/"--> <!--2. 静态资源默认servlet默认配置 1).加入对静态资源的处理: js, git ,jpg 2).允许使用"/"作为整体映射--> <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="/WEN-INF/jsp"/> <property name="suffix" value=".jsp"/> </bean> <!--扫描Web相关的bean--> <context:component-scan base-package="org.seckill.controller"/> </beans>
查看全部 -
## 细节处理
## 注解映射技巧
## HTTP请求地址映射原理
mvc.xml
1. DispatcherServlet2. DefaultAnnotation HandlerMapping
3. DefaultAnnotation HandlerAdapter
4. SeckillController
5. ModelAndView /seckill/list
6. InternalResource ViewResolver
7. Model->list.jsp
8. user
查看全部 -
## Handler开发
## SpringMVC运行流程
查看全部 -
## 秒杀API的URL设计
查看全部 -
## 详情页流程逻辑
查看全部 -
1.前端交互设计 由产品、前端、后端设计完成
2.URL设计
3.SpringMVC使用技巧
4.SpringMVC运行流程
5.Bootstrap和JS使用
查看全部 -
CDN的插件src
查看全部 -
SpringMVC运行流程
用户发送请求,映射到DispatcherServlet(中央控制器Servlet)
默认用到DefaultAnnotationHandlerMapping,用于映射URL,URL对应到具体的Handler
使用默认的DefaultAnnotationHandlerAdapter,用于做Handler适配,最终会衔接到自己做的蓝色部分④Controller,如果其中用到intercept(拦截器)的话也会把拦截器绑定到流程当中
DefaultAnnotationHandlerAdapter产出⑤ModelAndView,同时交互到DispatcherServlet中
DispatcherServlet发现应用的是InternalResourceViewResolver(默认的jsp的view)
DispatcherServlet把⑦Model和list.jsp相结合,反馈给用户
实际开发的是蓝色部分Controller,其他部分用注解形式完成
HTTP请求地址映射原理
注解映射技巧
请求方法细节处理
请求参数绑定
请求方式限制
请求转发和重定向
数据模型赋值
返回json数据
cookie访问
例子:
PathVariable:占位符匹配,通过该注解绑定参数,对应到value中的占位符,当用户传入URL时,会获取URL中的seckillId部分传入detail方法中
method:该请求只允许GET方法访问
model:承载返回给用户的数据;model.addAttribute传入seckill(通过key-value方式映射),在页面中可以通过seckill拿到model中的数据
redirect/forward:通过该字符串,控制请求的是重定向/转发
最后return“detali”字符串,根据配置默认返回的是对应的jsp页面,即detail.jsp,把model的数据传递给detail.jsp做相应的数据替换,最终输出该jsp的html内容
返回json数据:
produces:返回http请求的header
ResponseBody:返回json数据,SeckillResult result会把最终数据封装成json返回给浏览器
Cookie访问:
CookieValue:required=false,不强制传入cookie,当cookie中没有对应的killPhone时不做拦截,也进入到方法中,后续会报异常
查看全部
举报