Linking two jsp pages with Submit Button Liferay - forms

I have a form where I allow user to select some data and submit the form and based on that selection data will be displayed in another jsp.
I have used the following code in first jsp:
<aui:form name="updateDailyAttendance" action = "<%=request.getContextPath()%> /admin/test.jsp" method="post" >
<input type = "date" name = "myDate"/>
<input type = "submit" name = "Submit"/>
</aui:form>
test.jsp is the second JSP. But the code above isn't working. How should I mention the second jsp name in the "action" above so that the above jsp takes me to second jsp. I am using lIferay

instead of passing a url as 'action', you should provide an actionUrl with the jsp page as param.
<portlet:actionURL var="actionName" name="yourMVCPortletFunctionName">
<portlet:param name="jspPage" value="/admin/test.jsp" />
</portlet:actionURL>
<aui:form name="updateDailyAttendance" action = "<%= actionName %>" method="post" >
<input type = "date" name = "myDate"/>
<input type = "submit" name = "Submit"/>
</aui:form>
then in Your Controller:
public void yourMVCPortletFunctionName(ActionRequest actionRequest, ActionResponse actionResponse){
throws PortletException, IOException
//Do your stuff
//Redirect
String redirectUrl = (String)request.getParameter("jspPage");
actionResponse.setRenderParameter("jspPage", redirectUrl);
}
This way you can have actions that do some standard stuff, like handling that "myDate" param, and have them redirect to other pages each time. So calling them from different points (different jsp page or form), will target ta a new redirect each time

Related

JAX-RS, #FormParam display result in the same page

