秒殺系統(tǒng)Web層設(shè)計(jì)的實(shí)現(xiàn)方法
秒殺系統(tǒng)Web層設(shè)計(jì)的實(shí)現(xiàn)方法
一、Restful接口設(shè)計(jì)
使用資源+名詞的方式來(lái)為url鏈接命名。例如:
訪問(wèn)詳情頁(yè)的鏈接可以是: seckill/{seckillId}/detail
二、SpringMVC配置
1、首先要在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">
<!-- 修改servlet版本為3.1 -->
<!-- 配置中央控制器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>
<!-- 默認(rèn)匹配所有的請(qǐng)求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2、為了讓Spring管理Controller層的bean,需要新建一個(gè)spring-web.xml配置文件,
<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">
<!--配置Spring MVC-->
<!--開(kāi)啟SpringMVC注解模式-->
<!--簡(jiǎn)化配置
1、自動(dòng)注冊(cè)DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
2、提供一系列功能:數(shù)據(jù)綁定,數(shù)字和日期的轉(zhuǎn)化@NumberFormat,@DataTimeFormat
xml,json默認(rèn)讀寫(xiě)支持
-->
<mvc:annotation-driven/>
<!--servlet-mapping映射路徑-->
<!--靜態(tài)資源默認(rèn)servlet配置
1、加入對(duì)靜態(tài)資源的處理:js,css,img
2、允許使用/做整體映射
-->
<mvc:default-servlet-handler/>
<!--配置jsp顯示viewResolver-->
<bean>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--掃描web相關(guān)的bean-->
<conext:component-scan base-package="org.seckill.web"/>
</beans>
三、Controller層開(kāi)發(fā)
項(xiàng)目中的每一個(gè)url都剛好對(duì)應(yīng)著Controller層的一個(gè)方法。我們有兩種返回值類型。一種是讓頁(yè)面跳轉(zhuǎn)到某個(gè)網(wǎng)頁(yè),在model中帶上從service層中獲得的數(shù)據(jù)。在下例中,前端的detail.jsp就能夠以${seckill.name}取得放在model中的sekill實(shí)體的名字。
/**
* 秒殺詳情頁(yè)
*
* @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";
}
另外一種是jsp頁(yè)面中點(diǎn)擊某個(gè)按鈕,通過(guò)ajax來(lái)刷新頁(yè)面的某部分,需要后端給前端一個(gè)json格式的數(shù)據(jù)。使用@ResponseBody告訴SpringMVC返回一個(gè)json類型的數(shù)據(jù)SeckillResult。由jsp頁(yè)面在JQeury的回調(diào)函數(shù)內(nèi)拿到該json數(shù)據(jù),并進(jìn)行對(duì)應(yīng)的操作。
@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;
}
js代碼中回調(diào)函數(shù)的處理方式:
$.post(seckill.URL.exposer(seckillId),{},function(result){
//在回調(diào)函數(shù)中,執(zhí)行交互流程
if(result && result["success"]){
var exposer = result["data"];
if(exposer["exposed"]){
//開(kāi)啟秒殺
//獲取秒殺地址
var md5 = exposer["md5"];
//綁定一次點(diǎn)擊事件,防止連續(xù)點(diǎn)擊
var killUrl = seckill.URL.execution(seckillId,md5);
console.log("秒殺地址:"+killUrl);
});
四、請(qǐng)求方法的細(xì)節(jié)處理
1、請(qǐng)求參數(shù)的綁定
@RequestMapping(value = “/{seckillId}/exposer”
public SeckillResult exposer(@PathVariable Long seckillId)
2、請(qǐng)求方式的限制
@RequestMapping(method = RequestMethod.POST,
3、請(qǐng)求轉(zhuǎn)發(fā)、請(qǐng)求重定向
return “redirect:/seckill/list”;(發(fā)送兩次請(qǐng)求,瀏覽器地址改變) return “forward:/seckill/list”;(發(fā)送一次請(qǐng)求,瀏覽器地址不變)
4、數(shù)據(jù)模型賦值
model.addAttribute(“seckill”, seckill);
5、返回json數(shù)據(jù)
@RequestMapping(value = “/{seckillId}/exposer”,
method = RequestMethod.POST,
produces = {“application/json;charset=utf-8” })
@ResponseBody
6、cookies訪問(wèn)
@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 “”):參數(shù)名例如: JSESSIONID
(2)required(default true):是否請(qǐng)求路頭中必須帶value指定的參數(shù)。如果沒(méi)有設(shè)置cookies我們這個(gè)業(yè)務(wù)也要能夠訪問(wèn)并讓用戶填寫(xiě)相應(yīng)信息,所以設(shè)為false即可。
五、其他
其實(shí)課程的這一部分在前端js交互中有很多值得學(xué)習(xí)的地方,比如JQuery的使用,js模塊化開(kāi)發(fā),js交互設(shè)計(jì)等內(nèi)容。因?yàn)闀r(shí)間關(guān)系以及復(fù)習(xí)側(cè)重點(diǎn)不在js部分的原因,我就暫時(shí)不去做總結(jié)。
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章:

網(wǎng)公網(wǎng)安備