这就是事情,
为了模仿grails为一个表单提交多个提交的方式,我实现了这个拦截器,以便在遇到如下的URL时: / whatever-url / $ {controllerName} / index它重定向到/ whatever-url / $ {controllerName} / $ {submitName}
我有两个问题,首先是使用get方法重定向,第二个我不保留jsp中原始表单的postData,任何人都知道如何在重定向请求中设置它?
这是拦截器类:
public class ControllerIndexInterceptor extends HandlerInterceptorAdapter {
private static String[] redirects = {"edit", "delete"};
/**
* {@inheritDoc}
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String param = null;
for (String redirect : redirects) {
if((param = request.getParameter(redirect)) != null){
response.sendRedirect(request.getContextPath()+request.getServletPath()+
transformURI(request.getPathInfo(), redirect, handler));
return false;
}
}
return true;
}
public class ControllerIndexInterceptor extends HandlerInterceptorAdapter {
private static String[] redirects = {"edit", "delete"};
/**
* {@inheritDoc}
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String param = null;
for (String redirect : redirects) {
if((param = request.getParameter(redirect)) != null){
response.sendRedirect(request.getContextPath()+request.getServletPath()+
transformURI(request.getPathInfo(), redirect, handler));
return false;
}
}
return true;
}
答案:
public class ControllerIndexInterceptor extends HandlerInterceptorAdapter {
private static String[] redirects = {"edit", "delete"};
/**
* {@inheritDoc}
*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String param = null;
for (String redirect : redirects) {
if((param = request.getParameter(redirect)) != null){
request.getRequestDispatcher(request.getServletPath()+
transformURI(request.getPathInfo(), redirect, handler)).forward(request,response);
return false;
}
}
return true;
}
答案 0 :(得分:2)
IMO response.sendRedirect()
会向浏览器发送重定向状态代码,最终会发出新请求,这是您获取GET请求的原因。这也是没有获得POST数据的原因(因为它是一个新的和不同的请求)。请改用request.getRequestDispatcher()
。