java parameter in jsp:include - jspinclude

I wanted to know if I can pass a java variable to a . I tried to do something like this inside a jsp file:
<% String folder = request.getParameter("something"); %>
<jsp:include page="../<%=folder%>/myframe.jsp" ></jsp:include>
This does not work. I get an error which says 'Failed to find resource /=folder/myframe.jsp'.
Can this be done?
Thanks for any help.

You either need to have the page parameter as an entire string or as an entire substitution.
<jsp:include page="<%= myPageToInclude %>" />
<jsp:include page="path/to/my/page.jsp" />
Also when concatenating strings inside a value you need to escape the double quotes.
So for your problem :
<% String folder = request.getParameter("something"); %>
<jsp:include page="<%= \"../\" + folder + \"/myframe.jsp\" %>" />
Should work fine, or for easier reading :
<%
String folder = request.getParameter("something");
String page = "../" + folder + "/myframe.jsp";
%>
<jsp:include page="<%= page %>" />

Concidering your param name as "something",
Try this:
<jsp:include page="../${param.something}/myframe.jsp" />
Other solution is to do that:
<% String folder = request.getParameter("something"); %>
<% pageContext.setAttribute("folder", folder); %>
<jsp:include page="../${folder}/myframe.jsp"></jsp:include>
First is better but both should works.

Related

OpenXava: Retain URL after load image

The default behaviour for uploading image, using Stereotype("PHOTO") will change the url. For example, localhost:8080/m/Activity?Uid=15&Year=2017 will change to localhost:8080/m/Activity?application=Survey&module=Activity once image loaded. I have tried to retain the url with the code shown below but it does not work. Can somebody help me on how to retain the previous url?
ImageEditor.jsp
<%# include file="../imports.jsp"%>
<%# page import="org.openxava.model.meta.MetaProperty" %>
<%# page import="org.openxava.web.Ids" %>
<%
String propertyKey = request.getParameter("propertyKey");
MetaProperty p = (MetaProperty) request.getAttribute(propertyKey);
boolean editable="true".equals(request.getParameter("editable"));
String uId = request.getParameter("UId");
String year = request.getParameter("Year");
long dif=System.currentTimeMillis(); // to avoid browser caching
%>
<img id='<%=propertyKey%>' name='<%=propertyKey%>' src='<%=request.getContextPath()%>/xava/ximage?UId=<%=uId%>&Year=<%=year%>&property=<%=propertyKey%>&dif=<%=dif%>' title="<%=p.getDescription(request)%>" alt=""/>
<% if (editable) { %>
<span valign='middle'>
<xava:link action='ImageEditor.changeImage' argv='<%="newImageProperty="+Ids.undecorate(propertyKey)%>'/>
<xava:action action='ImageEditor.deleteImage' argv='<%="newImageProperty="+Ids.undecorate(propertyKey)%>'/>
</span>
<% } %>
Thanks.
Brian
OpenXava changes the URL after uploading, it works in that way. You should store the parameter values in a session object to not lossing them.

Open liferay's portlet in pop-up and after success refresh the calling portlet

