Two methods in same action in struts2? - java

I am trying to create an action with two methods (JSON action). I am calling them from JSP files. If I try to call an action value as 'medias' in my code, it simply run both the methods every time.
#Action(value="medias", results = {#Result(name="success",type="json")})
public String getMedias(){
System.out.println("IN METHOD CALL medias");
return SUCCESS;
}
#Action(value="allMediaTypes", results = {#Result(name="success",type="json")})
public String getAllMediaTypes(){
System.out.println("IN METHOD CALL allMediaTypes");
return SUCCESS;
}
Both method runs simultaneously, no matter which method is getting called from jsp, it runs both the methods.

Don't prefix your method names with get - doing this has implications.
It's a good idea to name them the same as your action names for consistency, ie:
public String medias() {
...
}
public String allMediaTypes() {
...
}

Related

What is the best way to call one #RequestMapping method from another in Java?

I have 3 methods in a controller, looking something like this.
#RequestMapping(value="export-data")
public ExcelView exportData(String urlForData, String paramToPass) {
List<Data> data; //WHAT SHOULD I DO HERE?
return new ExcelView(data);
}
#RequestMapping(value="get-data-1")
public List<Data> getData1(String param) {
//return the data based on param
}
#RequestMapping(value="get-data-2")
public List<Data> getData2(String param) {
//return the data based on param
}
The value for urlForData will be either get-data-1 or get-data-2. And I'll need to call the specified method with the given paramToPass.
Is there a way to call the methods via Spring (versus just doing a String equality check)? Notice that this is not a redirect as I want to remain in the exportData method (versus giving up control to the other methods).

Maintaining values of action variables?

