Liferay request current page name - java

What is the smartest way to get current page name where request came from? By page I mean the real page name that contains current portlet.

By using something like that you should be ok
ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
String title = themeDisplay.getLayout().getName(themeDisplay.getLocale());

just be aware,themeDisplay.getLocale() could result different char encoding format, use with caution, may cause certain string functions to fail

Related

In Spring Boot, can we pass anchor tag, as a variable, through URL like #PathVariable or #RequestParam?

I would like to use URL http://localhost:8080/blog#section & obtain #section as a variable in getBlog() method.
// localhost:8080/blog#section
#GetMapping("/blog/{section}")
public String getBlog(Model model, #PathVariable("section") String section){
return "blog" + section; // localhost:8080/blog#section
}
The #section part is never sent to server (see this answer),so what you are demanding is impossible.
In fact #section is used for browser to locate the content of a web page, it's not used for server to decide which content to return.
What you are demanding can be easyily achieved with a query like localhost:8080/blog?section=? or a url path like localhost:8080/blog/section

Request parameter is modified in my servlet

I sent one request as URL with data to servlet, But by default servlet is modifying the data and sending as request. Can you please suggest how to maintain the request URL with data which i passed to servlet should remain same ?
Example:- when i am passing the data to servlet
http://localhost/helloservlet/servlet/ppd.abcd.build.coupons.CouponValueFormatterServlet?dsn=frd_abc_abcde&lang=ENG&val=PRCTXT|12345 &ABCDEFG
when it using the above url in servelt as request , like string abc = request.getParameter("val"), the val attribute is trimmed automatically and assigned as " val=PRCTXT|12345" but it supposed to be like " val = PRCTXT|12345 &ABCDEFG ". Please help me on this.
The servlet interprets each & in the URL as the start of a new parameter. So when it sees &ABCDEFG, it thinks you are sending a new parameter called ABCDEFG with no value (though this is technically a "keyless value" according to the specifications).
Two things to fix this, first is when you want to actually send an &, use %26 instead. This will be skipped by the code that divides up the parameters, but converted to a real & in the parameter's value.
Second is to replace spaces with +. Spaces in URLs work sometimes but can be problematic.
So your actual request URL should look like this:
http://localhost/helloservlet/servlet/ppd.abcd.build.coupons.CouponValueFormatterServlet?dsn=frd_abc_abcde&lang=ENG&val=PRCTXT|12345+%26ABCDEFG
If you're building these parameters in javascript, you can use encodeURIComponent() to fix all problem characters for you. So you could do something like this:
var userInput = *get some input here*
var addr = 'http://www.example.com?param1=' + encodeURIComponent(userInput);

How to get an xsp value from URL link using Java

I am trying to retrieve the page name(.xsp)from the URL of the current page using Java. i have been able to accomplish the same thing with the Javascript below
context.getUrl().getSiteRelativeAddress(context).toString()
and it works but i want to get the same thing don using Java.
The best way to get SSJS variable names via Java is resolveVariable. This should work:
XSPContext context = (XSPContext) ExtLibUtil.resolveVariable(FacesContext.getCurrentInstance(), "context");
String pageName = context.getUrl().getSiteRelativeAddress(context).toString();
(Updated with correct syntax for second line, thanks Knut)

How to add a parameter to a URL in GWT?

I have a Java/GWT application. In that there is a list of items. If I click on any item title then that item is opened with full description.
I am using Anchor for the item title, so what I want is when user clicks on item title then in the URL the id of that item is appended to the current URL.
For example, this is my URL:
"http://127.0.0.1:8888/MyApp.html?gwt.codesvr=127.0.0.1:9997#listItem?list"
and I have to append id to the end of the URL like:
"http://127.0.0.1:8888/MyApp.html?gwt.codesvr=127.0.0.1:9997#listItem?list&itemId=55"
Using Window.Location should do your trick : see the doc here
Something like this :
String url = Window.Location.getHref();
url = url + "&itemId=" + itemId;
Window.Location.replace(url);
Although of course, as Crollster pointed out, you should insert your url parameter before the # sign. Give more details on what you're looking for exactly (why do you have to add the parameter manually, does the page have to reload ...)
you can use redirect command in order to add this parameter
response.sendRedirect(your url + itemId=55);
Then you can extract this variable.
I hope this will help.
You can try with javascript coding.When the user clicks on link, get this URL and appends your id to it and reconstruct the URL.
You see that # in the URL? Thats an anchor - you will need your parameter to be added before that, so it looks like this:
http://127.0.0.1:8888/MyApp.html?gwt.codesvr=127.0.0.1:9997&itemId=55#listItem?list
HTH
URIBuilder of Apache HttpComponents offers a convenient method to add parameters and will deal with existing query parameters and anchors.

Get current URL in Webapplication

I am in process to capture Current URL as its being displayed in the browser's address bar in my JSP page and have few options to get it done.
Using javax.servlet.include.request_uri and other defined in Servlet 2.4 specification.I refer this thread to get details about it java-httpservletrequest-get-url-in-browsers-url-bar.
In my current application, we are going to put web-server in front of our application server as than it seems that those values will be of not any use.
I have another way to take help of javascript's document.URL but i am not sure how reliable it is going to be.
I need to get the details about the location of the user and if I can use getRequestURI(), it will return me something like www.abc.com/abc/search.jsp.
In short, all I want to capture the URL being there in the address bar of the browser and save it in a hidden field of my JSP page.
I am not sure what is the best way to achieve this.
In Java, you can do this:
public static String getCurrentUrl(HttpServletRequest request){
URL url = new URL(request.getRequestURL().toString())
String host = url.getHost();
String userInfo = url.getUserInfo();
String scheme = url.getProtocol();
String port = url.getPort();
String path = request.getAttribute("javax.servlet.forward.request_uri");
String query = request.getAttribute("javax.servlet.forward.query_string");
URI uri = new URI(scheme,userInfo,host,port,path,query,null)
return uri.toString();
}
If you want a javascript solution, you can use window.document.location object and its properties:
console.log(window.document.location.protocol);
http:
console.log(window.document.location.host);
stackoverflow.com
console.log(window.document.location.port);
console.log(window.document.location.href);
http://stackoverflow.com/questions/10845606/get-current-url-in-webapplication
console.log(window.document.location.pathname);
/questions/10845606/get-current-url-in-webapplication
You can understand other parameters reading this article at MDN.
You can create a hidden field in your form
<input type="hidden" id="myurl" name="myurl"/>
then write a javascript
<script type="text/javascript">
document.getElementById('myurl').value = window.location.href
</script>
is that help?

Categories

Resources