We have a requirement to open liferay's add/edit folder/document screen in a dialog pop-up of our custom plugin portlet.
And after successful (no exceptions or errors) save it should automatically close the pop-up and refresh just our plugin portlet on the page.
Following is the code we have used to open liferay's pop-up:
<liferay-portlet:renderURL var="addFolderURL" portletName="<%=PortletKeys.DOCUMENT_LIBRARY %>" windowState="<%=LiferayWindowState.POP_UP.toString() %>">
<liferay-portlet:param name="struts_action" value="/document_library/edit_folder" />
<liferay-portlet:param name="redirect" value="<%= currentURL %>" />
<liferay-portlet:param name="repositoryId" value="<%= String.valueOf(repositoryId) %>" />
<liferay-portlet:param name="parentFolderId" value="<%= String.valueOf(folderId) %>" />
</liferay-portlet:renderURL>
<%
String addFolderJavascript = "javascript:Liferay.Util.openWindow({dialog: {destroyOnHide: true}, id: 'addFolderPopUpId', title: '"
+ ((folder != null) ? UnicodeLanguageUtil.get(pageContext, "add-subfolder") : UnicodeLanguageUtil.get(pageContext, "add-folder"))
+ "', uri: '" + HtmlUtil.escapeJS(addFolderURL) + "'});";
%>
<aui:a href="<%= addFolderJavascript %>" label='<%= (folder != null) ? "subfolder" : "folder" %>' />
The difficulty is how would we know when the add/edit document is successful, how would we close the pop-up and then refresh the portlet since we don't have any control over the add/edit screen coming from liferay. And even if we do it through hook the problem is how to know the success/failure of the document.
Thanks
I never thought the answer would be in Liferay's code itself and was pretty simple.
Liferay also uses such a functionality in Asset Publisher portlet, as shown below:
So liferay after adding the blogs, document etc closes the pop-up and refreshes the asset-publisher portlet. It does this with a simple redirect trick.
Three things:
you require a jsp which would have the redirect and close script.
a redirect url which will call the above jsp after successful save.
In liferay redirect parameter is always passed as a parameter to almost all action requests and on successful save it redirects to the URL passed in this parameter
the popup-id which can be referenced from the jsp.
The jsp is /asset_publisher/add_asset_redirect.jsp and its code is as shown below which can be used in your plugin as it is (I have used comments to explain somethings):
<%
String redirect = request.getParameter("redirect");
redirect = PortalUtil.escapeRedirect(redirect);
Portlet selPortlet = PortletLocalServiceUtil.getPortletById(company.getCompanyId(), portletDisplay.getId());
%>
<aui:script use="aui-base">
Liferay.fire(
'closeWindow', // closes the pop-up window
{
id: '<portlet:namespace />editAsset', // the id of the pop-up which we gave
portletAjaxable: <%= selPortlet.isAjaxable() %>,
<c:choose>
<c:when test="<%= redirect != null %>">
redirect: '<%= HtmlUtil.escapeJS(redirect) %>' // redirects the page to this URL
</c:when>
<c:otherwise>
refresh: '<%= portletDisplay.getId() %>' // refreshes only the portlet
</c:otherwise>
</c:choose>
}
);
</aui:script>
So now lets achieve what we intend to step-by-step in our plugin:
have a jsp similar to add_asset_redirect.jsp in your plugin.
build the redirect url like:
<%
PortletURL customRedirectURL = liferayPortletResponse.createLiferayPortletURL(themeDisplay.getPlid(), portletDisplay.getId(), PortletRequest.RENDER_PHASE, false);
customRedirectURL.setParameter("mvcPath", "/html/common/add_asset_redirect.jsp"); // mvcPath so that your custom portlet's render() calls this jsp when a redirect from liferay's page happens
/* customRedirectURL.setParameter("redirect", themeDisplay.getURLCurrent()); // set this if you want to redirect to a certain page, if only refresh is your motto then leave this commented. */
customRedirectURL.setWindowState(LiferayWindowState.POP_UP);
String customRedirect = customRedirectURL.toString();
// popUp-id while opening the pop-up. should be same as that in add_asset_redirect.jsp
String customPopUpId = renderResponse.getNamespace() + "editAsset";
%>
build the code to open the pop-up:
<liferay-portlet:renderURL var="addFolderURL" portletName="<%=PortletKeys.DOCUMENT_LIBRARY %>" windowState="<%=LiferayWindowState.POP_UP.toString() %>">
<liferay-portlet:param name="struts_action" value="/document_library/edit_folder" />
<liferay-portlet:param name="redirect" value="<%= customRedirect %>" />
<liferay-portlet:param name="repositoryId" value="<%= String.valueOf(repositoryId) %>" />
<liferay-portlet:param name="parentFolderId" value="<%= String.valueOf(folderId) %>" />
</liferay-portlet:renderURL>
<%
String addFolderJavascript = "javascript:Liferay.Util.openWindow({dialog: {destroyOnHide: true}, id: '"
+ customPopUpId +"', title: '"
+ ((folder != null) ? UnicodeLanguageUtil.get(pageContext, "add-subfolder") : UnicodeLanguageUtil.get(pageContext, "add-folder"))
+ "', uri: '" + HtmlUtil.escapeJS(addFolderURL) + "'});";
%>
<aui:a href="<%= addFolderJavascript %>" label='<%= (folder != null) ? "subfolder" : "folder" %>' />
that's all. Go check-it out.
Hope this helps.

