org.apache.struts2.result.PostbackResult Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.result;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
/**
*
*
* A result that renders the current request parameters as a form which
* immediately submits a postback
* to the specified destination.
*
*
*
* Parameters:
*
*
*
* - location - http location to post the form
* - prependServletContext (true|false) - when location is relative, controls if to add Servlet Context, default "true"
* - actionName - action name to post the form (resolved as an expression)
* - namespace - action's namespace to use (resolved as an expression)
* - method - actions' method to use (resolved as an expression)
* - cache (true|false) - when set to true adds cache control headers, default "true"
* - parse (true|false) - when set to true actionName, namespace and method are parsed, default "true"
*
*
*
* Examples:
*
*
*
* <action name="registerThirdParty" >
* <result type="postback">https://www.example.com/register</result>
* </action>
*
* <action name="registerThirdParty" >
* <result type="postback">
* <param name="namespace">/secure</param>
* <param name="actionName">register2</param>
* </result>
* </action>
*
*
*/
public class PostbackResult extends StrutsResultSupport {
private static final long serialVersionUID = -2283504349296877429L;
private String actionName;
private String namespace;
private String method;
private boolean prependServletContext = true;
private boolean cache = true;
protected ActionMapper actionMapper;
@Override
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);
// Cache?
if (!cache) {
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1
response.setHeader("Pragma", "no-cache"); // HTTP 1.0
response.setDateHeader("Expires", 0); // Proxies
}
//set contenttype @see ww-4564
response.setContentType("text/html");
// Render
PrintWriter pw = new PrintWriter(response.getOutputStream());
pw.write("