How can I get the values submitted from a JSP in L

2020-04-26 05:26发布

问题:

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> &nbsp;&nbsp;<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


%>

回答1:

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.



回答2:

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/>">


回答3:

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'