Embed route value parameter in MVC Url Action

I am struggling with the MVC syntax for setting up the image src with the below code. I use Webform view engine and following the syntax
<img src="<%: Url.Action("LoadPhoto", "EmployeeProfile",
new { empno = "<%: ViewBag.EmpNo%>"}) %>" />
getting this error Newline in constant
How to embed this ViewBag value as a route parameter for Action?
As you are already in code, the following should work:
<img src="<%: Url.Action("LoadPhoto", "EmployeeProfile",
new { empno = ViewBag.EmpNo }) %>" />
(I'm reasonably sure the newline mentioned in the error isn't the one between "EmployeeProfile", and new { ... }.)
ViewBag is not supported by MVC 2. You should use ViewData instead like this:
<img src="<%: Url.Action("LoadPhoto", "EmployeeProfile",
new { empno = ViewData["EmpNo"] }) %>" />

GET/POST variable not set in netbeans IDE

I dunno if its a silly mistake but a post/get variable is not being set, while it is supposed to be. Here are the HTML and php code snippets:
<html>
<head>
<title>
Chain Story
</title>
</head>
<body>
<form method="GET" action="check-valid.php">
<textarea name="a" rows="5" cols="50"></textarea>
<input type="submit" value="Add" />
</form>
</body>
</html>
check-valid.php:
<?php
require 'includes/connect.inc.php';
$conn_ref = connect_db('chainstory') or die(mysqli_error());
if(isset($_GET)){
echo 'Get variable set';
if(isset ($_GET['a'])){
$as = $_GET['a'];
$query = "insert into story1 values (1, " . $as . ")";
mysql_query($query, $conn_ref);
}
else{
echo $_GET;
}}
?>
I get the following output:
Get variable set
Notice: Array to string conversion in /home/kevin/Code/php/myWebsite/check-valid.php on line 15
Array
I am coding this in netbeans. Can anyone point me out what mistake im making? :(
Did you try rename the textarea? Longer name , and give id to textarea same the name.
What browser does you use for testing?
I met some problems with input names in IE for example if an input name matches a javascript function name or protected names. Have you a javascript function or variable in your code what's name is a ? Because if the name of the input conflicts with a js var or name IE does not send the input field to the server. (Chrome Firefox and other browsers does)

Rails - what is the correct syntax for adding current url to Facebook comments div href?

I've tried every variation I can think of to add the current url path to the href in the facebook comments div. Why doesn't this work?
<div class="fb-comments" data-href="<%= #{request.protocol}#{request.host_with_port}#{request.fullpath} %>" data-width="470" data-num-posts="2"></div>
I've also tried this but the end of the ruby injection code seems to end the div this way:
<div class="fb-comments" data-href=<%= "#{request.protocol}#{request.host_with_port}#{request.fullpath}" %> data-width="470" data-num-posts="2"></div>
Posting another way in case someone needed it, you were close in the first attempt, no need for the #{} since it is already inside the <%= %>
This worked for me:
<div class="fb-comments" data-href="<%= request.protocol + request.host_with_port + request.fullpath %>" data-width="470" data-num-posts="10"></div>
However i faced a small issue using this method to retrieve current_url since any params in the Url will affect where the comment is attached
e.g http://localhost:3000/users/1?locale=en will store different comments than http://localhsot:3000/users/1?locale=ar Which is not what i wanted!!
So, i found that using request.path instead of full_path solves this:
<div class="fb-comments" data-href="<%= request.protocol + request.host_with_port + request.path %>" data-width="470" data-num-posts="10"></div>
I needed to use a content_tag which converted the original facebook comments div:
<div class="fb-comments" data-href="http://example.com" data-width="470" data-num-posts="2"></div>
into this:
<%= content_tag(:div, nil, :class => 'fb-comments', "data-href" => "#{request.protocol}#{request.host_with_port}#{request.fullpath}", "data-width" => '470', "data-num-posts" => "2" ) %>