I am trying to get the UUID from a user.
So far i tried
Accessing the user from a liferay portlet?
Get the current user Liferay using a simple Java code
putting String userId = renderRequest.getRemoteUser() into the view.jsp worked to get the intern ID.
However i wanted the UUID instead.
If i use the code from the links above (into the java-class doView) i only get a null-user object.
Using getUserUuid() and getUuid() returns null.
Here is my class:
ThemeDisplay td =(ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
User user = td.getUser();
String userId = user.getUuid();
renderRequest.setAttribute("myUser", userId);
and my view.jsp
<%
String userId = (String) renderRequest.getAttribute("myUser");
%>
<%= userId %>
Any help is appreciated.
On JSP, extract your parameter from implicit request object. Like:
<%
String userId = (String) request.getAttribute("myUser");
%>
Related
I was trying to do this in my servlet:
Date date = new Date();
request.setAttribute("status", status);
request.setAttribute("date", date);
if (status.equalsIgnoreCase("Incorrect password")|| status.equalsIgnoreCase("Username not found")) {
request.getRequestDispatcher("error.jsp").forward(request,response);
}
else {
request.getRequestDispatcher("success.jsp").forward(request,response);
}
and this inside my success.jsp:
<%
String stat = (String) request.getAttribute("status");
String timestamp = (String) request.getAttribute("date");
%>
<p>Welcome, <%= stat %>! </p>
<p>TimeStamp : <%= timestamp %> </p>
I got a 500 internal server error from the code above then I decided to use a separate servlet for my date object and forwarded the request to the same jsp but the I got a null value after this.
Why is it that my first implementation got a 500 internal server error and how come I got a null value on my second implementation? How can I solve this?
I am trying to use SpringMVC to handle a form but now I am stuck and I'm here to ask for your help.
When the user submit the form, my controller returns the jsp result page and the user can check what he filled in. In that result page I have 2 buttons, one to send definitively the form and one to modify it.
Here my problem : when I click on modify all the fields of the form are again empty and I would like to keep the value filled by the user.
My controller :
#Controller
#RequestMapping( "/adhesion" )
public class FormController {
//Send the form
#RequestMapping( method = RequestMethod.GET )
public String getFormulaire( #RequestParam( value = "user", required = false ) T_demande_adhesion User, Map<String, Object> model ) {
System.out.println( "user" + User ); //Null everytime
model.put( "user", User );
System.out.println( "Envoi formulaire !" );
return "formulaireAdhesion";
}
//Handle the form
#RequestMapping( method = RequestMethod.POST )
public String gestionAdhesion( #ModelAttribute( "formAdhesion" ) #Valid T_demande_adhesion user, BindingResult erreurForm, Map<String, Object> model )
throws ServletException, IOException {
final String CHEMIN_DOC_APP = "C:/DocumentsApp/";
// Form JSP page
final String FORMULAIRE_ADHESION = "formulaireAdhesion";
// Result page
final String AFFICHER_RESULT = "afficherResult";
// Objet de validation du formulaire
ValidationDmdAdhesion demandeAdhesion = new ValidationDmdAdhesion();
//Return AFFICHER_RESULT or FORMULAIRE_ADHESION
String resultat = demandeAdhesion.validationUser( user, erreurForm, model, FORMULAIRE_ADHESION, AFFICHER_RESULT, CHEMIN_DOC_APP );
//putting the object to the request
model.put( "user", user );
return resultat;
}
}
In JSP side I added the parameter 'User' in the request :
'
<input type="button" value="Modifier" onclick="document.location.href='<c:url value="adhesion?user=${user}"/>';" class="boutonAdhesion" />
'
But this send me a string and the url is :
/adhesion?user=com.user.entities.T_demande_adhesion#1dfe8158
When the user clicks on modifiy button (which is in the result page), I would like to get this object #ModelAttribute( "formAdhesion" ) #Valid T_demande_adhesion user on the GET request to fill in the form, in purpose to avoid the user to put again the same informations.
Any help ?
An extract from the form :
<label for="nom">Nom <span class="requis">*</span></label><input type="text" id="nom" name="identite.nom" value="<c:out value ="${user.identite.nom}"/>" size="35" maxlength="30" />
<spring-ext:errors path="identite.nom" class="erreur" firstErrorOnly="true"/>
It's here : value="<c:out value ="${user.identite.nom}"/> that I want to get the value of nom.
Sorry for the English.
Frenchy.
When I click on modify all the fields of the form are again empty and
I would like to keep the value filled by the user
Because the form data is already available on the page at client side, there is no meaning of submitting the form again to the server & show the same data, which is an unnecessary round trip from the client to the server i.e.,
To keep the data on the form, do NOT to submit the form to the server/controller itself.
How can I check the validation of the information filled in without
submit the form? How can I display them in my result page?
There is no need to validate the data that the user is anyhow going to modify. So, the validation of the form will be done only after the user completes the modifications and clicks on Submit. Once the validations are successful/failed, your can show the result on the results page.
In this case, I didn't find another way than using #SessionAttributes. In this point off my application I didn't to use a session but it seems that there isn't another way.
#RequestMapping(value = "",method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", doctorDetails);
return "doctorchannelling/index";
}
I have added this controller in my SpringMVC project now I need to access the element doctorDetails in a java code which is inside a jsp
how can I call this doctorDetails inside <% %> Mark
Something like below
<% String message = (String)request.getAttribute("message"); %>
or
<%
String message = ${message};
%>
You can simply use the EL to print them as,
${message}
If it is a String set in the request. For Model objects use ,
${message.fieldName}
See Also
How to avoid Java code in JSP files?
printing servlet request attributes with expression language
I am trying to pass a string value to a JavaScript function by taking from request parameter in JSP, in my struts based project. here is the code:
<%
String timeVal = "Not found";
if(request.getAttribute("myDate")!=null){
timeVal= (String)request.getAttribute("myDate");
}
%>
and then pass it in function as parameter
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('update', <%=timeVal %>)">Save</html:submit>
Where the JavaScript function is
function SubmitPage(action, aa)
{
alert("Date is ...." + aa);
}
But when i try to run this it gives me an error
HTTP Status 400 - Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
With message on web page.
Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
Thanks in advance.
EDIT Here is stack trace
[ERROR] DispatchAction - -Request[/AMResourceLibraryListAction] does not contain handler parameter named ref
it's work for me :
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('<%=timeVal %>')">Save</html:submit>
('<%=timeVal %>') // between single Quotation
Rather using that i will advise you to use value like this in your JavaScript function
var tt = <%=(String)request.getAttribute("myDate")%>
alert(tt+ "Done this....");
Hope this will help you.
Use '<%=timeVal %>' instead of <%=timeVal %> in Javascript method:
<html:submit property = "save" styleClass = "button_c" onclick = "return SubmitPage('update', '<%=timeVal %>')">Save</html:submit>
I'm trying to create a webb application using Struts 2 and javascript and I'm having some trouble passing data from my action into my javascript.
This is the list I'm trying to send/access:
List<MarkerClass> markers;
MarkerClass is defined acoprding to belove:
final class MarkerClass
{
public Integer objectId;
public String street;
public String streetNumber;
public String zip;
public String city;
public Integer statusId;
public Float lattitude;
public Float longitude;
}
The action also includes a getter for markers:
public List<MarkerClass> getMarkers()
{
return markers;
}
In my jsp-file I have tried doing this:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function initialize()
{
var titel = "";
for(var i, "${markers}")
{
titel = titel+ "${markers[i].street}";
}
}
The browser substitutes "${markers}" with "[se.fubar.test.MarkerClass#37a4, se.fubar.test.MarkerClass#9ad97c]"
I'm guessing there is a better and smarter way to do this but since I'm a bit new to Struts and is trying to code while under the influence of a migrane the answer elludes me.
You cannot just use a struts variable in a javascript function and expect it to work. Remember that the ${...} stuff gets processed before the HTML for the page is sent to the browser. By the time the javascript is rendered at the browser, you are only left with the textual representations. What you will need to do is something like (check the syntax, I haven't used this stuff i a while):
function initialize() {
var title = "";
<c:foreach var="marker" list="${markers}">
title = title + "${marker.street}";
</c:foreach>
}
Something along those lines anyway... Basically the Javascript seen by your browser will look like
function initialize() {
var title = "";
title = title + "Street1";
title = title + "Street2";
title = title + "Street3";
title = title + "Street4";
}
I hope that makes sense and is related to what you were asking.
By the way, there are usually better ways of accomplishing this functionality that building dynamic js etc. Probably there are built in Struts 2 components that you can use?
you would have to set that variable in request or in session and then access it using a <c:out jsp tag like so
var myVar= '<c:out value="${requestScope.myVar}"/>';
then use the var inside your js.
In case you set an object in request or session you have to use the get method to access the value of an attribute then use it like so:
var myVar= '<c:out value="${requestScope.myObj.attribute}"/>';
(assuming you getter method is getAttribute)
it is not possible to access data in session or request directly from js
hope this helps
You could convert the object to json on the server (see http://www.json.org/java/index.html ) and then call eval() on the string to get a javascript representation of your object.
you can try accessing it something like this.
but you have to use a loop to fetch each object from the list place it on the value stack and than fetch each object of it.else you can ask ognl to do it for you.something like
<script type="text/javascript">
function initialize()
{
var titel = "";
for(var i, "${markers}")
{
titel = titel+ <s:property value="%{markers.get(i).street}">;
}
}
just try it since OGNL has capacity to access the object on the value stack