linquize
linquize

Reputation: 20396

How to get a result code to JSP in Struts 2?

How to get an action result code in JSP?

I want to use the same JSP for all results.

public String doSth()
{
    return a ? "ok" : "failed";
}

Upvotes: 0

Views: 1737

Answers (2)

Roman C
Roman C

Reputation: 1

Only static access is available

<s:property value="@com.opensymphony.xwork2.ActionContext@getContext().getActionInvocation().getResultCode()"/>

For this code being able to work you need a configuration

<constant name="struts.ognl.allowStaticMethodAccess" value="true" />

Upvotes: 1

Prabhakar Manthena
Prabhakar Manthena

Reputation: 2313

same.jsp:

    <%-- 
        Author     : Prabhakar
    --%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>

<html>
    <body>    
          The Result is : <s:property value="a" />
    </body>
</html>

ActionClass.java:

package com.prabhakar;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

/**
 *
 * @author Prabhakar
 */
public class ActionClass extends ActionSupport {

    private String a;

    public String getA() {
        return this.a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String doSth() {
        if (some condition) {
            a = "success";
        } else {
            a = "failed";
        }
        return a;
    }
}

struts.xml

<struts>
  <package name="somename" extends="struts-default">
  <action name="resultName" method="doSth" class="com.prabhakar.ActionClass">
   <result name="success">same.jsp</result>
    <result name="failed">same.jsp</result>
 </action>
</package>
</struts>

Upvotes: 1

Related Questions