0

I am using struts in my projects. I have an idea to create a reusable component (May be tag or plugin, not sure) which can be used on different pages within same website or it can be imported in new website (projects) later. Like I want to show poll on each page of site based upon the current page. The idea is it should work independently (with using some properties files for db settings etc.) like validation or saving poll results in DB and show result after submission etc.

Now my concern/question is what should I use, custom tag/Plugin or what?

I need step by step instructions for the same, if custom tag then steps for creating custom tag or if plugin then steps for creating plugin and how can we import the same in other projects. Please provide the references if available :).

Thanks Krishan Babbar

1 Answers1

1

A Struts 2 plugin is a single JAR that contains classes and configuration that extend, replace, or add to existing Struts framework functionality. But you are planning is to create some kind of reusable component which you can use at per your needs.

Strtuts2 provides out of the box support to create such reusable components.You have two options

  1. You can use struts2 component tag <s:component>

here is the detail about it Struts2 component tag

here is a very good post about using this component

Creating a UI comonent

If this is not fulfilling your requirement you can always go to create a custom component by extending org.apache.struts2.components.Component

Here are the details foe this as this is the base class to create reusable components.

Component Struts2

Here is a sample how you will create a component

public class Hello extends Component {
    protected String name;

    public Hello(ValueStack stack) {
        super(stack);
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean start(Writer writer) {
        try {
            writer.write("Hello " + name);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

    public boolean end(Writer writer) {
        return true;
    }

    @Override
    public boolean usesBody() {
        return false;
    }
}

Refer this blog for details Creating Custom Component in Struts2

Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204