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()
Related
We're using JSF 2.0 on WebSphere v8.5 with several component libraries PrimeFaces 4.0, Tomahawk 2.0, RichFaces, etc.
I am looking for generic mechanism to avoid form re-submission when the page is refreshed, or when the submit button is clicked once again. I have many applications with different scenarios.
For now I have considered disabling the button with a piece of JavaScript in onclick attribute, but this is not satisfying. I'm looking for a pure Java implementation for this purpose, something like the Struts2 <s:token>.
I am looking for generic mechanism to avoid form re-submission when the page is refreshed
For that there are at least 2 solutions which can not be combined:
Perform a redirect after synchronous post. This way the refresh would only re-execute the redirected GET request instead of the initial request. Disadvantage: you can't make use of the request scope anymore to provide any feedback to the enduser. JSF 2.0 has solved this by offering the new flash scope. See also How to show faces message in the redirected page.
Perform the POST asynchronously in the background (using ajax). This way the refresh would only re-execute the initial GET request which opened the form. You only need to make sure that those forms are initially opened by a GET request only, i.e. you should never perform page-to-page navigation by POST (which is at its own already a bad design anyway). See also When should I use h:outputLink instead of h:commandLink?
or when the submit button is clicked once again
For that there are basically also at least 2 solutions, which could if necessary be combined:
Just block the enduser from being able to press the submit button during the submit and/or after successful submit. There are various ways for this, all depending on the concrete functional and design requirements. You can use JavaScript to disable the button during submit. You can use JSF's disabled or rendered attributes to disable or hide the button after submit. See also How to do double-click prevention in JSF 2. You can also use an overlay window during processing ajax requests to block any enduser interaction. PrimeFaces has <p:blockUI> for the purpose.
Validate uniqueness of the newly added entity in the server side. This is way much more robust if you absolutely want to avoid duplication for technical reasons rather than for functional reasons. It's fairly simple: put a UNIQUE constraint on the DB column in question. If this constraint is violated, then the DB (and DB interaction framework like JPA) will throw a constraint violation exception. This is best to be done in combination with a custom JSF validator which validates the input beforehand by performing a SELECT on exactly that column and checking if no record is returned. A JSF validator allows you to display the problem in flavor of a friendly faces message. See also among others Validate email format and uniqueness against DB.
Instead of creating a token manually, you can use BalusC' solution. He proposed a Post-Redirect-GET pattern in his blog
Alternative solutions can be found in these answers:
Simple flow management in Post-Redirect-Get pattern
How can Flash scope help in implementing the PostRedirectGet (PRG) pattern in JSF2.0
<!--Tag to show message given by bean class -->
<p:growl id="messages" />
<h:form>
<h:inputText a:placeholder="Enter Parent Organization Id" id="parent_org_id" value="#{orgMaster.parentOrganization}" requiredMessage="Parent org-id is required" />
<h:commandButton style="margin-bottom:8px;margin-top:5px;" class="btn btn-success btn-block " value="Save" type="submit" action="#{orgMaster.save}" onclick="resetform()" />
</h:form>
public String save() {
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().getFlash().setKeepMessages(true); //This keeps the message even on reloading of page
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Your submission is successful.", " ")); // To show the message on clicking of submit button
return "organizationMaster?faces-redirect=true"; // to reload the page with resetting of all fields of the form.. here my page name is organizationMaster...you can write the name of form whose firlds you want to reset on submission
}
So, I have this jsf code
<h:inputText id="serviceId"/>
<h:commandButton value="Enter" action="agreementDetail.xhtml"/>
I need to send the customer's input in the serviceId field as a f:param to the next page (agreementDetail.xhtml). So, the customer inputs the value, clicks on the "Enter" button, and the next page receives the inputted value as a parameter.
Just use a regular GET form with a regular HTML button instead. JSF as being a POST form oriented framework offers no advantages for you here, so there's not really a point of using a JSF component here.
<form action="agreementDetail.xhtml">
<input name="serviceId" />
<input type="submit" value="Enter" />
</form>
For the case you didn't already know that, you can in the target view agreementDetail.xhtml use <f:viewParam> to process the GET request parameter and set it as a bean property like as if it's a <h:inputText>. See also What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
I am learning JSF and when I send an object to a bean parameter the instance of the object is not correct.
I have a page that list a lot of movies, if I want to see the complete information about a movie, I click on a link that send me to a page with this code:
<h:form>
<h:commandLink action="#{sharedMovie.share(movie)}" value="Share">
<div class="movieDetail">
<div class="movieDetailTitle">"#{movie.title}"</div>
<div class="movieDetailYear">"#{movie.year}"</div>
<div class="movieDetailSynopsis">"#{movie.synopsis}"</div>
</div>
</h:form>
The movie bean have a request scope.
The problem is that when I click on "Share" my class sharedMovie receives a new instance of Movie, instead of the displayed movie in the page. All the displayed data is correct, only when I call the other page that this problem happens.
I already tried sending just the movie ID (but it is always 0), using f:setPropertyActionListener, f:param, but nothing works. If I debug my project, when I click on share link it always hit the breakpoint inside the constructor of Movie class.
How I can pass this instance correctly?
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'
I want do display a login link when the user isn't logged in and a logout link when the user is logged in. I'm using container managed security as defined in web.xml.
How can I achieve this?
The username of the logged-in user is available by ExternalContext#getRemoteUser() which delegates under the covers to HttpServletRequest#getRemoteUser(). Both are available in EL by #{facesContext.externalContext.remoteUser} and #{request.remoteUser} respectively. If it is null, then it means that the user is not logged in.
So, in your view you can check it in the rendered attribute as follows:
<h:form rendered="#{not empty request.remoteUser}">
<h:commandLink value="Logout" action="#{auth.logout}" />
</h:form>
<h:link value="Login" outcome="login" rendered="#{empty request.remoteUser}" />
See also:
Conditionally displaying JSF components
This depends on your definition of "logged in". Usually you can login an user in your application by implementing your own login mechanism. Otherwise you are using some container dependent mechanism which your server will take care of.
For the container managed method you can usually check FacesContext with its ExternalContext.
FacesContext.getExternalContext().getRemoteUser();
You can put that method into a helper bean and check it with the rendered attribute of your link component.
If you implement your own system its totally up to you.
You may check session to know whether one is logged in or not (if you are using session to manage login information). Assuming you stored user information with the key user,here is an example:
<%
String page = "login.jsp";
String linkName = "Login";
if (session.getAttribute("user") != null) {
page = "logout.jsp";
linkName = "Logout";
}
%>
<%=linkName %>