该楼层疑似违规已被系统折叠隐藏此楼查看此楼
这是我写的代码 不知道行不行 求助
package base.web.resolver.result;import java.util.HashMap;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;import base.web.annotation.ResultMapping;
import base.web.annotation.ResultMapping.Type;
import base.web.model.Result;public class RedirectChainResultHandler extends BaseResultHandler implements HandlerMethodReturnValueHandler { private static final Logger logger = LogManager.getLogger(RedirectChainResultHandler.class);
@Override
public void handleReturnValue(Object returnValue,MethodParameter returnType, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest ) throws Exception {
logger.info("-------------RedirectChainResultHandler resolver result-----------");
// 标识请求是否已经在该方法内完成处理
mavContainer.setRequestHandled(true);
// 得到request与response对象
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
// 得到返回的结果
Result result = (Result) returnValue; // 获取方法上的注解
ResultMapping resultTypeAnnotation = returnType.getMethodAnnotation(ResultMapping.class);
// 返回的默认url
String redirectUrl = resultTypeAnnotation.defaultUrl();
if(result.getUrl() != null && !"".equals(result.getUrl())){
redirectUrl = result.getUrl();
}
try {
HashMap hashMap = new HashMap();
//如果发现异常
if (result.getCode() == -1) {
hashMap.put("code", result.getCode());
hashMap.put("msg", "发生错误:" + result.getMsg());
hashMap.put("expMsg", result.getExpMsg());
//出错做转发
redirectUrl = ERROR_WEB_PAGE;
request.setAttribute("model", hashMap);
request.getRequestDispatcher(redirectUrl).forward(request, response);
return;
}
//取得参数
String parameterName = returnType.getParameterName();
//设置进去
mavContainer.addAttribute(parameterName);
//重定向
response.sendRedirect(redirectUrl);
} catch (Exception ex) {
logger.error("RedirectResultHandler发生错误:{}",ex.getMessage());
HashMap hashMap = new HashMap();
hashMap.put("code", "-1");
hashMap.put("msg", "RedirectResultHandler转换器发生错误:" + ex.getMessage());
hashMap.put("expMsg", ex.getMessage());
request.setAttribute("model", hashMap);
//出错做转发
redirectUrl = ERROR_WEB_PAGE;
request.setAttribute("model", hashMap);
request.getRequestDispatcher(redirectUrl).forward(request, response);
} finally {
}
} @Override
public boolean supportsReturnType(MethodParameter returnType) {
return returnType.getParameterType() == Result.class
&& returnType.getMethodAnnotation(ResultMapping.class) != null
&& returnType.getMethodAnnotation(ResultMapping.class).value().name().equals(Type.REDIRECT_CHAIN.name());
}
}