Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get action url in jsp using Struts 2

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?

like image 876
阳光Emi Avatar asked May 21 '13 08:05

阳光Emi


2 Answers

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.

like image 41
Roman C Avatar answered Nov 14 '22 20:11

Roman C


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.

like image 144
Quaternion Avatar answered Nov 14 '22 21:11

Quaternion