checkbox to string conversion - java

I have a page that contains diff properties with checkboxes.and I have another page that contains the summary of the above page. That means If I check the in the first page its has to reflect the relavent field details in the summary page.But as the variable type in pojo is boolean its showing as true of false.I unable to get the fields name.Im using HTML front end and java. Please help me on how to do it.
In pojo the variable is boolean. Im fetching that variable in the next summary page. I need that to be name of the variable not true of false. hope im clear

You can add the string you want to properties, evaluate your boolean, get the message you want from properties according to the boolean value:
public String getLabel(){ if(isMarried) return messages.get(marriedPropertyMessage);}

Related

Sending boolean as request parameter

In my web application I have a link - "Create New User". From the jsp I am sending some request to the server like -
<div style="float:right" class="view">
Create New User
</div>
Here user.hasPermission[] is an array of boolean. If the current user (that is user ) has the permission(that is 'createUser') to create an new user than it returns true.
Now from my controller I am trying to get the value from the request parameter, like -
request.getParameter("hasCreatePermission");
But the problem is request.getParameter() returns a String. So how can I get the boolean value from the parameter. There is no overloaded version of request.getParameter() method for boolean.
I don't think it is possible. Request is always String content. But you can do
boolean hasCreatePermission= Boolean.parseBoolean(request.getParameter("hasCreatePermission"));
If you are sure it's a boolean you can use
boolean value = Boolean.valueOf(yourStringValue)
All parameters are translated by servelt as String. You need to convert String's value to Boolean.
Boolean.parseBoolean(request.getParameter("hasCreatePermission"));
To avoid manual parsing, you have to use a framework like Spring MVC or Struts.
In servlet it will accept all inputs given by users as string.So, we should parse the input after we got the inputs.
e.g
boolean flag = Boolean.parseBoolean(req.getParameter("Flag "));
I think this will be useful for you.

Java Parsing nested conf file

I have a config file:
company=My Company
num_users=3
user={
name="John"
age=24
}
user={
name="Anna"
age=27
}
user={
name="Jack"
age=22
}
I'm trying to parse this config file using Java. I tried java.util.Properties but I don't know how to get each single user data.
I still can get company property value using getProperty method.
Please help me with this.
Thanks!
Nothing wrong with having same property name with different value but getProperty(String key) can not differentiate between them as it will return the first value.
Secondly you can not access nested property directly.Right now here getProperty will return whole String including {} as value because that's what your value contains.You may get value and perform some operations to fetch values from that whole String.Because property file should have only key=value format means left hand side should be key and right hand side should be value.That's it.
If you want to store values as you have specified in your code you should go for JSON format than you can store whole JSON data to file and get it back from the file while you want to use it.
If you use java.util.Properties class to load the config file, you will get the following result:
{company=My Company, age=22, user={, name="Jack", }=, num_users=3}
The reason, you could refer to the javaDoc for "public void load(Reader reader)" method of Properties class.
Since you don't describ the detailed syntax format for your config file,
base on your example input, the following sample code could retrive the name=value correctly:
String reg="(\\w+)\\s*=\\s*((?>\\{[^\\{\\}]*\\})|(?>.*$))";
Pattern pMod = Pattern.compile(reg, Pattern.MULTILINE);
Matcher mMod = pMod.matcher(line);
while (mMod.find()) {
System.out.println(mMod.group(1));
System.out.println(mMod.group(2));
}
The output is:
company
My Company
num_users
3
user
{
name="John"
age=24
}
user
{
name="Anna"
age=27
}
user
{
name="Jack"
age=22
}

Is SharedPreferences' defaultValue always read as String when defined in .xml?

I'm trying to retrieve a default value:
Definition:
<integer name="keyOfDefaultValue">2</integer>
Referenced as:
android:defaultValue="#integer/keyOfDefaultValue"
Actual code:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int intVar = sharedPrefs.getInt(keyOfDefaultValue, fallbackIntValue);
I get ClassCastExceptions stating that I'm trying to cast String to an Integer. I've read the Android docs about default values:
http://developer.android.com/reference/android/preference/Preference.html#attr_android:defaultValue
so I expected to work with the correct type, but apparently I get a String.
Can someone confirm that default values defined in .xml are always to be retrieved as Strings? Or you could just point me to a page in the documentation where this is explained...
Thanks
if you check the return value on AOSP code for different types of preference
EditTextPreference, ListPreference return String values
SwitchPreference,CheckBoxPreference returns boolean
MultiSelectListPreference returns a set of strings
If you want to change it, I guess you can always override
onGetDefaultValue in your customPreference , if you have one

