Access action class map in java script within ftl - javascript

We need to access an instance map defined in struts action class (it has get/set methods). We need to populated that map dynamically. We generate code to populate the map at runtime using a java script based on some user input. We are using freemarker as template.
Java script code within ftl looks like:
innerHTML += '<input class="isn" onChange="validateTag(this);" name="serialsInp[\'' + listing + '\']" value=""/>';
Here serialsInp is HashMap<String, String> and listing is java script variable. Above code renders a text field. The expectation is when user enters text in this text field, serialsInp should be populated with 'listing' as key and user entered value as value.
Is this the correct way of accessing maps in javascript/ftl?

Solved! The problem was form parameters. The map key in our case contains '-', which is disallowed due to security reasons.
The issue got resolved once we change 'params' interceptor to allow '-' in parameter names by adding following code in struts.xml
<interceptor-ref name="params">
<param name="acceptParamNames">\w+((\.\w+)|(\[\d+\])|(\['\w+(\-\w+)*'\]))*</param>
</interceptor-ref>
Reference 202 & post

Related

Java script not working on application page

I have wrote a java script to replace the value of email field to "user name" same time when user tying in the email field and I hided the username field.
Used string builder to render the script
function change(){
var Email=document.getElementByID('#Email');
var UserName=document.getElementByID('#UserName);
UserName.value=Email.Value;
}
the above code is working only when I add the webpart inside sharepoint page,but when I added the web part on an application page the UserName field is not getting updated when email field is typed.
when checked browser debugging I cant see the java script in that application page .
can I how to work this is application page ,Do I need to copy the same script to the application page where I added the webpart?
Thanks
There was two problems here, the first, getElementById wasn't written well and second, value property is lowercase.
function change(){
var Email = document.getElementById('#Email');
var UserName=document.getElementById('#UserName');
UserName.value=Email.value;
}
Issue 1 : it should be "getElementById" not getElementByID
Issue 2 : The id should not have # until and unless you are using the ID that has #(sounds silly to use # in Id)
Issue 3 : it should be "value" not Value
Use an IDE like visual studio and you will not get these silly errors.

crm 2011 get the default view id

Is there a way in CRM 2011 to get the default view id for a custom entity? Using JavaScript I want to dynamically generate a HREF but I don't want to hard code any part of the URL. I know how to get the Server URL and Org name in the link below but not this portion "etc=2&extraqs=%3f_gridType%3d2%26etc%3d2%26id%3d%257b"
http://dev:5555/MyOrg/main.aspx?etc=2&extraqs=%3f_gridType%3d2%26etc%3d2%26id%3d%257b
You can query public views just like any other entity in CRM. The entity name is SavedQuery and there are properties for returnedtypecode (Entity Name), isdefault, and querytype (the type of query it is).
So with that in mind you can make a query to the OData or Soap endpoints from JavaScript to get the default query for any entity type.
Take a look at: http://msdn.microsoft.com/en-us/library/gg334266.aspx
You should be able to use something like:
var defaultViewId = Xrm.Page.getControl("<lookup field name>").getDefaultView()

How to pass value of javascript to Mako template

I'm planning on server's cherrypy with programming of python and mako.
Now i have one problem because i know to pass value of Mako template to Javascript with
<script type="text/javascript">
var contapara=${input_nparams};
</script>
and viceversa? It 's possible apply viceversa Mako<---JAvascript?
Thanks
Edit:
Because I have one variable of Mako (kwargs) that contains the data of all forms sent.
The user enters a word that is the "clave" and is stored in Javascript.
After this I pass in Mako to search in variable (kwargs).
Call the function's MAko.
After exist this I open one windows with the textarea (new form) and I have to write data.
AFter the user change data and send the form.
Make a view that takes in the arguments that you would want to send to mako, then in your javascript use ajax. for example:
jQuery.ajax(
{
url : url_of_the_mako_view,
data : {foo:bar,foo2:bar2},
type : 'POST',
success : function(data)
{
/*
when the mako template is rendered by your view then the result will
be passed to this function in the variable data
*/
}
}
)
If you are after something a little more like a page redirect then you can do something more like this:
make a form that redirects to the right place
put in a hidden field
populate the hidden field with a json string representing the arguments you want to pass to mako
submit the form
Then in the view that gets the form data you need to turn the json into a dictionary and pass that to mako

Pass Javascript Variable into createlink method call Grails

var search= document.getElementById('appMenu').value
document.location.href= '${createLink(controller: 'application' , action:'ajaxAppSearch', params: ['query': search])}'
The element appMenu is a text field, so I am getting the value that the user enters into the text box to pass into a search controller. However, it keeps telling me that the params query is null. It seems that search isn't being passed into the create link method. Anyone have a suggestion?
Grails (controllers, GSP and tags, etc) are working on server side. JavaScript on client side. And this link is prepared before sending data to browser, and before JavaScript can pass its variable into GSP tag.
But you can prepare base link on server side, and add extra parameter on client side, by using javascript, like:
var search= document.getElementById('appMenu').value;
document.location.href= '${createLink(controller: 'application' , action:'ajaxAppSearch')}?query=' + escape(search);

How to access javascript variable in .net master page

I need to enable a script which is present in a .net master page, in only selected html pages that use this master page. can this be achieved by declaring a variable in javascript of html pages where I need this script and set the variable to some value, so that I can enable the scripts in master page only when variable is NotNullorEmpty? Does some one know if this works. If so how to get javascript variable in html, in to .net master page ?
There are 2 ways I can think of.
The first if to assign the variable to a hidden input field. Which can be accessed in the Request.Form.
html
<input type="hidden" value="your value" name="Hidden1" id="Hidden1"/>
javascript
document.getElementById('Hidden1').value = "an other value";
C#
var myValue = Request.Form["Hidden1"];
The second is saving the variable into a cookie which can be accessed in Request.Cookies.
var myValue = Response.Cookies["Cookie1"].Value;
The most flexible form of this that I have seen is to render a JSON object directly to the page as script (adding any other script you might need also), then making the javascript code fill in a hidden field with JSON data on form submit for the server to parse.
Here are the basic steps:
Create an empty hidden field for the server to read when the client submits, but send it to the client empty.
Create a string which is the script and JSON data you need to send to the client. (Use the JavaScriptSerializer .NET class.)
Fill a LiteralControl with the script tag and its contents. (This works well in the OnPreRender step, but you can do it elsewhere.)
Include JSON2.js (it's all over the web - get it from a trusted source)
Write javascript code to fill the hidden field on submit, using JSON2.stringify() to push any javascript object into that field.
On the server, you can again use JavaScriptSerializer to read the values from the submitted object.
What you are basically wanting to do is test for javascript.
There are a few ways to do this, but I would use both a server-side and client-side cookie.
In your global.asax's onbeginrequest, test for a server-side cookie of "js-test-s"
Cookie Exists, check for "js-test-c"
"js-test-c" Cookie Exists, JS enabled
"js-test-c" No Cookie, JS disabled
Cookie doesn't exist...
Set the "js-test-s" cookie
output a simple html document...
[html]
[head]
[meta refresh tag set to 1 second /]
[script to set "js-test-c" cookie, then force refresh /]
[/head]
[body][/body]
[html]
Browser will refresh the request with one or both cookies set.
WARNING: You may want to store the original request details, and redirect to another location in case cookies are disabled.
I might be not understanding the question correctly but as far as I can tell the easiest way to do this is by creating a property on the master page and then setting it on all pages that need to execute the script:
In Master page
public bool EnableScript
{
set
{
if (value == true)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "startup", "alert('test');", true);
}
}
}
In all pages that need to run javascript that use the current master page:
protected void Page_Load(object sender, EventArgs e)
{
this.Master.EnableScript = true;
}
If you have to output your JavaScript on every page, even on the once that don't execute it (why?) then just add it as a function and then inside your property add a code to call that function, and set your property to true on all pages that require JavaScript to execute.

Categories