Struts2 case passing variables

I am using dagatables server side ajax and need to pass some variables to the server. My server is running Struts2 actions to handle these data requests.

I am facing some problems because datatables pass predefined internal variables (e.g. iDisplayStart, iDisplayLength, iColumns, sSearch), but Struts2 cannot get this kind of camel (first char is lowercase and second is uppercase).

To ensure this, I created this test action:

@Action (value = "dummy", 
    results = { 
        @Result(name="ok", type="httpheader", params={"status", "200"}) } 
    ) 

@ParentPackage("default")
public class DummyAction {
    private String xTrace;

    public String execute () {
        System.out.println( xTrace );
        return "ok";
    }

    public String getxTrace() {
        return xTrace;
    }

    public void setxTrace(String xTrace) {
        this.xTrace = xTrace;
    }

}

      

I am calling this url:

localhost:8580/sagitarii/dummy?xTrace=thisisatest

      

The output is NULL, but when I change xTrace to xyTrace (and get, set and url too) everything goes well. How can I get this to work?

* EDIT *

I have already tried with any word in this format: iPad, iMac, iPhone, eMail, ... I think it might just be my configuration, but please try before answering questions.

+3


source to share


3 answers


but Struts2 can't get this kind of camel (first char is lowercase and second is uppercase)

In fact, this is not the case. Struts2 can receive any variable name that conforms to the JavaBeans specification. But besides the Java implementation, this spec. (if you want to know more about JavaBeans, see this post What is a JavaBean exactly? ) it uses its own implementation. Since Struts2 uses OGNL to access bean properties, you should be aware that the "property" is OGNL.

What is a property? Roughly speaking, an OGNL property is the same as a bean property, which means that a get / set method pair, or alternatively a field, defines a property (the full story is a little more complicated as properties differ for different object types, see below for a full explanation).

And you can find a full explanation here: Referring to Properties .

I won't go into detail on how OGNL handles object properties, but it must follow a contract PropertyAccessor

. There are many implementations for different kinds of "properties". One that OGNL uses for objects ObjectPropertyAccessor

. Here's just a description of what it does:

An implementation PropertyAccessor

that uses reflection of the target's class to find a field or set / get method pair with a given property name.



You can find out how to do this by looking at the source code. I'll just say that it uses the first letter of the property name before using it with a method prefix get/set

. So you asked

How can I get this to work?

Change getter / setter method signature for properties

public String getXTrace() {
    return xTrace;
}

public void setXTrace(String xTrace) {
    this.xTrace = xTrace;
}

      

UPDATE:

Since OGNL 3.0.11 single lowercase letter is not uppercase.

+1


source


If the variable

private String xTrace;

      

getters and setters should be



public String getXTrace() {
    return xTrace;
}

public void setXTrace(String xTrace) {
    this.xTrace = xTrace;
}

      

regardless of double consecutive uppercase characters. The rule is that the first character after the set / get element is uppercase, the rest is unchanged

+3


source


I figured it out by fetching variables with HttpServletRequest and bypassing the struts way.

HttpServletRequest req = (HttpServletRequest)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
xTrace = req.getParameter("xTrace");

      

This will display the variables correctly.

EDIT

Better and elegant way:

As Alexander M commented, changing the setter name of two performers to uppercase makes struts set the data correctly.

+1


source







All Articles