Struts 2 Rest Convention Plugin Configuration Example

Struts 2 Rest Convention Plugin Configuration Example


I was playing around with Struts 2.3.8 and I really liked it the way apache has bundled it with lots of plugins that can help developers shrink their coding efforts hugely.

I was trying with conventions plugin and a simple struts action. The action was not loading saying there is no mapping found.. After couple of tries I could make it work.. Posting the same if some one might find it helpful in saving time..

Action


package com.mycomp.recruit.actions;

import java.util.Collection;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Validateable;
import com.opensymphony.xwork2.ValidationAwareSupport;

@Results({
    @Result(name="success", type="redirectAction", params = {"actionName" , "authentication"})
})
public class AuthenticationController extends ValidationAwareSupport implements ModelDriven<Object>, Validateable{
    
    /**

*/
private static final long serialVersionUID = 1L; 
    private String id; 

    // GET /orders/1
    public HttpHeaders show() {
        return new DefaultHttpHeaders("show");
    }
    
    public HttpHeaders index(){
    System.out.println("Going thru authentication..");    
    return  new DefaultHttpHeaders("index").disableCaching();
    }


    public void validate() { 
    }

    public void setId(String id) {
        if (id != null) { 
        //do nothin!??
        }
        this.id = id;
    }
    
   public Object getModel() { 
  return null;
    }

}


struts.xml


<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!--  Overwrite Convention -->
    <constant name="struts.convention.action.suffix" value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <constant name="struts.convention.default.parent.package" value="rest-default"/>

    <constant name="struts.convention.package.locators" value="example,actions"/>
    
</struts>


JSP


/web-inf/content/authentication-index.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Authenticate..
</body>
</html>




Share this

Related Posts

Previous
Next Post »