0

I've got a problem with redirecting from one action to another in struts 2.

In action one (called: StudentZuPruefungHinzufuegen) i am creating action errors and want to show these in action two (called:ZeigePruefungsliste).

If i just use the "redirectAction" from struts it shows the correct action error at the place i wanted to show it. The problem is that the framework doesn't invoke my "execute" method before showing the resultpage with the action errors. Though the table I am filling within my "execute" method is empty and the resulting page is in fact useless.

Can somebody tell me how I can tell Struts to invoke the method while redirecting to action two?

My struts.xml looks like that:

<action name="ZeigePruefungsliste"
class="de.nak.multiplechoice.action.ZeigePruefungslisteAction">
    <interceptor-ref name="store">
        <param name="operationMode">RETRIEVE</param>
    </interceptor-ref>
    <interceptor-ref name="defaultLoginStack" />
    <result type="tiles" name="*">pruefungsListe</result>
</action>

<action name="StudentZuPruefungHinzufuegen"
class="de.nak.multiplechoice.action.EditiereTeilnehmerAction">
    <result type="tiles">studentZuPruefungHinzufuegen</result>
    <interceptor-ref name="store">
        <param name="operationMode">STORE</param>
    </interceptor-ref>
    <interceptor-ref name="defaultLoginStack" />
    <result name="input" type="redirectAction">
        <param name="actionName">
            ZeigePruefungsliste
        </param>
    </result>
</action>

EDIT: Code of my action classes.

public class EditiereTeilnehmerAction extends AbstractAction {

private static final long serialVersionUID = 882657701678551299L;

private Long ausgewaehltePruefungId;

private List<Nutzer> studenten;

private List<Long> ausgewaehlteStudenten;

@Override
public String execute() {
    if (ausgewaehltePruefungId == null) {
        addActionError("Es muss eine Prüfung ausgewählt werden.");
        return INPUT;
    }
    studenten = getNutzerService().ladeAlleStudenten();
    return SUCCESS;
}

public String zuPruefungHinzufuegen() {
    getPruefungService().registriereStudentenFuerPruefung(ausgewaehltePruefungId, ausgewaehlteStudenten);
    return SUCCESS;
}

public List<Nutzer> getStudenten() {
    return studenten;
}

public void setStudenten(List<Nutzer> studenten) {
    this.studenten = studenten;
}

public Long getAusgewaehltePruefungId() {
    return ausgewaehltePruefungId;
}

public void setAusgewaehltePruefungId(Long ausgewaehltePruefungId) {
    this.ausgewaehltePruefungId = ausgewaehltePruefungId;
}

public List<Long> getAusgewaehlteStudenten() {
    return ausgewaehlteStudenten;
}

public void setAusgewaehlteStudenten(List<Long> ausgewaehlteStudenten) {
    this.ausgewaehlteStudenten = ausgewaehlteStudenten;
}

}

public class ZeigePruefungslisteAction extends AbstractAction {

private static final long serialVersionUID = -7559234907568287686L;
private List<Pruefung> pruefungen;

@Override
public String execute() {
    pruefungen = getPruefungService().getAllePruefungen(getAktuellenNutzer());
    return SUCCESS;
}

public List<Pruefung> getPruefungen() {
    return pruefungen;
}

}

public abstract class AbstractAction extends ActionSupport {

private static final long serialVersionUID = -6905222101893534102L;

private INutzerService nutzerService;

private IPruefungService pruefungService;

public HttpSession getHttpSession() {
    HttpServletRequest request = (HttpServletRequest) ActionContext
        .getContext().get(StrutsStatics.HTTP_REQUEST);
    return request.getSession(true);
}

public Nutzer getAktuellenNutzer() {
    return nutzerService.get(((Nutzer) getHttpSession().getAttribute(
        StrutsKonstanten.NUTZER)).getId());
}

protected INutzerService getNutzerService() {
    return nutzerService;
}

public void setNutzerService(INutzerService nutzerService) {
    this.nutzerService = nutzerService;
}

protected IPruefungService getPruefungService() {
    return pruefungService;
}

public void setPruefungService(IPruefungService pruefungService) {
    this.pruefungService = pruefungService;
}

}

Greetings, Marcus

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Bit confusing when it's working and when it's not. And How you defined defaultLoginStack interceptor stack. – MohanaRao SV Nov 10 '12 at 14:03
  • OK i try to describe it in another way. I try to show my action errors from action one in the jsp of action two. If i just configure my struts.xml in the way that it uses a redirectAction to show the second Action, my Errors are displayed, but the rest of my page is empty. I initialize the content of this page with the execute method of action two. So my question is, how can i configure that Struts 2 is executing my "execute" methode before showing the jsp referenced to the action. Result type "chain" isn't an option for me. – Marcus Berndt Nov 10 '12 at 14:24
  • The defaultLoginStack is defined by myself, it just includes one extra interceptor into the interceptorchain to check if the user is logged in or not. – Marcus Berndt Nov 10 '12 at 14:25
  • If you redirect to an action, the client goes through a complete request cycle, and whatever method is defined in the action's config will be executed, just like it's a new request (because it is). – Dave Newton Nov 10 '12 at 18:13
  • In fact that is the point why i can't understand, that in my action the "execute" method isn't invoked. If you look at my configuration: I've said start a "redirectAction" to my "ZeigePruefungsliste" action. But with this configuration the "execute" methode isn't invoked. Is there something wrong in my struts.xml? – Marcus Berndt Nov 11 '12 at 11:03
  • Can you show code for these actions? – Quaternion Nov 12 '12 at 05:34
  • Hi @Quaternion, I've added the concerning Actions. – Marcus Berndt Nov 13 '12 at 22:49
  • I really need a solution for that quiet fast. Can nobody tell me what I have to do to get that working? – Marcus Berndt Nov 16 '12 at 08:06

0 Answers0