How can I get the values submitted from a JSP in Liferay? - liferay

I am using Liferay Portal 6 version.
How can I get the UserName and Password values with in the same page?
<%# taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%#page import="javax.portlet.RenderRequest"%>
<portlet:defineObjects />
This is the <b>Kiran</b> portlet.
<form>
<p><b>UserName:</b> <input type="text" name="UserName" size="10"></p>
<p><b>Password:</b> <input type="Password" name="Password"
size="10"></p>
<p><input type="submit" value="Submit" name="submit"><input type=
"reset" value="Reset" name="reset"></p><hr><hr>
</form>
<%
// here
%>

I'm not sure if its submitting the values correctly as your form has no target and is not refering to a portlet action url.
This tutorial shows some basic usage and parameter retrieval. Check the JSP portlet section. You should be able to access the request object in your jsp as well.
I wouldn't start writing JSP portlets. Its quite outdated nowadays. Check Spring Portlet MVC or even consider JSF.

In a portal/portlet all identifiers must be properly namespaced - you never know what other content ends up on the same html document with you. Thus a form control should rather read:
<input type="text" name="<portlet:namespace/>user" .../>
in order to be able to retrieve the parameter as "user" from the request.
If, in Liferay 6, you use the AlloyUI taglibs, a lot of this namespacing is done automatically for you.
Also, you should add the portlet action URL as Udo Held suggests:
<form action="<portlet:actionURL/>">

What are you trying to get ? Do you want to get the username and password in some other .java file or .jsp file ?? Or do you want to get the username and password once a user logs in ?
If you are trying to get user details set in present jsp page in some other .java or .jsp, then simply use the PortletSession.
Eg: From jsp
PortletSession portletSession = actionRequest.getPortletSession();
portletSession.setAttribute("liferayUserMap", liferayUserMap,PortletSession.APPLICATION_SCOPE);
From .java/.jsp
PortletSession portletSession = actionRequest.getPortletSession();
portletSession.getAttribute("liferayUserMap",PortletSession.APPLICATION_SCOPE);
By doing so, you can share the data among different files in different portlets too.
For case 2: If you are trying to get the user details, simply do the following:
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
themeDisplay.getUser();
I hope you are following the portlet structures while coding, otherwise the above mentioned code wouldn't work. Since you have to point to some action class, in the 'struts-config' and 'tiles-def'

Related

Control generated ID of ui:repeat

Sorry for the simple question. I am learning to use JSF 2.2 to create a form and trying to keep it close to plain HTML5 as possible. I have an ui:repeat generated list that goes like this:
<ul id="nameLst">
<ui:repeat var="d" value="#{controller.names}" varStatus="status">
<li>
<input
jsf:id="nameTxt"
jsf:binding="#{nameTxt}"
jsf:value="#{d}"
type="hidden" />#{d}
</li>
</ui:repeat>
</ul>
It gets rendered like this:
<ul id="nameLst">
<li>
<input id="j_idt14:0:nameTxt" name="j_idt14:0:nameTxt" value="Name1" type="hidden">
Name1
</li>
<li>
<input id="j_idt14:1:nameTxt" name="j_idt14:1:nameTxt" value="Name2" type="hidden">
Name2
</li>
</ul>
Now, I am trying to add names using JavaScript only to this list. Problem is, how can I control this generated id, so I can use it in JavaScript. And mainly, if the list starts empty, how do I generate this id so it can be correctly posted back to the managed bean.
how can I control this generated id, so I can use it in JavaScript
Just give it a fixed id.
<ui:repeat id="names" ...>
Alternatively, use jsfc attribute to turn <ul> into an <ui:repeat>.
<ul id="names" jsfc="ui:repeat" value="#{bean.names}" var="name">
<li>
<input jsf:id="name" type="hidden" value="#{name}" />
</li>
</ul>
And mainly, if the list starts empty, how do I generate this id so it can be correctly posted back to the managed bean
Use JSF for this instead of JS. An example can be found here: How to dynamically add JSF components
See also:
How can I know the id of a JSF component so I can use in Javascript
JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used

How to fetch UserId of the User submitting the form

