Want to pass the java value into javascript function in jsp - java

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>

Related

Jsp how to write html string with response sendredirect

I need to write a html string which redirects onclick but it is not working, I think it might be because of the double and single quotes but I don't know the correct code.
String html = "<input type = 'button' value = 'redirect' onclick = 'response.sendRedirect('redirect.jsp')'>";
Why are you not using standard Javascript for that?
String html = "<input type = 'button' value = 'redirect' onclick = 'document.location.href='redirect.jsp'>";

Playframework 2.1.2 QueryStringBindable objects Pager/MeterLimitVal not binding querystrings when routed from template to controller as a parameter

I tried with my own class MeterLimitVal and with less complex example Pager class. The bind method doesn't work correctly, because it's Map parameter is null. I am able to see that from the debug checks I put to bind method. As a result I get Bad request error message.
#Override
public F.Option<Pager> bind(String key, Map<String, String[]> data) {
//if (data.containsKey(key + ".index") && data.containsKey(key + ".size")) {
// try {
System.out.println("Pager: bind()");
System.out.println("Pager: bind() key > " + key);
System.out.println("Pager: bind() data > " + data);
index = Integer.parseInt(data.get(key + ".index")[0]);
size = Integer.parseInt(data.get(key + ".size")[0]);
System.out.println("Pager: bind() > index, size > " + index + ", " + size);
return F.Option.Some(this);
...
}
I implemented Pager class exactly in the same way as previous link, then got rid of ..data.containsKey checks in bind method, that's the reason why I get Bad requests, otherwise F.Option.None() is returned.
I create Pager object in my controller's method pipeIndex and then at the end of this method I pass that object to return value:
return ok(views.html.pipeIndex.render(pager1, ...);
Here's the important parts of pipeIndex template(pipeIndex.scala.html):
#(pager1: models.Pager, ...)
...
<div class="accordion" id="accordion2">
<form class="form-horizontal" action="#routes.Application.indexResults(0, "pipe_consequence_index", "desc", pager1)" method="GET">
...
<input type="submit" id="searchsubmit" value="#Messages("calculate.index")" class="btn btn-primary">
</form>
part of routes file:
GET /indexResults controllers.Application.indexResults(p:Int ?= 0, s ?= "pipe_consequence_index", o ?= "desc", pager1:models.Pager)
When clicking the button the application is supposed to show the indexResults page, but I get Bad request error message.
I tried to search Playframework's integration test pages for an example. From the unit test class I learned using Call class and put it to my Controller's method pipeIndex where I create Pager class:
Pager pager1 = new Pager();
Call call = routes.Application.indexResults(0, "pipe_consequence_index", "desc", pager1);
System.out.println("Call URL > " + call.url());
This shows the call URL in the expected way:
Call URL > /indexResults?pager1.index=45&pager1.size=313
When I try to hack the address line in my browser, I am able to get into indexResults page.
http://localhost:9000/indexResults?pager1.index=0&pager1.size=0
Somehow the parameters going to bind method doesn't go in the correct way (null :( ), when indexResults is called from template. Did you see that kind of problem? Can you help me?
Cheers,
Alparslan

Not able to retrieve value of parameters from JSP page

Suppose
home.jsp code contain a button like
<input name="R" onclick="del(<%=rs.getString("VDB")%>)" type="radio" value="<%=rs.getString("VDB")%>" />
//note: i am passing value of VDB in del()
it is calling javascript as :
<script type="text/javascript">
function del(delno)
{
var ff = document.vendorform;
var url = "delete.jsp?+vendor="+delno;
window.navigate("delete_vendor.jsp?+vendor="+delno);
//window.location.href="delete_vendor.jsp?+vendor="+delno;
//ff.method = "POST";
//ff.action ="delete_vendor.jsp?+vendor="+delno;
//ff.submit();
}//i have used 3 methods to redirect to delete.jsp
delete.jsp :
on this page i want to retrieve value of vendor i used :
String D1=request.getParameter("vendor");
But getting null value.
Any help
var url = "delete.jsp?+vendor="+delno;
you have inserted wrong string +vendor. it should be only vendor in url.
var url = "delete.jsp?vendor="+delno;
Remove the "+" before the word vendor. Change
var url = "delete.jsp?+vendor="+delno;
To
var url = "delete.jsp?vendor="+delno;

How to call action with parameters of JSP Servlet from JavaScript?

function printthis()
{
var content_vlue = document.getElementById('print_content').innerHTML;
var target= 'printValue?value1='+content_vlue;
document.forms[0].action = target;
document.forms[0].submit();
}
<div id="print_content">hello i am good</div>
For frontend I am using JSP. While executing this code to get the value in servlet
String msg = request.getParameter("value1");
While executing this code the browser url changes to printValue?
But I am unable to get the value of value1
Please suggest me...
Seems you are missing value1='+content_vlue from the request
try this and see
var target= "'printValue?value1="+content_vlue+"'";
Create a hidden variable inside your form like this
<form ..>
....
<input type="hidden" id="value1" name="value1"/>
</form>
and modify javascript function to this .
function printthis()
{
var content_vlue = document.getElementById('print_content').innerHTML;
document.getElementById('value1').value = content_value;
var target= 'printValue';
document.forms[0].action = target;
document.forms[0].submit();
}
Hope this will work for you.

Extend displaytag with struts 1. Problem in keeping current page and sort condition

I am implement displaytag by extend the standard. Please see detail below.
JSP
display:table name="testList" id="obj" requestURI="testAction.do?pageAction=init" pagesize="${paging_size}" sort="list" class="table" **sort="external"** excludedParams="*" decorator="dyndecorator" export="false" **keepStatus="true"**
display:setProperty name="pagination.pagenumber.param" value="page"
display:setProperty name="pagination.sort.param" value="sort" /
display:setProperty name="pagination.sortdirection.param" value="dir"
display:column property="testNo" title="Test no." **sortable="true"** **sortName="testNo"**
display:column property="testValue" title="Test value" **sortable="true"** **sortName="testValue"**
display:table
PaginatedListImpl
public PaginatedListImpl(HttpServletRequest request) {
sortCriterion = request.getParameter("sort");
sortDirection = "desc".equals(request.getParameter("dir"))? SortOrderEnum.DESCENDING : SortOrderEnum.ASCENDING;
pageSize = DEFAULT_PAGE_SIZE;
String page = request.getParameter("page");
index = page == null? 0 : Integer.parseInt(page) - 1;
}
JAVA
public PaginatedListImpl getTest(
Criteria criList = session.createCriteria(TestDto.class);
criList.setFirstResult(pageDisplay.getFirstRecordIndex());
criList.setMaxResults(pageDisplay.getObjectsPerPage());
pageDisplay.setList(criList.list());
pageDisplay.setTotal((Integer) criTotal.uniqueResult());
return pageDisplay;
}
Anyway, I found problem on browser when the browser render the result
when I move the mouse over page_no. It will show "http://localhost:8080/WebProject/testAction.do?page=2&pageAction=init"
when I move the mouse over column name. It will show "http://localhost:8080/WebProject/testAction.do?sort=testValue&*dir=asc*pageAction=init"
but I need the link of both like this
"http://localhost:8080/WebProject/testAction.do?page=2&sort=testValue&dir=asc&pageAction=init"
I search on google many time but I still have a problem.
How can I do? Someone Please help me out of problem.
Thank you a lot..
I am giving you my sample code below
<%
String str = "/details.do?method=showQuestions&surveyId="+arrayNew.get(0);
%>
<html:link page='<%=str%>'><%=arrayNew.get(1)%></html:link>
Try to put url in string variable and use the string variable in the html:link tag as shown above. This may solve your problem.
Thanks

Categories

Resources