Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

jakarta ee - How to access url parameters in Action classes Struts 2

I'm new to Java EE and Struts2. I need to know if I'm doing it wrong or not.

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

All I want is to get the idUser value.

Here is what I use to get the parameter value in my action class :

HttpServletRequest request = (HttpServletRequest) ActionContext.getContext()
                                  .get(ServletActionContext.HTTP_REQUEST);
System.out.println(request.getParameter("idUser"));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

S2 provides a clean way to fetch the request parameters in you action class all you need to follow these simple rules.

  1. Create a property with same name as request parameter name.
  2. create getter and setters for this property or make property public (for S2.1+)

S2 will check the request parameter and will look for matching property in your action class and will inject the value in respected property.

in your case all you need to do

public class MyAction extends ActionSupport{

 private String idUser;
 getter and setters   

}

So in this case S2 will find the idUser property in your action class and its build in interceptor will inject the value in the idUser property


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...