I have this program that I am coding, the main thing is mostly to get familiar with Jax-rs and rest API. I am sending from a form in JSP page one input and with POST I pass it to my java Response class(that I use annotation #FormParam), it does what calculation I need and then I get the result in another link, what can i do to take the result just below my button on the same page? For my button function I use javascript, and when I push it I don't want to be redirected and take the result like this.
So i use this form and a javascript function in my jsp to pass the value in my POST method in java throw #FormParam
<form name="Form1" method="post">
<p>
Name : <input type="text" name="malename" id="malename" autofocus
value="enter your name..." />
</p>
<input type="submit" name="action" id="malename" value="I'm feeling lucky"
onclick="OnButton1();"> </form>
function OnButton1() {
document.Form1.action = "webapi/perfectmatcher/mate"
document.Form1.target = "_self"; // Open in a new window
document.Form1.submit(); // Submit the page
//return;
}
Here after i have the value i use two other classes to do the calculations and return the result i need. In a few words i pass a value in my form and when i push the button i am redirected in another link(the path of POST) and i display the result. I want to display the result below my button. I hope the code is helpful and i hope i am clear with my explanation
#POST
#Path("/mate")
#Produces(MediaType.TEXT_PLAIN)
public Response postMaleName(#FormParam("malename") String malename) {
String femalename = NumMatcher.numMatcher(Numberizer.numberizer(malename));
// FemaleName female = new FemaleName(femalename);
female.setName(femalename);
System.out.println(female.getName());
return Response.status(200).entity("Perfect Match for " + malename + " is " + femalename).build();
}
P.S.:The object female that i am trying to create its for testing purposes, i was trying somehow to get the information that i store but i cannot find how!!I used even jsp expressions but i get null value of course ...
<%
FemaleName female = new FemaleName("john");
String string = female.getName();
out.print(string);
%>

what error is in the code snippet below?

I have the following jsp code which uses struts tag:
<input type = "radio"
id = "<s:property value="name"/>"
name = "<s:property value="name"/>"
value = "<s:property value="value"/>"
<s:if test="fieldValue==null">
<s:if test="defaultOption==true">
checked="checked"
</s:if>
</s:if>
<s:else>
<s:if test="value==fieldValue">
checked="checked"
</s:if>
</s:else>
/>
Eclipse says that:
Start tag (input) not closed properly, expected >.
But I cannot find where is the error. Please help.
It is probably an eclipse bug because with Netbeans there is no error
if you dont want to see the error with Eclipse (and in that case only) :
in the first if, put test like that :
fieldValue==null && defaultOption==true
and get rid of the internal if
it should be the same
PS : try to find a way to use s:radio maybe
Eclipse complains because it does not accept tags (for example <s:if>) inside input tag, you can deactivate JSP validation as suggested in comments .
Go to project properties and uncheck JSP Content Validator, then clean your project
If you don't want to disable validation, here is a suggested solution:
<s:if test="fieldValue==null">
<s:if test="defaultOption==true">
<input type = "radio" id = "<s:property value="name"/>" name = "<s:property value="name"/>" value = "<s:property value="value"/>" checked="checked" />
</s:if>
</s:if>
<s:else>
<s:if test="value==fieldValue">
<input type = "radio" id = "<s:property value="name"/>" name = "<s:property value="name"/>" value = "<s:property value="value"/>" checked="checked" />
</s:if>
</s:else>
Just as a note, your JSP code give same result(I guess it's just for illustration :) ).

how to pass parameters from jsp to java class using RESTFUL

i hav created a jsp page from which i m taking some values and on submit it should pass the parameters to the java class using rest.
< form id="payment" method="post" action="addtogroup">
< ol style="padding: 0;">
< li>< label for="groupId">Group id:< /label>
< input type="text" id="groupId" name="groupId"/>
< br />
< li>< label for="vendorId">Profile Id :< /label>
< input type="text" id="vendorId" name="vendorId"/>
< li>
< input type="submit" value="Submit"/>
< br />
< /ol>
< /form>
and the java code is:
#RequestMapping(value = "/addtogroup/{groupId}/{vendorId}",method = RequestMethod.POST)
public String addtoGroup(#ModelAttribute("groupId") String groupId,#ModelAttribute("vendorId") String profileId){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String username = auth.getName();
System.out.println("group id is="+groupId);
System.out.println("profileId is="+profileId);
System.out.println("username is="+username);
grpDao.addToGroup(groupId,profileId,username);
return "addtogroup";
}
when i type [http://localhost:8080/biznex/rest/addtogroup/2/13] in the address bar directly the code is executed.
but when i click submit button in the jsp i get page not found error.
plz help me out.
< form id="payment" method="post" action="addtogroup"> This statement means submit the FORM data using POST method to the url "currentpath"/"addtogroup"
However, your RESTFUL Server side component expects the url in the form of /addtogoup/{groupid}/{vendorid} in a GET method
I would suggest you to have a JavaScript method which converts your form fields to the URI path- using jQuery or plain JavaScript
As thewhiterabbit wrote, you have to pass the complete REST url with the groupid and vendorid.
In order to build it you don't need to use JS, it's enough the spring tag library. Here, how it works:
<spring:url value="/addtogoup/${form.groupid}/${form.vendorid}" var="actionURL"/>
<form:form method="POST" action="${actionURL}" modelAttribute="form">

(Lift) Ajax submit using SHtml.select onchange event

I'm tring to implement the classic functionality of this select:elements depends on this other select:selection using Lift, E.g. select the country and get the possible states for the selected country.
The problem that I'm facing is that the "this.myForm.submit()" that I have inside the onchange of the select element is not firing the ajax request. If I use an input type"submit" it works perfectly.
Is this behaivior related with Lift framework? Is there a better way of implementing this kind of functionality using Lift libraries?
The relevant snipped code:
"name=distributionPoints" #> SHtml.select(distributionPoints, Empty, selectedDistributionPoint = _, "id" -> "the_distributionPoints") &
"name=devices" #> (SHtml.select(devices, Empty, selectedDevice = _, "id" -> "the_devices") ++ SHtml.hidden(process))
The view html:
<form class="lift:form.ajax">
<select name="distributionPoints" id="the_distributionPoints" onchange="submit()">
<option></option>
</select>
<br/>
Device:
<select name="devices" id="the_devices">
<option></option>
</select>
</form>
The rendered HTML:
<form id="F391649920812YBACEZ" action="javascript://" onsubmit="liftAjax.lift_ajaxHandler(jQuery('#'+"F391649920812YBACEZ").serialize(), null, null, "javascript");return false;">
<div>
Punto de distribuciĆ³n:
<select onchange="submit()" id="the_distributionPoints" name="F630704816482OLP514"></select>
<br />
Equipo:
<select name="F391649920817BLRJW5" id="the_devices"></select><input type="hidden" name="F391649920818HPS35E" value="true" />
<br />
</div>
</form>
[edit]
I finally got the solution. Like Chris mentioned I used ajaxSelect instead of just selects and instead of using setHtml there's a method called JsCmds.ReplaceOptions that does exactly what I was looking for.
You should understand that when using Ajax submit the page is not reloaded. So I would suggest you to use JsCmds.setHtml on server side to "reset" the second select element.
So, in fact the first select is an ajaxSelect which is meant to modify the second one (so it is not concerned by the hidden submit in my opinion). The second select is updated when the first one is changed, using 'selectPoint(s)'
Piece of Scala code
def selectPoint(s):JsCmd = {
selectedDistributionPoint = s;
newDevices:List[String] = findCorrespondingDevices(s);
JsCmds.setHtml("name=devices", SHtml.select(newDevices, Empty, selectedDevice = _, "id" -> "the_devices")) ++ SHtml.hidden(process))
}
"name=distributionPoints" #> SHtml.AjaxSelect(distributionPoints, Empty, s=> selectPoint(s), "id" -> "the_distributionPoints") &
"name=devices" #> (SHtml.select(Nil, Empty, selectedDevice = _, "id" -> "the_devices") ++ SHtml.hidden(process))
Piece of template code
<input name="distributionPoints" id="the_distributionPoints" onchange="submit()"/>
<input name="devices" id="the_devices"/>

How can I pass FORM on a button click in MVC2.0?

I am calling a controller method SendMe(formCollection myForm){}.
But I don't know how to pass Form that contains some information. I know how to read it, and also, how to call SendMe() on given controller, but I don't know how to pass it. Though, for the reference I am calling SendMe as:
<div id="menucontainer">
<ul id="menu">
<li><%: Html.ActionLink("Send", "SendMe", "Mail")%></li>
</ul>
</div>
and my SendMe method is:
public ActionResult SendMe(FormCollection Form)
{
string From = Form["From"].ToString();
string Pass = Form["Password"].ToString();
string To = Form["To"].ToString();
string Subject = Form["Subject"].ToString();
string Message = Form["Message"].ToString();
MailMessage nmsg = new MailMessage(From, To, Subject, Message);
nmsg.IsBodyHtml = true;
nmsg.Priority = MailPriority.High;
SmtpClient smtpc = new SmtpClient("smtp.gmail.com", 587);
smtpc.Credentials = new System.Net.NetworkCredential(From, Pass);
smtpc.EnableSsl = true;
smtpc.Send(nmsg);
return View();
}
I haven't put anything in Model, though, MailController and SendMe.aspx are in place.
tell me how to get through this. Thanks in advance.
This simple code should work...if all you need is this to submit information from a form...Please clear if i understood it wrong..I would recommend using a viewmodel or something though to have validation for those fields...and in viewmodel case you should inherit the page from the viewmodel...and retrieve from the viewmodel object instead of formcollection...
<% Html.BeginForm("SendMe", "Send", FormMethod.Post); %> //Here 1st parameter is the actionname while second is the Controller name
//Form Fields....
<input id="SendMe" type="submit" value="Send Info" />
<% Html.EndForm(); %>
In order to obtain the FORM variables in your controller, the form needs to be submitted via a Http POST.
<% using (Html.BeginForm()) {%>
.....
<% Html.EndForm(); %>