class SampleAction extends ActionSupport {
private Map<String,String> circleIdNameMap;
public String preprocess(){
--logic for populating value of MAP
}
--getters and setters
}
Now my problem is on page load I call preprocess function and populate the value of Map. After page submit another method is called and during that after some DB interaction it redirects to JSP, but this time the value of Map is empty. I am using this Map for drop-down tag in Struts2.
My preprocess is associated in the link like:
href="/gma/preprocessConfigureTspThreshold?operatorId=5102&sessionId=12332"‌`​
So only first time when the link is clicked preprocess is called, after that when I redirect to my JSP so its not called then, so second time the value of Map is empty.
Shall I put the map in a session so that it is retained? Or can do something else?
I read that don't use preprocess function, use Preparable interface. But as per docs:
The prepare method will always be called by the Struts 2 framework's prepare interceptor
whenever any method is called for the Action class.
So, it will be called for every method. I want preprocess to be called only when page loads.
The prepare method of the Preparable action class is called on every action execution, that's right. That might be the reason why you prepare the map for a drop-down in the preprocess method.
public class SampleAction extends ActionSupport {
private Map<String,String> circleIdNameMap;
private String circleId;
//getters and setters here
protected boolean reload = false;
private void preprocess(){
// Get the Map by calling a stateless Session bean
circleIdNameMap = remoteInterface.getMap();
}
public String action1(){
preprocess();
Map session = ActionContext.getContext().getSession();
session.put("circleIdNameMap ", circleIdNameMap );
return SUCCESS;
}
public String action2(){
Map session = ActionContext.getContext().getSession();
circleIdNameMap = (Map<String,String>)session.get("circleIdNameMap");
if (circleIdNameMap == null){
if (reload) {
preprocess();
Map session = ActionContext.getContext().getSession();
session.put("circleIdNameMap ", circleIdNameMap );
} else {
addActionError("circleIdNameMap is null");
return ERROR;
}
}
return SUCCESS;
}
...//other actions
}
the JSP for drop-down
<s:select name="circleId" list="circleIdNameMap" listKey="key" listValue="value"/>
The meaning of this code is: you should not return result SUCCESS or INPUT if fields in JSP aren't initialized.

Can I have multiple URLs map to an Action, changing a boolean parameter?

I have a spring (3.1) controller action method like this:
#RequestMapping(value="/{id}")
public String myAction(#PathVariable("id") long someId) { ... }
I want to introduce a boolean:
public String myAction(#PathVariable("id") long someId, boolean doBranch) { ... }
However, I want the boolean to be set based on which URL the method is accessed. In other words, I want
/foo/123 => myAction(123,false)
/debug/foo/123 => myAction(123,true)
I know I can add multiple urls to the RequestMapping annotation, but I haven't seen a way to have the mapping itself impact a parameter, short of passing in the parameter or course (/foo/123?doBranch=true").
Can I somehow easily extend Spring to do that? Or is there something already in there that would do that?
I know this isn't the most elegant way but:
#RequestMapping("/foo/{id}")
public String myActionA(#PathVariable("id") long someId) {
return myAction(someId, true);
}
#RequestMapping("/debug/foo/{id}")
public String myActionB(#PathVariable("id") long someId) {
return myAction(someId, false);
}
public String myAction(#PathVariable("id") long someId, boolean doBranch) { ... }
Simply map the URLs to 2 different methods that call a third method with either true or false.

Calling static methods in Play! framework controller doesn't work

I have a Play! framework with two actions which contain redundant code. So I factored this code into a private static method, but It doesn't work anymore then.
public static void show(long itemId, String listId) {
render(getItem(itemId, listId));
}
private static Item getItem(long itemId, String listId) {
// otherwise duplicate code ...
return item;
}
If I inline the code contained in getItem into the show action everything is fine:
// this works
public static void show(long itemId, String listId) {
Item item = // duplicate code ...
render(item);
}
Why can I not call other static methods within a Play! controller?
Solution
Thanks to 'Codemwnci' I've implemented the following solution:
public static void show(long itemId, String listId) {
renderArgs.put("item", getItem(itemId, listId));
render();
}
I prefer renderArgs because it makes the intention more clear than a local variable.
When you pass a local variable into the render method, the name of the local variable is used when passed through to the Groovy view. In your example, you are not passing a local variable, therefore Play does not know what name to give the item you have specified.
You have a couple of options. You can do either
Set the return from getItem to a local variable (item), and pass item into the view
Set the return from getItem into the renderArgs map, and specify your own name.
Option 1 is probably the most sensible.

Can an object know from which object its method is called?

In Moritz Haarmann's Blog I found an example of usage of Bonjour by Java. Here is the code taken from there:
public class ServiceAnnouncer implements IServiceAnnouncer, RegisterListener {
private DNSSDRegistration serviceRecord;
private boolean registered;
public boolean isRegistered(){
return registered;
}
public void registerService() {
try {
serviceRecord = DNSSD.register(0,0,null,"_killerapp._tcp", null,null,1234,null,this);
} catch (DNSSDException e) {
// error handling here
}
}
public void unregisterService(){
serviceRecord.stop();
registered = false;
}
public void serviceRegistered(DNSSDRegistration registration, int flags,String serviceName, String regType, String domain){
registered = true;
}
public void operationFailed(DNSSDService registration, int error){
// do error handling here if you want to.
}
}
I have a question about the "serviceRegistered" method. As far as I understand it is called during (or after) registration of the service (and it sets variable "registered" to be equal to "true"). But what is not clear to me is how exactly it is called. Because the service is registered by the method "registerService". This method, in its turn, calls "DNSSD.register". And, as far as I understand, the "DNSSD.register" will call the "serviceRegister" method of the "ServiceAnnouncer" class. But how "DNSSD.register" knows that it needs to call a method of the "ServiceAnnouncer" class? Can "DNSSD.register" know that it is called from a particular class (in this case "ServiceAnnouncer" class)?
The ServiceAnnouncer has passed itself as last argument of the DNSSD.register() method, which in turn is apparently expecting any instance of RegisterListener. This way the DNSSD can have a direct handle to the ServiceAnnouncer instance.
It seems that this class is a listener - namely RegisterListener. It has been registered as a listener in DNSSD by passing itself to the register(..) method.
For more information read about the Observer pattern.

Categories