{"rsdb":{"rid":"156517","subhead":"","postdate":"0","aid":"117478","fid":"54","uid":"1","topic":"1","content":"

\u5f88\u591a\u65f6\u5019,\u5f00\u53d1Web\u5e94\u7528\u65f6\u5404\u79cd\u524d\u540e\u53f0\u4ea4\u4e92\u8ba9\u4eba\u5f88\u70e6\u95f7,\u5c24\u5176\u662f\u5404\u79cd\u6743\u9650\u9a8c\u8bc1\u5566,\u8d2d\u7269\u8f66\u5546\u54c1\u4fe1\u606f\u5566\u7b49\u7b49\u2026\u2026<\/p>

\u5927\u5bb6\u7b2c\u4e00\u65f6\u95f4\u60f3\u5230\u7684\u662f: \u91c7\u7528HttpSession\u6765\u5b58\u8fd9\u4e9b\u5bf9\u8c61.\u7136\u540e\u5c31\u662f\u5404\u79cd\u53c2\u6570\u4eceController\u4f20\u5230Service\u518d\u4f20\u5230\u6301\u4e45\u5c42\u65b9\u6cd5.\u4e00\u76f4\u4f20\u4e00\u76f4\u4f20.<\/p>

\u5728\u73b0\u9636\u6bb5\u9700\u6c42\u53d8\u5316\u6781\u5feb\u7684\u524d\u63d0\u4e0b,\u5982\u679c\u67b6\u6784\u6ca1\u4e8b\u5148\u60f3\u597d\u5404\u79cd\u53c2\u6570\u7684\u4f20\u9012,\u5f88\u5bb9\u6613\u5bfc\u81f4\u6211\u4eec\u9700\u8981\u7684\u4e00\u4e9b\u53c2\u6570\u8981\u901a\u8fc7\u65b9\u6cd5\u5c42\u5c42\u4f20\u9012\u624d\u80fd\u7528\u5230.\u4e3a\u4ec0\u4e48\u4e0d\u60f3\u4e00\u4e2a\u7b80\u5355\u70b9\u7684,\u76f4\u63a5\u901a\u8fc7\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u8f7b\u677e\u62ff\u5230\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u6211\u4eec\u968f\u65f6\u968f\u5730\u83b7\u53d6\u8fd9\u4e9b\u5bf9\u8c61\u7684\u65b9\u5f0f\u5462?<\/p>

\u6539\u53d8\u4f20\u7edf\u7684\u53c2\u6570\u5c42\u5c42\u4f20\u9012,\u8ba9\u5927\u5bb6\u5199\u51fa\u7b80\u6d01\u4f18\u7f8e\u7684\u4ee3\u7801\u662f\u6211\u7684\u6700\u7ec8\u7406\u60f3.\u6240\u4ee5\u6211\u505a\u4e86\u4e00\u70b9\u8bd5\u9a8c,\u4ec5\u4f9b\u53c2\u8003.<\/p>

\u9996\u5148,\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u7b80\u5355\u4e0a\u4e0b\u6587\u63a5\u53e3,\u4e5f\u53ef\u4ee5\u79f0\u4e4b\u4e3a\u7b80\u5355\u7684\u5bf9\u8c61\u7f13\u5b58\u63a5\u53e3.\u63a5\u53e3\u52a0\u4e0a\u5b9e\u73b0\u7c7b,\u5927\u7ea6\u4e0d\u5230200\u884c\u4ee3\u7801,\u5f88\u8f7b\u677e.<\/p>

\u7136\u540e\u5c31\u662f\u8ba9\u6211\u4eec\u7684\u4e0a\u4e0b\u6587\u5b58\u70b9\u4e1c\u897f.\u8fd9\u91cc\u8981\u6ce8\u610f\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u5bf9\u8c61\u9694\u79bb.\u5982\u679c\u4e0d\u662f\u524d\u540e\u7aef\u5206\u79bb\u7684\u505a\u6cd5,\u53ef\u4ee5\u91c7\u7528ThreadLocal\u529e\u5230,\u4e00\u4e2a\u5de5\u5177\u7c7b\u641e\u5b9a.\u5982\u679c\u662f\u524d\u540e\u7aef\u5206\u79bb\u7684\u65b9\u5f0f,\u5bfc\u81f4\u7684\u7ebf\u7a0b\u65e0\u6cd5\u8ddf\u8e2a\u7684\u95ee\u9898,\u6211\u4eec\u5728\u540e\u9762\u8ba8\u8bba.<\/p>

\u63a5\u4e0b\u6765\u5c31\u662f\u628a\u81ea\u5b9a\u4e49\u7684\u4e0a\u4e0b\u6587\u63a5\u53e3\u7528\u8d77\u6765.\u6211\u4eec\u5148\u6765\u4f20\u7edf\u7684.\u5927\u6982\u4e09\u79cd\u505a\u6cd5:<\/p>

\u4e00. \u901a\u8fc7\u65b9\u6cd5\u53c2\u6570\u4f20\u5165HttpServletRequest\u5bf9\u8c61\u6216\u8005HttpSession\u5bf9\u8c61<\/p>

\u81eaSpring2.5\u7684annotation\u4f7f\u5f97 controller \u6446\u8131\u4e86 Servlet API \u5bf9\u65b9\u6cd5\u53c2\u6570\u7684\u9650\u5236,\u8fd9\u91cc\u5c31\u4e0d\u8d58\u8ff0\u4e86.Spring\u5bf9annotationed\u7684 action \u7684\u53c2\u6570\u63d0\u4f9b\u81ea\u52a8\u7ed1\u5b9a\u652f\u6301\u7684\u53c2\u6570\u7c7b\u578b\u5305\u62ec Servlet API \u91cc\u9762\u7684 Request\/Response\/HttpSession\uff08\u5305\u542bRequest\u3001Response\u5728Servlet API \u4e2d\u58f0\u660e\u7684\u5177\u4f53\u5b50\u7c7b\uff09\u3002\u4e8e\u662f\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u901a\u8fc7\u5728 controller \u7684\u65b9\u6cd5\u53c2\u6570\u4e2d\u58f0\u660e Request \u5bf9\u8c61\u6216\u8005 HttpSession \u5bf9\u8c61\uff0c\u6765\u8ba9\u5bb9\u5668\u6ce8\u5165\u76f8\u5e94\u7684\u5bf9\u8c61\u3002<\/p>

\u4f8b\u5982:<\/p>

@RequestMapping
public void hello(HttpSession session){
? ? User user = (User)session.getAttribute(\"currentUser\");
}<\/p>

\u4f18\u70b9\uff1a
1. \u7a0b\u5e8f\u4e2d\u76f4\u63a5\u5f97\u5230\u5e95\u5c42\u7684 Request\/HttpSession \u5bf9\u8c61\uff0c\u76f4\u63a5\u4f7f\u7528 Servlet API \u89c4\u8303\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\u64cd\u4f5c\u8fd9\u4e9b\u5bf9\u8c61\u4e2d\u7684\u5c5e\u6027\uff0c\u76f4\u63a5\u800c\u7b80\u5355\u3002
2. controller \u9700\u8981\u8bbf\u95ee\u54ea\u4e9b\u5177\u4f53\u7684 Session \u5c5e\u6027\uff0c\u662f\u7531\u81ea\u5df1\u63a7\u5236\u7684\uff0c\u771f\u6b63\u7cbe\u786e\u5230 Session \u4e2d\u7684\u6bcf\u4e2a\u7279\u5b9a\u5c5e\u6027\u3002
\u7f3a\u70b9\uff1a
1. \u7a0b\u5e8f\u5bf9 Servlet API \u4ea7\u751f\u4f9d\u8d56\u3002\u867d\u7136 controller \u7c7b\u5df2\u7ecf\u4e0d\u9700\u8981\u4ece HttpServlet \u7ee7\u627f\uff0c\u4f46\u4ecd\u9700\u8981 Servlet API \u624d\u80fd\u5b8c\u6210\u7f16\u8bd1\u8fd0\u884c\uff0c\u4e43\u81f3\u6d4b\u8bd5\u3002
2. \u66b4\u9732\u4e86\u5e95\u5c42 Servlet API\uff0c\u66b4\u9732\u4e86\u5f88\u591a\u5e76\u4e0d\u9700\u8981\u7684\u5e95\u5c42\u65b9\u6cd5\u548c\u7c7b\uff0c\u5f00\u53d1\u4eba\u5458\u5bb9\u6613\u6ee5\u7528\u8fd9\u4e9b API\u3002<\/p>

\u4e8c. \u901a\u8fc7\u5b9a\u5236\u62e6\u622a\u5668\uff08Interceptor\uff09\u5728controller\u7c7b\u7ea7\u522b\u6ce8\u5165\u9700\u8981\u7684\u4e0a\u4e0b\u6587\u5bf9\u8c61<\/p>

Interceptor \u662f Spring \u63d0\u4f9b\u7684\u6269\u5c55\u70b9\u4e4b\u4e00\uff0cSpringMVC \u4f1a\u5728 handle \u67d0\u4e2a request \u524d\u540e\u8c03\u7528\u5728\u914d\u7f6e\u4e2d\u5b9a\u4e49\u7684 Interceptor \u5b8c\u6210\u4e00\u4e9b\u5207\u9762\u7684\u5de5\u4f5c\uff0c\u6bd4\u5982\u9a8c\u8bc1\u7528\u6237\u6743\u9650\u3001\u5904\u7406\u5206\u53d1\u7b49\uff0c\u7c7b\u4f3c\u4e8e AOP\u3002\u90a3\u4e48\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u53d6\u8fd9\u6837\u4e00\u4e2a\u201c\u6a2a\u5207\u70b9\u201d\uff0c\u5728 SpringMVC \u8c03\u7528\u65b9\u6cd5\u524d\uff0c\u5728 Interceptor \u7684 preHandle \u65b9\u6cd5\u4e2d\u7ed9 controller \u6ce8\u5165\u4e0a\u4e0b\u6587\u6210\u5458\u53d8\u91cf\uff0c\u4f7f\u4e4b\u5177\u6709\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002
\u6b64\u5916\u8fd8\u9700\u8981\u7ed9\u8fd9\u4e9b\u7279\u5b9a controller \u58f0\u660e\u4e00\u7c7b interface\uff0c\u6bd4\u5982 IContextAware\u3002\u8fd9\u6837\u5f00\u53d1\u4eba\u5458\u5c31\u53ef\u4ee5\u53ea\u9488\u5bf9\u8fd9\u4e9b\u9700\u8981\u6ce8\u5165\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u5bf9\u8c61\u7684 controller \u8fdb\u884c\u6ce8\u5165\u589e\u5f3a\u3002<\/p>

IContextAware\u63a5\u53e3:<\/p>


public interface IContextAware {
? ? public void setContext(MyApplicationContext context);
}<\/p>

UserController\u7c7b:<\/p>


@Controller
@RequestMapping(value=\"\/user\")
@Scope(value=\"prototype\")
public class UserController implements IContextAware{
? ? private static final Logger log = LoggerFactory.getLogger(UserController.class);
? ? @Autowired
? ? private IUserService service;
? ? private MyApplicationContext context;
? ?
? ? @Override
? ? public void setContext(MyApplicationContext context) {
? ? ? ? this.context = context;
? ? }
? ?
? ? @RequestMapping(value=\"\/get\/{id}\")
? ? @ResponseBody
? ? public User getUser(@PathVariable(\"id\") String id){
? ? ? ? log.info(\"Find user with id={}\", id);
? ? ? ? User user = null;
? ? ? ? try {
? ? ? ? ? ? user = service.findUserById(id);
? ? ? ? ? ? if (user != null) {
? ? ? ? ? ? ? ? context.setAttribute(\"currentUser\", user);
? ? ? ? ? ? }
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? return (User) context.getAttribute(\"currentUser\");
? ? }<\/p>

}<\/p>

HandlerInterceptor\u5b9e\u73b0\u7c7b:<\/p>


public class MyInterceptor implements HandlerInterceptor {
? ? private static final Logger log = LoggerFactory.getLogger(MyInterceptor.class);
? ? private static final String MY_APPLICATION_CONTEXT = \"MYAPPLICATIONCONTEXT\";
? ?
? ? @Override
? ? public boolean preHandle(HttpServletRequest request,
? ? ? ? ? ? HttpServletResponse response, Object handler) throws Exception {
? ? ? ? IContextAware controllerAware = null;
? ? ? ? HandlerMethod handlerMethod = (HandlerMethod) handler;
? ? ? ? Class clazz = handlerMethod.getBeanType();
? ? ? ? Type[] interfaces = clazz.getGenericInterfaces();
? ? ? ? if (interfaces != null && interfaces.length > 0) {
? ? ? ? ? ? for (int i = 0; i < interfaces.length; i++) {
? ? ? ? ? ? ? ? if (interfaces[i] == IContextAware.cla","orderid":"0","title":"SpringMVC\u4e2d\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u8ba4\u8bc6(\u4e00)","smalltitle":"","mid":"0","fname":"linux\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"548","pages":"3","comments":"0","posttime":"2017-02-21 08:16:05","list":"1487636165","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"SpringMVC<\/A> \u5b9a\u4e49<\/A> \u4e0a\u4e0b\u6587<\/A> \u8ba4\u8bc6<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.31","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"SpringMVC\u4e2d\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u8ba4\u8bc6","lastview":"1702422697","digg_num":"2821","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}