feder
feder

Reputation: 1795

How to read from an Action that is needed on every page?

I'm new to Struts2.

How does one invoke methods from a class without using an ActionSupport-implemented-class? So a simple Java class. It's because I want to add something to a include jsp file. Therefore this value is always valid - independent of the page one is requesting.

Probably simple.

Upvotes: 1

Views: 73

Answers (2)

Ankur Lathi
Ankur Lathi

Reputation: 7836

You can write your Action without extending ActionSupport class.

ActionSupport is just for convenience ActionSupport has default implementations of common methods (e.g., execute(), input()), gives access to Action.SUCCESS, Action.ERROR and other result names, etc.

Action Class:

public class TestAction
{
    public String testMethod()
    {

      return "success";
    }
}

Struts.xml:

<action name="TestAction" class="com.TestAction" method="testMethod">
    <result name="success">Mypage.jsp</result>
</action>

Upvotes: 2

Uchenna Nwanyanwu
Uchenna Nwanyanwu

Reputation: 3204

Struts2 architecture does not require you to extend ActionSupport class. It is that flexible. ActionSupport is just a convenience and provides basic functionality like validation etc. You can write a simple pojo action class. All you need is to return a String that will be used to forward to a resource like jsp.

For Example

public class MyAction
{
    public String testAction()
    {
        //Perform your logic here. Note it is not mandatory to return SUCCESS here, you can actually return any String here, but make sure you map that String to a resource in your `struts.xml`
        return SUCCESS;
    }
}

Upvotes: 2

Related Questions