| Method from org.apache.struts2.dispatcher.ServletRedirectResult Detail: |
public ServletRedirectResult addParameter(String key,
Object value) {
requestParameters.put(key, String.valueOf(value));
return this;
}
Adds a request parameter to be added to the redirect url |
protected void doExecute(String finalLocation,
ActionInvocation invocation) throws Exception {
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
if (isPathUrl(finalLocation)) {
if (!finalLocation.startsWith("/")) {
ActionMapping mapping = actionMapper.getMapping(
request, Dispatcher.getInstance().getConfigurationManager());
String namespace = null;
if (mapping != null) {
namespace = mapping.getNamespace();
}
if ((namespace != null) && (namespace.length() > 0) && (!"/".equals(namespace))) {
finalLocation = namespace + "/" + finalLocation;
} else {
finalLocation = "/" + finalLocation;
}
}
// if the URL's are relative to the servlet context, append the servlet context path
if (prependServletContext && (request.getContextPath() != null) && (request.getContextPath().length() > 0)) {
finalLocation = request.getContextPath() + finalLocation;
}
ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(invocation.getResultCode());
if (resultConfig != null ) {
Map resultConfigParams = resultConfig.getParams();
for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext();) {
Map.Entry e = (Map.Entry) i.next();
if (!getProhibitedResultParams().contains(e.getKey())) {
requestParameters.put(e.getKey().toString(),
e.getValue() == null ? "" :
conditionalParse(e.getValue().toString(), invocation));
String potentialValue = e.getValue() == null ? "" : conditionalParse(e.getValue().toString(), invocation);
if (!supressEmptyParameters || ((potentialValue != null) && (potentialValue.length() > 0))) {
requestParameters.put(e.getKey().toString(), potentialValue);
}
}
}
}
StringBuilder tmpLocation = new StringBuilder(finalLocation);
UrlHelper.buildParametersString(requestParameters, tmpLocation, "&");
finalLocation = response.encodeRedirectURL(tmpLocation.toString());
}
if (LOG.isDebugEnabled()) {
LOG.debug("Redirecting to finalLocation " + finalLocation);
}
sendRedirect(response, finalLocation);
}
|
protected List<String> getProhibitedResultParams() {
return Arrays.asList(new String[]{
DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location",
"prependServletContext", "supressEmptyParameters"});
}
|
public void handle(ReflectionException ex) {
// Only log as debug as they are probably parameters to be appended to the url
LOG.debug(ex.getMessage(), ex);
}
|
protected void sendRedirect(HttpServletResponse response,
String finalLocation) throws IOException {
if (SC_FOUND == statusCode) {
response.sendRedirect(finalLocation);
} else {
response.setStatus(statusCode);
response.setHeader("Location", finalLocation);
response.getWriter().write(finalLocation);
response.getWriter().close();
}
}
Sends the redirection. Can be overridden to customize how the redirect is handled (i.e. to use a different
status code) |
public void setActionMapper(ActionMapper mapper) {
this.actionMapper = mapper;
}
|
public void setPrependServletContext(boolean prependServletContext) {
this.prependServletContext = prependServletContext;
}
Sets whether or not to prepend the servlet context path to the redirected URL. |
public void setStatusCode(int code) {
this.statusCode = code;
}
|
public void setSupressEmptyParameters(boolean supressEmptyParameters) {
this.supressEmptyParameters = supressEmptyParameters;
}
Sets the supressEmptyParameters option |