Every Struts2 user should be using the conventions plugin.  It reduces xml configuration to almost nothing, speeds up development time and improves readability.  There is really no reason not to use it.  For detailed information on the struts2-conventions-plugin see the struts2 site.

Starting with an empty Struts2 project.

All we need to do is add the struts2-conventions-plugin (using the same version as struts2) to your class path.

Using the conventions plugin is very easy!

It forms a relationship between your packages and the JSPs under WEB-INF (you can use other view technology but in this example we'll use JSPs).

Basic Rules you must obey when using conventions

  • Packages are scanned for "action", "struts" or "struts2".  When such a package is found all Classes which implement ActionSupport or end with the name "Action" become actions.
  • Action Classes follow Java standard naming practices. That is the classes are written in camel case.
  • The JSPs which act as the view for an action class will be all lower case with hyphens between words.
  • The package structure after "action", "struts" or "struts2" become part of the name space for the action.  Packages which follow become struts2 namespaces.
  • JSPs for the root namespace are found under /WEB-INF/content other name spaces are added as folders under content.
  • JSPs under /WEB-INF/content automatically become actions even if there is no corresponding action class. 


The following examples use the the following:

Server: http://localhost:8080/

Context Root: Example/

So all our pages will be prefixed with http://localhost:8080/Example/

Example 1 - Create an action to display a Form (No action class required)

The following create an action to display a JSP which allows the user to enter their name, in the next example we allow them to submit it and process the results.

Create a JSP at /WEB-INF/content/enter-name.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Enter Name Example</title>
        <h1>Enter Name Example</h1>
        <s:form action="process-form">
            Please enter your name: <s:textfield name="name"/>

To access this action: http://localhost:8080/Example/enter-name

Example 2 - Process form and display results

We need to create an action to process the name from the above example.  We know that we must put this action in a package which contains the name "action", "struts" or "struts2".

Create a package called: com.example.action (really the package structure before ".action" can be what ever you want)

Create a file in this package called: ProcessForm.java

Add the following source:

package com.example.action;

import com.opensymphony.xwork2.ActionSupport;

public class ProcessForm extends ActionSupport{
    private String name; //If this was public we could omit the getters/setters

    public String execute(){
        //processing goes here
        //lets do something really simple like adding Hello to the name
        name = "Hello, " + name + "!";
        return SUCCESS;

    public String getName() {
        return name;

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

Create the JSP for this action: /WEB-INF/content/process-form and add the following content

<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Display Processing Example</title>
        <h1>Display Processing Example</h1>
        <s:property value="name"/>

That's it! Run the project and test it with http://localhost:8080/Example/enter-name

To input a value or supply a get request by using the url, such as: http://localhost:8080/Example/process-form?name=Ken

Created by Ken McWilliams on 2012/03/03 23:02
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 12.3 - Documentation