Retrieving value from Request URL
I have an URL which wil开发者_C百科l call a method my method in Java. It looks like this:
http://www.example.com/method?value=24
How can I retrieve value 24
and use it in my called method?
OK, the way to access a request parameter in wicket is this:
final Request request = RequestCycle.get().getRequest();
final String valueAsString = request.getParameter("value");
int value = Integer.parseInt(valueAsString);
But usually you don't have to do that, because you pass parameters by using BookmarkablePageLinks with PageParameters objects and read those objects from the page constructors. Read this page for some material on Bookmarkable Links in Wicket.
I am assuming here that you do create the number - the '24' - in your Java code, since you say you are using Wicket.
Thus, as seanizer already said, in 99% of the cases, you do not need to parse the request url to get the value, something like this should be sufficient:
public class MyPage extends WebPage {
public MyPage() {
// your number is generated somehow
final int value = 24;
this.add(new Link<Integer>("myLink") {
@Override
public void onClick() {
// do something with the value
int newValue = value * 2;
}
}
}
}
or - with models - like this
public class MyPage extends WebPage {
public MyPage() {
// your number is generated somehow
int value = 24;
Model<Integer> model = new Model<Integer>(value);
this.add(new Link<Integer>("myLink", model) {
@Override
public void onClick() {
// your '24'
Integer value = this.getModelObject();
// do something with the value
int newValue = value * 2;
}
}
}
}
If you really, really, REALLY-REALLY do need the parameter from the URL, I guess this is what you want:
public class MyPage extends WebPage {
public MyPage(PageParameters parameters) {
// your number is generated somehow
Integer value = parameters.getAsInteger("value");
}
}
Depending on how you configured your application, you might need to implement the other constructors accordingly.
Good luck.
If you are using Servlets, you can use
ServletRequest.getParameter(...)
If you use JSP, you can use the request
implicit object
request.getParameter(...)
Something like this:
public class ExampleServlet extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s = request.getParameter("value");
int value = 0;
try {
value = Integer.parseInt(s);
} catch(NumberFormatException nfe) {
nfe.printStackTrace();
}
}
}
One of:
- in a bookmarkable page, create constructor with single argument PageParameters and get the value from there: p.getString("value") or p.getInt("value)
- also page level but if you don't have that constructor or are e.g. in a component: getPage().getPageParameters() and get it from there
- from any component, getRequest().getParameter("value") (or use Request.get() instead)
There are even more ways of getting it, but these are the most straightforward.
精彩评论