Java EE – how to access URL parameter struts 2 in action class

I'm new to Java EE and struts 2 I need to know if I did something wrong

I have a link like this: http://localhost:8080/myProject/deleteUser?idUser=42

What I want is to get the iduser value

This is how I get the parameter value in the action class:

HttpServletRequest request = (HttpServletRequest) ActionContext.getContext()
                                  .get(ServletActionContext.HTTP_Request);
System.out.println(request.getParameter("idUser"));

Solution

S2 provides a clean way to get the request parameters in your action class, just follow these simple rules

>Create an attribute with the same name as the request parameter name. > Create getters and setters for this property or make the property public (for s2.1)

S2 will check the request parameters, find the matching attribute in the action class, and inject the value into the relevant attribute

In your case, what you need to do

public class MyAction extends ActionSupport{

 private String idUser;
 getter and setters   

}

So in this case, S2 will find the iduser attribute in your action class, and its construction in the interceptor will inject this value into the iduser attribute

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>