I have the URL: http://demo.lendingclub.com/account/summary.action. When visit this url, it will first go to the authenticate interceptor, in the interceptor class, If I use:
String uri = req.getRequestURI();
it will return /account/summary.action
But if I use it in jsp:
<%
HttpServletRequest req = ServletActionContext.getRequest();
String uri = req.getRequestURI();
%>
it will return : /mainapp/pages/account/summary.jsp
Why they're different, and how can I get action URL in JSP?
You could get the action URL or any other value if you set property to the action, and then retrieve that property from the value stack via OGNL.
private String actionURL;
public String getActionURL(){
return actionURL;
}
the code to calculate the action URL is similar you posted to the comments
public String getPath(){
ActionProxy proxy = ActionContext.getContext().getActionInvocation().getProxy();
String namespace = proxy.getNamespace();
String name = proxy.getActionName();
return namespace+(name == null || name.equals("/") ?"":("/"+name));
}
this code is not supported .action
extension, if you need to add the extension to the path then you need to modify this code correspondingly.
then write your action method
public String excute() {
actionURL = getPath();
...
return SUCCESS;
}
in the JSP
<s:property value="%{actionURL}"/>
you have been used dispatcher result to forward request to the JSP as a result you get the URI pointed to the JSP location.
The easiest way to get the current actions url is: <s:url/>
if you supply namespace and action parameters you can make it point at other actions but without these parameters it defaults to the current url.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With