Reputation: 489
I created an action in struts 2
class MyAction extends ActionSupport {
private List<User> users;
//getters and setters users
public String execute(){
users=//code to get users;
return SUCCESS;
}
}
I mapped this action class with url /MyAction and the success result is a jsp which displays the users when i open the url:
http: //localhost:8080/MyAction.action
When I open the page as:
http: //localhost:8080/MyAction.action?xyz=123
I do not get any error. But when I open the page as:
http: //localhost:8080/MyAction.action?users=123
Struts tries to call setUsers()
method and fails to convert 123 to a list of Users. Hence I get an Conversion error.
Since the action is not expecting any parameter it should behave in same way to all extra parameters provided. It should not just ignore few and show error for others.
The solution that I was able to find for this problem is to make all the setter methods private except for the setters for expected parameters.
Does anyone has a better solution?
Upvotes: 1
Views: 470
Reputation: 489
Thanks for your replies. I understand that this is struts functionality, but what I'm pointing out is that if struts is ignoring other parameters (for which there is no setter) it should ignore all the parameters that the action does not require. But yes, there should be some way for specifying the list of acceptable parameters.
As Quaternion pointed out ParameterNameAware is a solution but this works when there is only one action method in the action class or all the action methods must accept the same list of parameters.
I have found another way of specifying the list of acceptable parameters for each action using the params interceptor.
<action name="xxx" class="yyy" method="action1">
<intercecptor-ref name="defaultStack">
<param name="params.excludeParams">regex</param>
</intercecptor-ref>
<!-- other code -->
</action>
in place of regex you specify the regular expression for the excluding the parameters except for the ones that are not excepted by your expression.
Upvotes: 0
Reputation: 499
Anu's right. I think what you are trying to fix is not a problem, it is the framework's functionality itself.
Upvotes: 0
Reputation: 10458
You can use ParameterNameAware.
The above link should make the usage clear.
Although as anu was suggesting, your action is generally a public interface. Struts2 exposes setters as a convenience, and a great convince it is. You should only have setters for input. The action then does all of its work at another layer (Business), which typically in turn uses another layer(or tier if you prefer), which accesses persistent data. You probably know this but by only putting what is appropriate in the action layer, life is made less complicated (your issue never comes up).
Upvotes: 1