the dynamic namespace assignment ListPeopleAction example
package org.apache.struts2.showcase.person;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
@ParentPackage(person)
@Namespace(value=listPerson) //theoretically this should change namespace
from /person to /listPerson
@Results({
@Result(name=list, location=list-people.action, type=redirect),
@Result(name=input, location = list-people.action, type=redirect),
@Result(name=success, location = listPerson.jsp, type=redirect)
//produces No result defined for action
org.apache.struts2.showcase.person.NewPersonAction and result input
//com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:375)
//@Result(name=input, location=/person/editPerson.jsp),
})
public class ListPeopleAction extends ActionSupport {
private static final long serialVersionUID = 3608017189783645371L;
PersonManager personManager;
List people = new ArrayList();
public void setPersonManager(PersonManager personManager) {
this.personManager = personManager;
}
public String execute() {
people.addAll(personManager.getPeople());
return SUCCESS;
}
public List getPeople() {
return people;
}
public int getPeopleCount() {
return people.size();
}
}
theoretically listPerson namespace is supposed to replace default assigment of
'person' so lets take a look at behavior once struts-showcase webapp is loaded:
http://localhost:8080/struts2-apps/person
I see the original 2 menu options:
1) Create a new Person
2) List all persons
I want to see if the /listPerson is assigned is working so I click on 2)List
all persons and the dispatcher takes me to
http://localhost:8080/struts2-apps/person/listPeople.action
listPeople.action is correct because @Result(name=input, location =
list-people.action, type=redirect)
but the dynamic re-assigment of namespace /person to listPerson has not taken
plac
I then correct address bar
http://localhost:8080/struts2-apps/listPerson/listPeople.action
and am displayed 404 by dispatcher
2 possible courses of action
1)redact @Namespace since it doesnt invoke JDKProxy to redirect namespace
'person' to 'listPerson'
2)is there a way to change xwork ObjectFactory to not use ProxyObjectFactory or
CGLIB but the JDK Proxy change:
xwork
bean type=com.opensymphony.xwork2.ObjectFactory name=default
class=com.opensymphony.xwork2.ProxyObjectFactory /
to a factory which will implement JDK Proxy e.g:
// Implement interface
Class? clazz = Class.forName(Namespace);
Object listenerInstance = Proxy.newProxyInstance(clazz.getClassLoader(),
new Class?[]{clazz}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws
Throwable {
if(method.getName().equals(onReceive)){
System.out.println(ARGS: + (Integer)args[0] + , +
(Integer)args[1]);
return 1;
}
else return -1;
}
});
Thoughts?
Martin
-
The key to prevent a hacked email account is to change password more than every
5 years..
From: mgai...@hotmail.com
To: user@struts.apache.org
Subject: RE: Using Struts2 Rest Plugin with deep cascading actions
Date: Sat, 24 Jan 2015 18:38:43 -0500
Date: Sat, 24 Jan 2015 15:24:49 +0530
Subject: Re: Using Struts2 Rest Plugin with deep cascading actions
From: ghotankaru...@gmail.com
To: user@struts.apache.org
Well after doing some research on designing rest api ***best practices*** I
realized I was doing something wrong.
For getting all companies, we must use
http://localhost/company
For getting all locations of that company we can use
http://localhost/company/111/locations
For anything else to get from any particular location we can use
http://localhost/company/locations/111 - to get location details
(/company/ only in namespacing )
http://localhost/company/locations/111/contacts - to get location contacts
etc.
Now question is how do we implement namespacing in struts2 rest api???
As by default all actions are @root right?
can we have @namespace(value=/company/) annotation in Location Action
But this does not work for me, it redirects to company action only and says
no method available.
Or we must not use such namespacing and its better to use
http://localhost/location instead of http://localhost/company/location
Please suggest.
MGyou need to specify the namespace attribute in package specification
MGhttp://www.mkyong.com/struts2/struts-2-namespace-configuration-example-and-explanation
MGthere is a movement to add more