Thanks in advance for reading my question.
Question: I want original url in my strut's interceptor.
E.g:
From the browser, suppose i called... http://www.fakedomain.com/mycode/test.html
But this url is a url without rewrite...
Actually, Struts framework rewrite this into http://www.fakedomain.com/CreateMyCode.action
So, sceanario is...
When i fired the url from the browser...
First, Strut's filter comes in a picture and it determines weather url rewrite is required or not. If it requires then
Again same strut's filter is called modifdies into another url.
Finally, strut's interceptor comes in a picture. But now when i tried to get url... i got modified url.
But i want the original url (which is not rewrite by struts...and which is actual called from the browser).
How can i get this?
My Try:
I have created one new filter... which comes before urlrewrite filter. And set some attribute like...
HttpServletRequest httpReq = (HttpServletRequest) request;
String uri = httpReq.getRequestURI();
httpReq.setAttribute("URLBeforeRewrite", uri);
But in interceptor when i tried to get the value like...
req.getAttribute("URLBeforeRewrite")
i got modified url.
now, how can i solve this???
Related
I have an asp.net-mvc site and I send out a lot of email from my site. Before I used to send email from my controller and I used this code to get the base url:
protected string GetBaseUrl()
{
return string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
}
that is because i obviously need fully formed URL. so in a normal link on a page that i have href="/GoHere", i would want that to translate to:
"http://www.mysite.com/GoHere"
not just relative URLs given that they are going in emails like
"/GoHere"
I am now refactoring my controller to move all of this code outside of it, but i find myself passing this baseURL string around (because the function above relies on Request which is in namespace:
System.Web
and I can't seem to access this request object outside the controller class. Right now I am passing a string BaseURL all over the place so when i need to generate the emails, i can append the relative URL after the base URL but that feels very hacky
Is there a better way to get the baseURL (either through the Request object or not) outside of a controller class in an asp.net-mvc website?
UrlHelper.Action has an overload that accepts "protocol". Use this to generate full urls that conform to your routing table.
var baseUrl = Url.Action("Index", "Home", null, Request.Url.Scheme);
MSDN Source
In a default project MVC with default routing, this would return something like:
http://mydomain.com
Note that if you can't use/access the HttpRequest object, consider passing in the scheme or if you know it's always "https" (for example) then you can use a magic (hard coded) string or read it from a settings/config file.
You can access the HttpRequest object outside of the controller by using:
var request = System.Web.HttpContext.Current.Request;
On a similar note, you will have the same problem accessing the UrlHelper instance of the controller. You can create an instance like so:
var url = new UrlHelper(System.Web.HttpContext.Current.Request.RequestContext);
HttpRequest.RequestContext is new to .NET 4.0. I thought I'd not that seeing as though you haven't specified a working version. MSDN source.
The problem is determining the baseURL programmatically as it is highly relative and a website is reachable at many URLs. For example:
* http://localhost/GoHere locally
* http://localhost:8080/GoHere locally on another port
* http://127.0.0.1/GoHere locally by the loop back address
* http://10.0.10.5/GoHere the internal IP of the machine
* http://72.34.56.78/GoHere the public IP of the machine
* http://www.mysite.com/GoHere preferred public URL
* http://www.bestsiteever.com/GoHere SEO URL
It would be easier to set the baseURL in a config file, a database by customer, or whatever way you need to correlate the baseURL to the email.
I want to get the URL accessed by the Struts2 web application. For instance, if user accesses http://www.webpage.com or https://www.webpage.com, I need the protocol; http or https. I tried implementing the action with ServletRequestAware and using request.isSecure() or request.getScheme(). However, I am getting false and 'http' even if I access 'https://..' link.
Is there any way to get the protocol ?
You can extract it from the header:
System.out.println(request.getHeader("referer"));
OUTPUT:
https://www.webpage.com
I think you forgot to try one more thing.
Implement Action with ServletRequestAware and use
request.getProtocol();
Else you can use the following code in your action class:
HttpServletRequest request=ServletActionContext.getRequest();
String protocol=request.getProtocol();
System.out.println("Protocol Used::"+protocol);
I'm using Symfony 1.4 and i want to forward in my controller to another controller and action with some parameters.
After creating a "bike" with "bike/create" i want to forward to "bike/show/id/X" with the id i got from my new bike instance.
$forwardString = 'bike/show/id/'.$bike->id;
$this->forward($url);
This does not work :-(
Maybe you can help! :)
Greetings!
The method definition for forward is public function forward($module, $action) the request should be preserved and if theese are things not currently in the request you will have to add them first.
Also you might even need redirect instead so the url changes and where you just give it the url public function redirect($url, $statusCode = 302) so usage would be $this->redirect('bike/show?id=' . $bike->id);
is there a way to access URL parameters in a Jetspeed2 Portlet/Portal?
like: www.bla.com/portal/page.psml?param=12345
I can only find some tools for liferay (PortalUtil.java) to access the httpservletrequest, but as far as i know there is no such thing for jetspeed?
I thought the public render parameters can be used for such thing, but i'm a little confused here? Didn't anyone had this problem before?
thanks in advance :)
found the answer:
Until version 2.1, Jetspeed merged portal request parameters with portlet specific
parameters, effectively allowing "shared" parameters.
This is not compliant with the JSR-168 PLT.11, so by default this is now disabled.
By setting merge.portal.parameters.with.portlet.parameters=true this feature can
be "restored".
In the situation of portal and portlet parameters with the same name, by default
the portlet parameters will be provided first in the values array, but this too
can be overridden by setting merge.portal.parameters.before.portlet.parameters=true
Setting both these properties to true will deliver the "old" pre-2.1 behavior.
Note: for individual portlets, these global settings can be overridden by setting these properties as metadata in jetspeed-portlet.xml
merge.portal.parameters.with.portlet.parameters=false
merge.portal.parameters.before.portlet.parameters=false
To use public render parameters from the 2.0 spec
Don't know if it works for jetspeed, but you can try getting the httprequest like this:
HttpServletRequest httpRequest = (HttpServletRequest) request.getAttribute("javax.servlet.request");
Then find the query string in a header like this:
String referer = httpRequest.getHeader("referer");
You'll then have the full page referer so you can parse the query string.
I have a ActionLink, that calls my public ActionResult, and I would like it to return back to the page that it was called from, but how?
There are a couple of tricks that you can use for this.
The simplest is ...
return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);
AbsoluteUri may not give you the exact path you are looking for, but UrlReferrer should have the imformation you are looking for. Redirect returns a subclass of ActionResult so it is a valid return value.
Another idea is to base the redirect location off of stored values. This is useful when you are going to make multiple requests before you want to redirect, such as when you validate a form and show validation issues on the first response. Another situation will be when the referrer is not a local site. In either case, your referrer won't be what you want it to and you will need to retrieve the correct location from somewhere else.
Specific implementations include using a hidden input field on your form, session state, pulling a descriminator value from your route data, or even just a more constant value like HttpContext.Request.ApplicationPath.
Good luck.
Keep in mind that due to the state-less nature of the web, your ActionResult isn't "called from" your ActionLink as much it is simply a url that the user-agent requested.
Because of this, the only real "built-in" way you can know where that user was coming from is by inspecting the http Request headers to see what the referring page was:
string referrer = Request.Headers["referer"];
You'd then be responsible for parsing out the Action method from this url, if you were going to call it directly. Be aware that this referrer may not be a link within your own site.