Second-kill system Web layer design implementation method, web layer
Implementation of the Web layer design of the second kill System
1. Restful Interface Design
Use the resource + term method to name the url link. For example:
The link to the access details page can be: seckill/{seckillId}/detail
Ii. SpringMVC Configuration
1. Configure the central controller in web. xml.
<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 "> <! -- Modify the servlet version to 3.1 --> <! -- Configure the central controller DispatcherServlet --> <servlet-name> seckill-dispatcher </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <! -- Configure the configuration file spring-dao.xml, spring-service.xml that spring MVC needs to load, 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> <! -- All requests are matched by default --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2. In order for Spring to manage the bean at the Controller layer, a new spring-web.xml configuration file is required,
<Beans xmlns =" http://www.springframework.org/schema/beans "Xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "Xmlns: mvc =" http://www.springframework.org/schema/mvc "Xmlns: conext =" http://www.springframework.org/schema/context "Xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans Spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc Spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context /Spring-context-3.1.xsd "> <! -- Configure Spring MVC --> <! -- Enable SpringMVC annotation mode --> <! -- Simplified configuration 1. Automatic Registration of DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter 2. provides a series of functions: Data Binding, conversion of numbers and dates @ NumberFormat, @ DataTimeFormat xml, json default read/write support --> <mvc: annotation-driven/> <! -- Servlet-mapping ing path --> <! -- Default servlet configuration for static Resources 1. Add processing for static resources: js, css, img 2. Allow/perform overall ing --> <mvc: default-servlet-handler/> <! -- Configure viewResolver in jsp --> <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> <! -- Scan web-related beans --> <conext: component-scan base-package = "org. seckill. web"/> </beans>
3. Controller LAYER DEVELOPMENT
Each url in the project corresponds to a method at the Controller layer. We have two types of return values. One is to let the page jump to a webpage with the data obtained from the service layer in the model. In the following example, the front-end detail. jsp can get the name of the sekill object in the model with $ {seckill. name.
/*** Second kill details page ** @ param seckillId * @ param model * @ return */@ 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 "forward:/seckill/list";} model. addAttribute ("seckill", seckill); return "detail ";}
In addition, you can click a button on the jsp page to use ajax to refresh a part of the page. You need to backend the page with a json format data. Use @ ResponseBody to tell SpringMVC to return a json data SeckillResult. The jsp page obtains the json data in the JQeury callback function and performs corresponding operations.
@RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = {"application/json;charset=utf-8" }) @ResponseBody public SeckillResult<Exposer> exposer(@PathVariable 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; }
How to handle callback functions in js Code:
$. Post (seckill. URL. exposer (seckillId), {}, function (result) {// In the callback function, execute the interaction flow if (result & result ['success']) {var exposer = result ['data']; if (exposer ['exposed ']) {// enable the second kill // obtain the second kill address var md5 = exposer ['md5']; // bind a click event to prevent consecutive click var killUrl = seckill.URL.exe cution (seckillId, md5); console. log ("second kill address:" + killUrl );});
Iv. Detailed handling of request methods
1. Request Parameter binding
@RequestMapping(value = “/{seckillId}/exposer” public SeckillResult exposer(@PathVariable Long seckillId)
2. Restrictions on request methods
@RequestMapping(method = RequestMethod.POST,
3. Request forwarding and request redirection
Return "redirect:/seckill/list"; (the browser address is changed when two requests are sent) return "forward:/seckill/list"; (the browser address remains unchanged when one request is sent)
4. Data Model assignment
model.addAttribute(“seckill”, seckill);
5. Return json data
@RequestMapping(value = “/{seckillId}/exposer”, method = RequestMethod.POST, produces = {“application/json;charset=utf-8” }) @ResponseBody
6. cookies
@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) {...}
@ CookieValue (value = "killPhone", required = false) Long phone)
(1) value (default ""): parameter name, for example, JSESSIONID
(2) required (default true): whether the request header must contain the parameter specified by value. If cookies are not set, we need to be able to access this business and ask users to fill in the relevant information, so set it to false.
V. Others
In fact, this part of the course is worth learning in front-end js interaction, such as JQuery usage, js modular development, and js interaction design. I will not summarize the reason why the time relationship and review focus are not in js.
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!