I want to display errors detected in an action class, I use:
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("some_string_in_properties_file"));`
and it works fine. However, I have written some generic error messages, and I would like to reuse them, so I am trying to do this:
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("string1_in_properties_file", "string2_in_properties_file"));
where string1 = <li>{0} is required.</li>.
Then it is displaying string2 is required. It is not replacing string2 with its value.
I even tried
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("string1_in_properties_file",
new ActionMessage("string2_in_properties_file")));
then it is displaying string2[] is required. It is not replacing string2.
I know it can be done by hard-coding the value, but is there any other way?
Since you want to to fetch two key's value from Property file, and put it in global error key,
I would say, retrieve each value separately using
String sValue1 = getResources(request).getMessage(locale, "key1");
String sValue2 = getResources(request).getMessage(locale, "key2");
and then put it in your global error
errors.add(ActionErrors.GLOBAL_MESSAGE,sValue1+"<br/>"+sValue2);
Hope it help....
It's hard to tell you exactly what to do, since O don't know the code behind errors and ActionMessage. But you can, however, use String.format. Your code would look something like this
public class ActionErrors {
public static final String INVALID_INPUT "'%s' is not valid input.";
...
}
and
String input = "Cats";
String message = String.format(ActionErrors.INVALID_INPUT, input);
System.out.println(message);
The above will print
'Cats' is not valid input.
In Struts ActionMessage, you can specify value for your parameters {0}, {1}, {2}, {3} specified in your properties file, as follows:
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("some_string_in_properties_file", "value1"));
Alternately:
errors.add(ActionErrors.GLOBAL_MESSAGE,
new ActionMessage("some_string_in_properties_file", "value1", "value2", "value3"));
value1..value3 can be of any type (as Struts expects an Object).
so your property:
string1 = <li>{0} is required.</li>
Will be replaced to:
<li>value1 is required.</li>
(If you specify your key as string1).
Let's say you have a properties file that defines some keys for messages, like so:
string1: <li>{0} is required.</li>
string2: Username
The ActionMessage class has a number of constructors that take a varying number of arguments. The first is a string representing the key that refers to a message - in your case, the key is string1 which corresponds to the message <li>{0} is required.</li>; with the {0} being a placeholder for some dynamic content.
The remaining possible arguments are Objects that represent the actual values you want to replace those placeholders. If you do new ActionMessage("string1", "string2") you're passing in the literal value string2, and you'll end up with output of <li>string2 is required.</li>.
What you need to do is replace "string2" with a method call that will get the value that corresponds to the key string2. This is where my knowledge of the problem runs out, though, so you'll need to do some research on this part for yourself.
Related
I am trying to create a NetCDF file using java (unidata library). One of the requirements is to include the _FillValue attribute in all the Variables. I have one of type CHAR, and I can not do it.
The Attribute constructor only accepts Strings or numbers (or arrays of them), not chars. I have tried both of them anyway but the final netcdf does not show the attribute.
Other languages let you do it (we have seen this working in matlab), but I don't know how to do it using java.
I see in the documentation that the _FillValue should be of the same type of the Variable itself but Attribute values does not accept Chars, only String or Numbers
For example: When I try
Nc4Chunking chunker = Nc4ChunkingStrategy.factory(Nc4Chunking.Strategy.standard, 6, true);
NetcdfFileWriter dataFile = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4_classic, fileName, chunker);
....
Variable varid_scdr = dataFile.addVariable(null, "SCDR", DataType.CHAR, dimsTMS15);
varid_scdr.addAttribute(new Attribute("_FillValue", " "));
....
dataFile.write(varid_scdr, scodData);
dataFile.close();
The resulting netcdf file has no _FillValue, it is not written in the file.
But if I change the attribute name and do this
varid_scdr.addAttribute(new Attribute("FillValue", " "));
the parameter is present in the output file
I have no problems with other data types or other attribute names. I am prety sure that the problem is about the attribute _FillValue for the variable of type Char. I dont know how to write it and I need the _FillValue attribute to be explicity present in the variable attribute list.
********* 5th July 2019 ***********
I realized that the problem is only related to netcdf4 and netcdf4_classic files. So perhaps is about chunking or something like that. If I try it creating netcdf3 files it workis.
Any help about this issue? what am I missing?
I think this is due to bug that has been addressed in the latest version of netcdf-java (v5.0.0). v5.0.0 has been released and is available for download; my hope is that the announcement will go out today.
If you want to be explicit about writing a CHAR valued attribute, one way to to it would be:
String fillValue = " ";
Array charArrayFillValue = ArrayChar.makeFromString(fillValue, 1);
charAttrFillValue = new Attribute("_FillValue", charArrayFillValue);
varid_scdr.addAttribute(charAttrFillValue)
another way would be:
String fillValue = " ";
Array charArrayFillValue = ArrayChar.makeFromString(fillValue, 1);
charAttrFillValue = new Attribute("_FillValue", DataType.CHAR);
charAttrFillValue.setValues(charArrayFillValue);
varid_scdr.addAttribute(charAttrFillValue)
Both of those are a bit verbose, though. I just checked using version 5, and your one liner works:
varid_scdr.addAttribute(new Attribute("_FillValue", " "));
However, if you try to pass in a value for _FillValue that isn't a string of length 1, the netCDF-C library will throw an error. So this:
varid_scdr.addAttribute(new Attribute("_FillValue", "ab"));
will result in:
-36 (NetCDF: Invalid argument) on attribute ':_FillValue = "ab"' on var varid_scdr
netCDF-Java will make sure the string you pass in gets converted to CHARs, but it won't truncate the resulting set of CHARs to fit into the single character limit on the _FillValue attribute.
I'm testing some UI functionality with Java and AssertJ.
So when I receive some massive string from UI, I should verify if that String contains at least one predefined value from List<String>.
It is easy to do opposite thing - verify if list contains at least once some String value but this is not my case.
I can't find solution in standard methods.
public static final List<String> OPTIONS = Arrays.asList("Foo", "Bar", "Baz");
String text = "Just some random text with bar";
what I need is smth like this :
Assertions.assertThat(text)
.as("Should contain at least one value from OPTIONS ")
.containsAnyOf(OPTIONS)
.matches(s -> OPTIONS.stream().anyMatch(option -> s.contains(option)));
You can also try to use Condition and AssertJ assertions like areAtLeastOne(), areAtLeast(), for instance:
assertThat(OPTIONS)
.areAtLeastOne(new Condition<>(text::contains,
String.format("Error message '%s'", args);
Another option using AssertJ
assertThat(text.split(" ")).containsAnyElementsOf(OPTIONS);
Another simplified option using AssertJ:
.matches(s-> Stream.of("Foo", "Bar", "Baz").anyMatch(s::contains));
My text file has a pattern and it's just like the following:
1;Mary Yeah;John Freeman;(12)3456-7890;iammary#gmail.com
2;Ash Wilson;One Two Three;(99)1111-2222;lorddragon#hotmail.com
3;Xin Zhao;Street Address 55;(11)0101-0202;lolyourface#gmail.com
4;My Name;My Address;My Phone;myemail#mail.com
I want to be able to type the line number, the type of data I want to replace(e-mail, phone, name), and the string I want to replace them with. The program overwrites the text.
How could I code this in Java?
The issue of how to find a given row based on the line number depends on many things, most importantly it depends on code you haven't shown us. But as for what you can do once you have found a given line, you may try the following:
String line = "2;Ash Wilson;One Two Three;(99)1111-2222;lorddragon#hotmail.com";
String[] parts = line.split(";");
parts[4] = "some.address#mail.com"; // to change the email
// now join back to a single line
line = String.join(";", Arrays.asList(parts));
Demo
How can i send an Array with a HTTP Get request?
I'm Using GWT client to send the request.
I know this post is really old, but I have to reply because although BalusC's answer is marked as correct, it's not completely correct.
You have to write the query adding "[]" to foo like this:
foo[]=val1&foo[]=val2&foo[]=val3
That depends on what the target server accepts. There is no definitive standard for this. See also a.o. Wikipedia: Query string:
While there is no definitive standard, most web frameworks allow multiple values to be associated with a single field (e.g. field1=value1&field1=value2&field2=value3).[4][5]
Generally, when the target server uses a strong typed programming language like Java (Servlet), then you can just send them as multiple parameters with the same name. The API usually offers a dedicated method to obtain multiple parameter values as an array.
foo=value1&foo=value2&foo=value3
String[] foo = request.getParameterValues("foo"); // [value1, value2, value3]
The request.getParameter("foo") will also work on it, but it'll return only the first value.
String foo = request.getParameter("foo"); // value1
And, when the target server uses a weak typed language like PHP or RoR, then you need to suffix the parameter name with braces [] in order to trigger the language to return an array of values instead of a single value.
foo[]=value1&foo[]=value2&foo[]=value3
$foo = $_GET["foo"]; // [value1, value2, value3]
echo is_array($foo); // true
In case you still use foo=value1&foo=value2&foo=value3, then it'll return only the first value.
$foo = $_GET["foo"]; // value1
echo is_array($foo); // false
Do note that when you send foo[]=value1&foo[]=value2&foo[]=value3 to a Java Servlet, then you can still obtain them, but you'd need to use the exact parameter name including the braces.
String[] foo = request.getParameterValues("foo[]"); // [value1, value2, value3]
I am a beginner at Java. Pardon me if its too stupid a question.
I am fetching a string input from the user in a variable comparisonEntity. if the input is "xyz" then I have to compare all the instances based on this attribute. (the same name attribute exists in class) If the input is "abc" then I have to compare all the instances based on this attribute.
Also I dont want to write redundant code in if and else sections.
So is there something like
objectname."comparisonEntity"
"comparisonEntity" will be replaced by the attribute name of the same value.
If I understand your problem, I think you can achieve this by using reflection.
So you have an instance of your class, and also an input string:
Test instance = new Test();
String input = "nameOfSomeField";
You can obtain a Field that corresponds to the inputted string:
try {
Field field = instance.getClass().getField(input);
} catch (NoSuchFieldException ex) {
// Field doesn't exist
}
And then you can do things like get the value of such field:
Object value = field.get(instance);
Notice that field.get() returns an Object. You need to cast it.