javaannotationsstruts2struts2-convention-plugin

How to redirect to a different namespace using Result annotation in an action class in Struts 2


I have a website that has public and protected areas. It is setup using Struts 2 (2.5.30) and the Convention Plugin (2.5.30).

In one of my actions, I am trying to redirect from the public (/pub/enrollmentAction) to another action in the protected area (/prot/CompleteEnrollmentAction) using the Results Annotation.

When the redirect is performed, it prepends the /pub to the resulting URL. ie /pub/prot/complete-enrollment. It should be /prot/complete-enrollment.

The EnrollmentAction is

@Action(results = { @Result(name = "success", location = "/prot/complete-enrollment", type = "redirectAction") }, interceptorRefs = { @InterceptorRef("BasicStack") })
public class Enrollment extends ActionSupport {
  
  @Override
  public String execute() throws Exception {
    
    return "success";
  }

I have tried putting in the @namespace annotation in each of the classes, but no luck. I have tried putting the result into the struts.xml, but it did not resolve correctly.

I can manually hit the redirected URL if I type it in.


Solution

  • Since you use a Convention plugin you need to know how configuration is created by convention. The plugin has also its own configuration and if it was changed then it might affect your code.

    You have used a result configuration incorrectly. There should be a redirect result type if you want to use a location attribute. There's no such attribute in the redirectAction result type.

    @Result(name = "success", location = "/prot/complete-enrollment", type = "redirect")
    

    If you want to use redirectAction result type then

    @Result(name = "success", namespace = "/prot", actionName = "complete-enrollment", type = "redirectAction")