问题
I've got the URL like this:
http://test.com/testapp/test.do?test_id=1&test_name=SS
Is there any way we can get only this part
/test.do?test_id=1&test_name=SS
回答1:
Use java.net.URL
to parse a url string:
URL url = new URL("http://test.com/testapp/test.do?test_id=1&test_name=SS");
System.out.println(url.getPath()+"?"+url.getQuery());
回答2:
Inside your ActionClass
String actionName = (String)ActionContext.getContext().get(ActionContext.ACTION_NAME);
will give you "test".
HttpServletRequest request = ServletActionContext.getRequest();
is your servlet request where from you can get all the parameters. Anyway, take a look at ActionContext.getContext(). Lot of thing you can get from there. Hope this helps.
回答3:
Not really URL parsing but this would do your job:
String[] parts = "http://test.com/testapp/test.dotest_id=1&test_name=SS".split("/");
return "/"+parts[parts.length-1]
来源:https://stackoverflow.com/questions/21545912/url-parsing-in-java