Struts2 e il convention plugin: creare un action con redirect ad un url esterno

Un esempio molto banale di come gestire con Struts2 e il convention plugin un redirect ad un url esterno da una action. Ad esempio per redirigere la navigazione al sito di una banca per effettuare una pagamento da un portale di ecommerce.

Queste sono le dipendenze per chi utilizza maven relative all’ultima versione di struts2:

<!-- Struts 2 --> 
<dependency>
	<groupId>org.apache.struts</groupId>
	<artifactId>struts2-core</artifactId>
	<version>2.3.14.3</version>
</dependency>
 
<!-- Tiles Plugin -->
<dependency>
	<groupId>org.apache.struts</groupId>
	<artifactId>struts2-tiles-plugin</artifactId>
	<version>2.3.14.3</version>
</dependency>
 
<!-- Convention Plugin -->
<dependency>
	<groupId>org.apache.struts</groupId>
	<artifactId>struts2-convention-plugin</artifactId>
	<version>2.3.14.3</version>
</dependency>

La definizione dell’action è questa:

@Action(value="pagamento-xpay", results={
        @Result(name = "success", location="${payUrl}",  type="redirect")
})

Ovviamente deve essere gestita la proprietà payUrl. Ecco una piccola classe di esempio:

package it.nicola.struts2.actions.pagamento;
 
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
 
import com.opensymphony.xwork2.ActionSupport;
 
 
@ParentPackage("nicola_core")
@Namespace("/")
public class Pagamento extends ActionSupport {
 
	private String payUrl;
 
	@Action(value="pagamento-xpay", results={
			@Result(name = "success", location="${payUrl}",  type="redirect")
		})
	public String execute() throws Exception {
 
		payUrl = "http://www.google.it";
		return "success";		
	}
 
	public String getPayUrl() {
		return payUrl;
	}
 
	public void setPayUrl(String payUrl) {
		this.payUrl = payUrl;
	}
 
}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>