How to check an array is exist or not? - html

I am new in java, I am developing an application, where I found some difficulties. Can anyone tell me how to check that an array is set or not, means the array is exist or not.
Actually I'm getting error when I run my jsp page which contains a listing of data with a HTML form. When I submit the form and get results it works fine, but very first time to open the page it doesn't get that resultant array and give the error.
Thanks in advance.

You should be checking if the array is empty or not like this:
<c:if test="${!empty array}">
//list the array
</c:if>
empty is an EL operator which return true if array is null or empty.

You can check whether a variable has been initialized or not by checcking if == null or not.

Related

Cakephp 3 “Trying to get property of non-object” error with h() when Null

OK, I have a view template where I want to display an associated value from my 'item' entity:
<?= h($item->itemgroup->groupname)?>
If the $item->itemgroup_id is NULL, I get the error:
Trying to get property of non-object
It also errors without the h() function. However, if I change the view code to:
<?= h($item['itemgroup']['groupname']) ?>
It does not error, and displays a blank as expected.
Is it necessary to update all the baked view template code where a value is potentially NULL? Or is it a matter of database setup (i.e., not using NULL for a field that can be potentially blank)?
Thanks in advance for any insight or advice?
Cheers,
D.
Is it necessary to update all the baked view template code where a value is potentially NULL? Or is it a matter of database setup (i.e., not using NULL for a field that can be potentially blank)?
Yes, it is.
To avoid error, just use :
<?=$item->itemgroup!==null ? h($item->itemgroup->groupname) : ''?>

Ng-repeat in Sightly

I am trying to populate drop-down values using ng-repeat in Sightly. The AEM node saves my data as String array and I am able to fetch it properly, but not able to populate them as it throws "? undefined:undefined ?" error.
My code:
<select name="${validation.elementName}" id="${validation.elementName}" ng-model="${validation.elementName}" ng-change="${properties.clickfunction}">
<option ng-repeat="opt in ${properties.options}" value={{opt}}>opt</option>
</select>
And the output:
:
Is there anything I am missing? As Sightly is totally new to me. I will be very grateful for any help to improve this code or pointing my mistake.
First of all, you need to wrap the data you pass to value in quotes, so it should be like this:
value="{{opt}}"
Second, it looks like you are passing the values without single quotes and they are not being recognized as strings. Take a look at this plunker:
http://plnkr.co/edit/A2gZJbvVV9ozHloLkF4B?p=preview
You can see that the first ng-repeat works as expected, but the second throws an error in the console and doesn't display anything. Basically, you just need to put quotes around each string in your array.
Thanks #Victor for your reply.
Please find my below findings.
value="{{opt}}" was my mistake while pasting the code to stackoverflow.
${properties.options} returns string array.
ng-repeat seems to parse correctly as per this output.

View JSON key value

So this is my first time using nodeJs and first exposure to APIs in general. I am trying to get the value for a certain key from JSON that I receive in a response.
Here is a sample:
I need the value of name, which in this case would be "Hillary_Clinton".
But when I do
console.log(JSON.parse(body).face_detection.name);
it returns undefined. I tried it with .confidence , .quality etc. but they all return undefined. If I do just .face_detection , it properly returns everything inside it.
face_detection is an array not an object.
try console.log(JSON.parse(body).face_detection[0].name);
JSON.parse(body).face_detection[0].name
When the console work doesn´t work, try to do:
console.log(JSON.parse(body) ,JSON.parse(body).face_detection , JSON.parse(body).face_detection.name);
This way, you can see what is the first object that you can´t have access. And see their structure.

Nested If Statement JSTL/JSP/HTML

So I have a log in system that uses 3 inputs to log you in and all the details are in a SQL database. I want to know how to check if all three are true based on the first one but the only way I can think to do it is to use nested if loops.
After I've queried the database and input data into the I have 3 if statements which I know think can't be nested.
<c:forEach var="i" begin="0" end="3">
<c:if test="${param.Login == result.rows[i]['Login']}">
<c:if test="${param.Cust_Acc_No == result.rows[i]['Cust_Acc_No']}">
<c:if test="${param.password == result.rows[i]['password']}">
<c:set var="Greeting" value="Congratulations Mr.${result.rows[i]['surname']}" scope="session"/>
</c:if>
</c:if>
</c:if>
</c:forEach>
So I want to loop through all the tuples in the table and if the login entered matches the login of the tuple then look to see if the entered account number matches and then finally the password. Then the name of person will be stored in a greeting and they'll eventually be forwarded to the accounts page and greeted and they can see all their details. Any ideas? To either make this nested or how, if possible, I can use (not entirely sure how to use that though!)
I would not do those checks on my JSP page. If you want something custom for authentication I suggest to use a servlet to handle the parameters and see if you are dealing with an authorized user and use a filter to project your restricted content.
I also would like to advice you to think about using a security framework like Apache Shiro where you can create your own authentication handler.

putting all checked items in checkboxes in an array

I need to take all the checked items in an html page and put them in an array to perform a certain action on it. I don't understand the fields of a checkbox(name, value,...). I am using html with ruby on rails!
If you need to do it on the client side using javascript you can use jQuery like this:
var arr = $('[type=checkbox]:checked');
If you need to do this on the server side in ruby on rails I cannot help you. However I do know that only checked checkboxes will get send back to the server on a GET or POST, so if you know what names these checkboxes you can get them from the request object and put them in a array. Non checked checkboxes will never get to the server, so you won't have to explicitly ignore them.
$_POST is already an array of everything within the form set. Use this to do your "certain action" on the checkboxes. If for whatever reason you need the information in a different variable, then just loop through the $_POST and put it into a different variable like so:
while ($row = $_POST) {
$new_var[] = $row[0];
}
That should work for you to put into a new/different array but frankly, I don't see the point of that since it's already an array.
As far as understanding the way the array of $_POST looks like, then var_dump() it and see. The name of the checkbox will be that in [] and the value will display after the =>. So if your input is named name=box1 and the value is value=1 then the array, from the $_POST standpoint, would look like [box1] => 1 so you can then use this section by calling it specifically like $foo = $_POST['box1'] and the value of $foo would be the value of box1 or you can do whatever else you want with that (rather than put into a new variable) by calling it.
If you do something like this:
<input type='checkbox' name='User[]' VALUE='someVal'>Visible Text</option>
Whatever checkbox is checked by the user then submitted will be in a POST array called User.
Adding the [ ] to the name creates an array when submitted.
Not sure hot to do this in ruby on rails but in php. When the form is submitted but the user an array is passed to the server. We names our checkboxes as User so our posted array is called User. In php i retrieve the posted array like this:
print_r($HTTP_POST_VARS["User"]);
then this outputs the array like so:
Array ( [0] => Me [1] => You [2] => SomeoneElse )
all of the data in the array were check checkboxes. the you can do a
foreach (​​​​​​​ $HTTP_POST_VARS["User"] as $key => $value) { $key is 0, $value is Me. and so on }
Hope this helps –