1.拦截器(Interceptor)
解析:Struts2体系架构的核心
发出请求---->能匹配到拦截规则的请求会被
拦截器拦截,拦截
拦截器栈(InterceptorStack)
1.Struts执行流程
2.1 web.xml中 StrutsPrepareAndExecuteFilter类 核心控制器
2.2 找到doFilter方法
体现出一种设计模式
解释了Struts2中 request 并不是HttpServletRequest
request = prepare.wrap(包装)Request(request);
***:ActionMapping mapping = prepare.findActionMapping(request, response, true);
findActionMapping方法:
public ActionMapping findActionMapping() {
//先看内存中有没有映射结果!
ActionMapping mapping = (ActionMapping) request.getAttribute(STRUTS_ACTION_MAPPING_KEY);
//没有,找到ActionMapper构建
if (mapping == null || forceLookup) {
mapping = dispatcher.getContainer().getInstance(ActionMapper.class).
}
return mapping;
}
2.3 上面代码已经保证mapping不是null
if (mapping == null) {
} else {
//code execute
execute.executeAction(request, response, mapping);
}
executeAction方法原型如下:
public void executeAction() throws ServletException {
dispatcher.serviceAction(request, response, mapping);
}
*serviceAction原型
public void serviceAction(){
ActionProxy proxy = getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false);
}
*:寻找ActionInvocation
if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation():返回值类型是ActionInvocation);
} else {
proxy.execute();
}
2018年2月28日08:35:24
1.拦截器:底层是动态代理实现的。主要作用是在方法执行前后,插入通用业务(日志和事务);
logback
2.自定义拦截器
1.定义一个拦截器类 implements Interceptor{
public String intercept(ActionInvocation invocation){
获取到一个Action
Action action= invocation.getAction();
//获取到session
Map<String,Object> session=ActionContext.getContext().getSession();
Object name=ssssion.get("uname");
//获取ActionName
String actionName=ActionContext.getProxy().getActionName();
String value;
if(actionName.equals("loginAction")){
value=invocation.invoke();
}else if(name!=null){
value=invocation.invoke();
}else{
value="login";
}
}
}
2.UI 页面 伪造 login.jsp success.jsp
3.Action开发
LoginAction implemtns Action{
UserInfo user;
public String execute(){
if(成立){
return SUCCESS;
}
}
}