I am using Liferay 6.1.1.
I have created a portlet with <aui:form> having two text fields and a button.
On submit I want to know the userId who submitted the below form:
<aui:form action="<%=myUrl%>" method="post">
<aui:input type="text" name="name"></aui:input>
<aui:input type="text" name="addr"></aui:input>
<aui:button type="submit" value="save"></aui:button>
</aui:form>
How can I get the userId?
I hope the Users are login into your portal before you show them this form.
If yes than you can look at the answers of this question which show a JSR-286 specific way and a liferay specific way to get the UserId.
And if you are showing this form and letting guest users i.e. any anonymous person to submit this form, then obviously you can't get the UserId.
You can use the PortalUtil. On the Controler side :
User user = PortalUtil.getUser(actionRequest)
in your jsp :
User user = PortalUtil.getUser(renderRequest);
Don't forget to handle exceptions, or user being not null.
Type this in your java code:
String userId=ParamUtil.getString(portletRequest, param);
On your java code use,
get ThemeDisplay from the request(actionRequest) and get userid from
themeDisplay.getUserId()

jsf 2 components doesnt have name attribute

In my application after clicking to a link I want to focus to a part of a page after the page load. With static html we can do this by code parts below.
Go to Chapter 4
and the chapter 4 is defined;
<h2><a name="C4">Chapter 4</a></h2>
<p>This chapter explains ba bla bla</p>
In jsf 2 I can not find even name attribute in order to use for this purpose.
Any help will greatly be appreciated.
In JSF, the name attribute is computed based on the combination of the 'id' atribute of the jsf component and the 'id' attribute of the form. The reason for this is explained by BalusC here. So if you specify an id for a JSF component, then the name is also computed for it.
Anyways, starting HTML5 usage of name attribute for <a> tag is made obsolete instead it is recommended to use the id attribute of the nearest container. So do not rely on the name attribute.
Though you can try to focus any of these:
<h2 id="top4">This is a heading</h2> or
<h2><a id="top4">This is a heading</a></h2>
Or simply focus a container on the target page like this:
In HTML 4.01
<div id="top4">
<h2>This is a heading</h2>
<p>This is a paragraph</p>
</div>
In HTML 5
<article id="top4">
<h2>This is a heading</h2>
<p>This is a paragraph</p>
</article>
Now in JSF, this is how you focus the container on the target page:
When the outcome is a different page but in the same application:
<h:link id="link1" value="link1" outcome="welcome" fragment="top4" />
where the outcome is welcome.xhtml which is relative to the context root, and "top4" is the id of the container to be focused when the target page is rendered.
When linking an external site:
<h:outputLink id="link2" value="http://www.msn.com/#spotlight">link2</h:outputLink>
Within the same page:
<h:link id="link3" value="link3" fragment="top4" />
Here there is no outcome specified, so the outcome will be the same page.
See also:
http://dev.w3.org/html5/markup/a.html
h:link
h:outputLink

Liferay 6: Portlet View in MAXIMIZED State

Once the request is made to the MVCPortlet class , i want to show the View in MAXIMIZED State .
This is my Code in JSP Page
<portlet:actionURL name="addBook" var="addBookURL" />
<aui:form action="<%= addBookURL.toString() %>" method="post">
<aui:input type="text" name="name"></aui:input>
<aui:input type="password" name="pwd"></aui:input>
<aui:button type="submit"></aui:button>
</aui:form>
This is my MVCPortlet class :
if(true)
{
response.setPortletMode(PortletMode.VIEW);
response.setWindowState(WindowState.MAXIMIZED);
}
else
{
response.setPortletMode(PortletMode.VIEW);
response.setWindowState(WindowState.MAXIMIZED);
}
And this is the result (Please see the image below )
My question is that , i was expecting that once the Action class is called , so the Portlet will be displayed in MAXIMIZED State .
But still the Portlet remains in NORMAL State , please let me know why ?and how can i Portlet View in MAXIMIZED State after the result from MVCPortlet class .
You have two possibilities how to display portlet in maximized mode. You either add windowState parameter with value maximized to the actionURL tag (it will allways show the link to portlet in maximized state), or you put this line
response.setWindowState(WindowState.MAXIMIZED);
into processAction method of your portlet (you can make a decision based on something).
(this is what specification says, but Liferay usually adheres to it).

