控制实现类
package org.fkit.controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DataBindingController {
//静态的日志类LogFactory
private static final Log logger = LogFactory.getLog(DataBindingController.class);
@RequestMapping(value="/pathVariableTest/{userId}")
public void pathVariableTest(@PathVariable Integer userId) {
logger.info("-------------------通过PathVariable获得的数据:"+userId);
}
@RequestMapping(value="/requestHeaderTest")
public void requestHeaderTest(@RequestHeader("User-Agent") String userAgent,@RequestHeader(value="Accept") String[] accepts) {
logger.info("--------------------通过@RequestHeader获得数据:"+userAgent);
for(String accept:accepts) {
logger.info(accept);
}
}
@RequestMapping(value="/cookieValueTest")
public void cookieValueTest(@CookieValue(value="JSESSIONID",defaultValue="") String sessionId ) {
logger.info("--------------------通过@requestHeaderTest获得数据:"+sessionId);
}
}
前台
<html>
<body>
<h3>数据绑定测试</h3>
<a href="pathVariableTest/1">测试@PathVariable注解</a><br/><br/>
<a href="requestHeaderTest">测试@RequestHeaderTest</a><br/><br/>
<a href="cookieValueTest">测试@CookieValue注解</a><br/><br/>
</body>
</html>
测试结果
org.fkit.controller.DataBindingController pathVariableTest
信息: -------------------通过PathVariable获得的数据:
org.fkit.controller.DataBindingController requestHeaderTest
信息: --------------------通过@RequestHeader获得数据:Mozilla/5.0 (Windows NT 6.2; Win64; x64; org.fkit.controller.DataBindingController cookieValueTest
信息: --------------------通过@requestHeaderTest获得数据:E95FBBC476D6EEFBDB8381A7B7D06950