Passing Jackjson JSON object from JSP to JavaScript function

I have a JSON String stored in a database. In one of my JSP pages, I retrieve this string, and I want to be able to pass the String or the JSON object into Javascript function. The function is simply this for test purposes
function test(h){
alert(h);
}
Now I can retrieve the JSON string from the database fine, I have printed it out to the screen to ensure that it is getting it, however when I pass it in like this
<input type="button"
name="setFontButton"
value="Set"
class="form_btn_primary"
onclick="test('<%=theJSON%>'); return false;"/>
Nothing happens. I used firebug to check what was wrong, and it says there is invalid character.
So I then tried passing in the JSON object like so
Widget widg = mapper.readValue(testing.get(0), Widget.class);
Then pass in it
onclick="test('<%=widg%>'); return false;"/>
Now this will pass in without an error, and it alerts the object name, however I am unable to parse it. Object comes in like with the package name of where the widget class is stored like so
com.package.mode.Widget#ba8af9
I tried using Stringify, but that doesn't seem to work on this Jackson JSON object.
After all that failed, I tried a last resort of taking the String from the database, and encoding it in base64. However, this too fails if I do this
String test = Base64.encode(theString);
and pass that in. However if I do that, print it out to the screen, then copy what is printed out, and send that through it works, so don't quite understand why that is.
So could someone please tell me what I am doing wrong. I have tried soo many different solutions and nothing is working.
The JSON String is stored in database like this
{
"id":1,
"splits":[
{
"texts":[
{
"value":"Test",
"locationX":3,
"locationY":-153,
"font":{
"type":"Normal",
"size":"Medium",
"bold":false,
"colour":"5a5a5a",
"italics":false
}
}
]
}
]
}
Would be very grateful if someone could point me in the direct direction!!
Edit:
Incase anyone else has same problem do this to pass the JSON from JSP to the JS function
<%=theJSON.replaceAll("\"", "\\\'")%>
That allows you to pass the JSON in,
then to get it back in JavaScript to normal JSON format
theJSON = theJSON.replace(/'/g,'"');
Should work fine
I think the combination of double quotes wrapping the onclick and the ones in your JSON may be messing you up. Think of it as if you entered the JSON manually -- it would look like this:
onclick="test('{ "id":1, "splits":[ { "texts":[ { "value":"Test", "locationX":3, "locationY":-153, "font":{ "type":"Normal", "size":"Medium", "bold":false, "colour":"5a5a5a", "italics":false } } ] } ] }'); return false;"
and the opening double quote before id would actually be closing the double quote following onclick= (You should be able to verify this by looking at the page source). Try specifying the onclick as:
onclick='test(\'<%=theJSON%>\'); return false;'
You can follow the following steps
Fetch the jon string
Using the jackson or any other JSON jar file , convert the json string to json array and print the string using out.println.
Call this jsp which prints the json string
check in the firebug , you will be able to see your json .
If the Json string does not print , there can be some problems in your json format.
this is a good website for json beautification , http://jsbeautifier.org/ , really makes the string simple to read .
Thanks
Abhi

What actually means "value" in JSP?

I have read JSP recently, and have a doubt in the javabeans technolgy it uses. Lets say that the following JavaBeans code :
package mortgage;
public class Mortgage
{
private double amount = -1.0;
public void setAmount(double amount)
{
this.amount = amount;
}
}
And lets say i have to make use of this JavaBeans in my JSP and take the parameter values obtain from the HTML form or from the URL query string and JSP code as follows:
<jsp:useBean id="calc" class="mortgage.Mortgage" />
<p> Testing . . .
<c:set target="${calc}" property="amount" value="${param.mortgageAmount}" />
. . . . .
This example was little modified from my book. My question is what does this value in the above code JSP does? Where does the mortgageAmount came from?(is this the value from the HTML form element?)
And also what does target and property does?
Since I am a novice, i dont know what actually is going on the above code. Please help me and correct me if am wrong?
value represents expression that would be set to the target
Where does the mortgageAmount came from?
it assumed to be coming as param as you have used it in your code by param.mortgageAmount in url like
yourapp/page.jsp?mortgageAmount=someVAlue
In Simlper words
value is and Expression to be evaluated which will be set to
target object's property represented by property
See Also
Javadoc
param is a JSP implicit object. It's a map whose entries are the page parameters - so anything that's come in as a parameter in the query string, or (i think) through a form post.
Target and property govern what the c:set does; it sets the named property on the named target object to the given value.

Categories

Resources