The <t:message> doesn't display properly when using <t:inputFileUpload> inside <t:PanelTabbedPane>

In a JSP page, I created a <h:form enctype="multipart/form-data"> with some elements: <t:inputText>, <t:inputDate>, etc. Also, I added some <t:message for="someElement"> And I wanted to allow the user upload several files (one at a time) within the form (using <t:inputFileUpload> ) At this point my code works fine.
The headache comes when I try to put the form inside a <t:panelTabbedPane serverSideTabSwitch="false"> (and thus of course, inside a <t:panelTab> )
I copied the structure shown in the source code for TabbedPane example from Tomahawk's examples, by using the <f:subview> tag and putting the panelTab tag inside a new jsp page (using <jsp:include page="somePage.jsp"> directive)
First at all, the <t:inputFileUpload> fails to load the file at the value assigned in the Managed Bean UploadedFile attribute #{myBean.upFile}
Then, googling for a clue, I knew that <t:panelTabbedPane> generates a form called "autoform", so I was getting nested forms. Ok, I fixed that creating the <h:form> out of the <t:panelTabbedPane> and eureka! file input worked again! (the autoform doesn't generate)
But, oh surprise! oh terrible Murphy law! All my <h:message> begins to fail. The Eclipse console's output show me that all <t:message> are looking for nonexistents elements ID's (who have their ID's in part equals to they are looking for, but at the end of the ID's their names change)
At this point, I put a <t:mesagges> tag (note the "s" at the end) to show me all validation errors at once at the beginning of the Panel, and it works fine. So, validation errors exists and they show properly at the beginning of the Panel.
All validation error messages generated in this page are the JSF built-in validation messages. The backing bean at this moment doesn't have any validators defined.
¿How can I get the <t:message for="xyz"> working properly?
I'm using Tomahawk-1.1.6 with myFaces-impl-1.2.3 in a eclipse Ganymede project with Geronimo as Application Server (Geronimo gives me the myFaces jar implementation while I put the tomahawk jar in the WEB-INF/lib folder of application)
"SOLVED": This problem is an issue reported to myFaces forum.
Thanks to Kyle Renfro for the soon response and information. (Good job Kyle!)
See the issue
EDIT 1
1.- Thanks to Kyle Renfro for his soon response. The forceID attribute used inside the input element doesn't works at first time, but doing some very tricky tweaks I could make the <t:message for="xyz"> tags work.
What I did was:
1. Having my tag <inputText id="name" forceId="true" required="true"> The <t:message> doesn't work.
2. Then, after looking the error messages on eclipse console, I renamed my "id" attribute to this: <inputText id="namej_id_1" forceId="true" required="true">
3. Then the <t:message> worked!! but after pressing the "Submit" button of the form the second time. ¡The second time! (I suspect that something is going on at the JSF lifecycle)
4. This implies that the user have to press 2 times the submit button to get the error messages on the page.
5. And using the "j_id_1" phrase at the end of IDs is very weird.
EDIT 2
Ok, here comes the code, hope it not be annoying.
1.- mainPage.jsp (here is the <t:panelTabbedPane> and <f:subview> tags)
<%# taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%# taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%# taglib prefix="t" uri="http://myfaces.apache.org/tomahawk"%>
<html>
<body>
<f:view>
<h:form enctype="multipart/form-data">
<t:panelTabbedPane serverSideTabSwitch="false" >
<f:subview id="subview_tab_detail">
<jsp:include page="detail.jsp"/>
</f:subview>
</t:panelTabbedPane>
</h:form>
</f:view>
</body>
</html>
2.- detail.jsp (here is the <t:panelTab> tag)
<%# taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%# taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%# taglib prefix="t" uri="http://myfaces.apache.org/tomahawk"%>
<t:panelTab label="TAB_1">
<t:panelGrid columns="3">
<f:facet name="header">
<h:outputText value="CREATING A TICKET" />
</f:facet>
<t:outputLabel for="ticket_id" value="TICKET ID" />
<t:inputText id="ticket_id" value="#{myBean.ticketId}" required="true" />
<t:message for="ticket_id" />
<t:outputLabel for="description" value="DESCRIPTION" />
<t:inputText id="description" value="#{myBean.ticketDescription}" required="true" />
<t:message for="description" />
<t:outputLabel for="attachment" value="ATTACHMENTS" />
<t:panelGroup>
<!-- This is for listing multiple file uploads -->
<!-- The panelGrid binding make attachment list grow as the user inputs several files (one at a time) -->
<t:panelGrid columns="3" binding="#{myBean.panelUpload}" />
<t:inputFileUpload id="attachment" value="#{myBean.upFile}" storage="file" />
<t:commandButton value="ADD FILE" action="#{myBean.upload}" />
</t:panelGroup>
<t:message for="attachment" />
<t:commandButton action="#{myBean.create}" value="CREATE TICKET" />
</t:panelGrid>
</t:panelTab>
EDIT 3
On response to Kyle Renfro follow-up:
Kyle says:
"At the first view of the page, if you press the "CREATE TICKET" button with nothing in any of the inputTexts and no files uploaded, do the message tags work for the inputTexts? (ie. required = true) I'm just curious if the messages for the inputTexts are working but the message for the inputFileUpload is not."
Here is the behavior found:
1.- There is no validation error messages shown at all (the message tags don't work) Even when I try to test only one validation error message (for example, testing the message for the first input text) none of them shows up.
2.- The eclipse console shows me these internal errors:
ERROR [HtmlMessageRendererBase] Could not render Message. Unable to find component 'ticket_id' (calling findComponent on component 'j_id_jsp_1383779881_1:subview_tab_detail:j_id_jsp_1716158401_0j_id_1:j_id_jsp_1716158401_5j_id_1'). If the provided id was correct, wrap the message and its component into an h:panelGroup or h:panelGrid.
ERROR [HtmlMessageRendererBase] Could not render Message. Unable to find component 'description' (calling findComponent on component 'j_id_jsp_1383779881_1:subview_tab_detail:j_id_jsp_1716158401_0j_id_1:j_id_jsp_1716158401_8j_id_1'). If the provided id was correct, wrap the message and its component into an h:panelGroup or h:panelGrid.
ERROR [HtmlMessageRendererBase] Could not render Message. Unable to find component 'attachment' (calling findComponent on component 'j_id_jsp_1383779881_1:subview_tab_detail:j_id_jsp_1716158401_0j_id_1:j_id_jsp_1716158401_14j_id_1'). If the provided id was correct, wrap the message and its component into an h:panelGroup or h:panelGrid.
Here is when I saw the "j_id_1" word at the generated IDs, for example, for the id "ticket_id":
j_id_jsp_1383779881_1:subview_tab_detail:j_id_jsp_1716158401_0j_id_1:j_id_jsp_1716158401_5j_id_1
And, viewing the resulting HTML generated page, I saw that the IDs names are like this (whitout using "ForceId" atribute):
<input id="j_id_jsp_1383779881_1:subview_tab_detail:j_id_jsp_1716158401_0j_id_1:ticket_idj_id_1" name="j_id_jsp_1383779881_1:subview_tab_detail:j_id_jsp_1716158401_0j_id_1:ticket_idj_id_1">
The forceId attribute of the tomahawk components should solve this problem.
something like:
<t:outputText id="xyz" forceId="true" value="#{mybean.stuff}"/>
At the first view of the page, if you press the "CREATE TICKET" button with nothing in any of the inputTexts and no files uploaded, do the message tags work for the inputTexts? (ie. required = true) I'm just curious if the messages for the inputTexts are working but the message for the inputFileUpload is not.
Looks like it may be related a bug in myfaces. There is a newer version of myfaces and tomahawk that you might try. I would remove the subview functionality as a quick test - copy the detail.jsp page back into the main page.
https://issues.apache.org/jira/browse/MYFACES-1807?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12567158#action_12567158

Resources