When to use errors.reject() or errors.rejectValue()??
"The difference between rejectValue and reject is that rejectValue
always has to be about a field, hence it is a FieldError. The reject
method generates global errors."
Be careful with your calls because .reject() and rejectValue() both
have two string parameters. If your call is meant to be for
.rejectValue() but call .reject() instead, it will not break but also
won't work!
REF:
http://forum.springsource.org/archive/index.php/t-34649.html
From
org/springframework/spring-context/3.0.5.RELEASE/spring-context-3.0.5.RELEASE-sources.jar!/org/springframework/validation/Errors.java
Errors.java
/**
* Register a field error for the specified field of the current object
* (respecting the current nested path, if any), using the given error
* description.
* <p>The field name may be <code>null</code> or empty String to indicate
* the current object itself rather than a field of it. This may result
* in a corresponding field error within the nested object graph or a
* global error if the current object is the top object.
* @param field the field name (may be <code>null</code> or empty String)
* @param errorCode error code, interpretable as a message key
* @see #getNestedPath()
*/
void rejectValue(String field, String errorCode);
/**
* Register a global error for the entire target object,
* using the given error description.
* @param errorCode error code, interpretable as a message key
*/
void reject(String errorCode);
/**
* Register a global error for the entire target object,
* using the given error description.
* @param errorCode error code, interpretable as a message key
* @param defaultMessage fallback default message
*/
void reject(String errorCode, String defaultMessage);
Tuesday, 23 April 2013
Friday, 19 April 2013
Getting size of a map in JSTL (it's different in JSP2.1 and JSP 2.2)
Surprised to discover that this doesn't cause problems in JSP
<c:out value="${priceMap.lenght}"/>
tried fixing it to
${priceMap.size}
then realised it gets converted to getSize() and so changed to
${priceMap.size()}
and then get a JSP exception:
org.apache.jasper.JasperException: /WEB-INF/jsp/postform.jsp(31,22)
The function getSize must be used with a prefix when a default
namespace is not specified
I was wondering why, then realised that this works in JSP 2.2
(released in 2009) but not in JSP 2.1 (released 2006)
In JSP 2.1 you must use:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<c:out value="${fn:length(priceMap)}" />
<c:out value="${priceMap.lenght}"/>
tried fixing it to
${priceMap.size}
then realised it gets converted to getSize() and so changed to
${priceMap.size()}
and then get a JSP exception:
org.apache.jasper.JasperException: /WEB-INF/jsp/postform.jsp(31,22)
The function getSize must be used with a prefix when a default
namespace is not specified
I was wondering why, then realised that this works in JSP 2.2
(released in 2009) but not in JSP 2.1 (released 2006)
In JSP 2.1 you must use:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<c:out value="${fn:length(priceMap)}" />
Tuesday, 16 April 2013
IntelliJ IDEA: More keyboard shortcuts that don't work in Xubuntu 12.04
Just discovered a few more IntelliJ IDEA keyboard shortcuts that don't
work in Xubuntu 12.04 because of conflicts with OS default keyboard
mappings.
To fix these, go into:
Settings -> Settings Manager -> Window Manager -> Keyboard (tab)
Then remove the mappings for these key combinations:
Alt-F8 - Evaluate Expression
Ctrl-F1 to Ctrl-F12 - these are mainly for moving to different
Workspace. See the image for the replacement mapping I set - I
replaced them with Ctrl-Shift-Windows-Alt-F1-F4. I've left mappings
for Workspace 5-12 as empty because I dont' think I'll need more than
4 workspaces.
Alt-Insert - Generate
Alt-Delete - Safe Delete
work in Xubuntu 12.04 because of conflicts with OS default keyboard
mappings.
To fix these, go into:
Settings -> Settings Manager -> Window Manager -> Keyboard (tab)
Then remove the mappings for these key combinations:
Alt-F8 - Evaluate Expression
Ctrl-F1 to Ctrl-F12 - these are mainly for moving to different
Workspace. See the image for the replacement mapping I set - I
replaced them with Ctrl-Shift-Windows-Alt-F1-F4. I've left mappings
for Workspace 5-12 as empty because I dont' think I'll need more than
4 workspaces.
Alt-Insert - Generate
Alt-Delete - Safe Delete
IntelliJ IDEA: On Xubuntu 12.04, Ctrl-F12 (File structure popup) doesn't work. Here's how to fix it.
THE PROBLEM:
I use Ctrl-F12 a *lot* in IntelliJ IDEA, so it was annoying when I
started work on a new desktop with Xubuntu and found that it wasn't
working anymore. The IDEA settings were all there, but it wasn't
getting picked up.
Contacted support and found out the problem is due to conflict with
Xubuntu default keyboard mappings.
Xubuntu maps Ctrl+<function key> to Switch Workspaces.
THE SOLUTION:
Just go into Settings -> Settings Manager -> Window Manager -> Keyboard (tab)
Go down to Action = Workspace 12 and remove the keyboard mapping.
In the unlikely event you *do* need to move between 12 workspaces,
just map that to another combo, or change the IntelliJ mapping to
something similar like Windows + F12.
I use Ctrl-F12 a *lot* in IntelliJ IDEA, so it was annoying when I
started work on a new desktop with Xubuntu and found that it wasn't
working anymore. The IDEA settings were all there, but it wasn't
getting picked up.
Contacted support and found out the problem is due to conflict with
Xubuntu default keyboard mappings.
Xubuntu maps Ctrl+<function key> to Switch Workspaces.
THE SOLUTION:
Just go into Settings -> Settings Manager -> Window Manager -> Keyboard (tab)
Go down to Action = Workspace 12 and remove the keyboard mapping.
In the unlikely event you *do* need to move between 12 workspaces,
just map that to another combo, or change the IntelliJ mapping to
something similar like Windows + F12.
Monday, 15 April 2013
Depending on the Java Matcher method you use, your regex may not get what you want
We had a system that you could configure with regexes to parse
incoming data. One of them was a pattern to look at a user's email,
and was configured like:
@domain\.com
And it wasn't matching the values we had, even though the values were
of the form:
somebody@domain.com
someone.else@domain.com
Then I found that the code we used to find a match was:
pattern.matcher(value).matches();
Now the javadoc for matches() says:
"Attempts to match the entire region against the pattern."
So if you look at the ENTIRE string, then
somebody@domain.com
would never match
If you use Matcher.find(), then it just looks at any substring that
matches the pattern, and so this would succeed.
The fix was to change the regex to
.*@domain\.com
YES, it also means that invalid username values would be matched, but
we have other filters that would check if the entire string was of
valid email format. All we care about is the domain, for this bit of
functionality.
REF: http://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex
incoming data. One of them was a pattern to look at a user's email,
and was configured like:
@domain\.com
And it wasn't matching the values we had, even though the values were
of the form:
somebody@domain.com
someone.else@domain.com
Then I found that the code we used to find a match was:
pattern.matcher(value).matches();
Now the javadoc for matches() says:
"Attempts to match the entire region against the pattern."
So if you look at the ENTIRE string, then
somebody@domain.com
would never match
If you use Matcher.find(), then it just looks at any substring that
matches the pattern, and so this would succeed.
The fix was to change the regex to
.*@domain\.com
YES, it also means that invalid username values would be matched, but
we have other filters that would check if the entire string was of
valid email format. All we care about is the domain, for this bit of
functionality.
REF: http://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex
Thursday, 11 April 2013
Unix: how to display line numbers in less command
run "less"
then type ":"
then type "-N"
enter RETURN
then type ":"
then type "-N"
enter RETURN
Unix: Count the number of delimiters for each line in a huge file
Found this here:
http://ubuntuforums.org/showthread.php?t=301479
*THIS* is the one to use:
awk 'BEGIN { FS = ";" } ; { print NF-1 }' <million_lines.csv>
note that this outputs to console, so pipe to file for easier use
(add this at the end: > output_file)
example:
input file is "addresses.txt" and i want to
awk 'BEGIN { FS = "\t" } ; { print NF-1 }' addresses.txt > tab_count.txt
The other approach using echo |tr|wc is really really slow.
Note to self: learn awk and sed!
http://ubuntuforums.org/showthread.php?t=301479
*THIS* is the one to use:
awk 'BEGIN { FS = ";" } ; { print NF-1 }' <million_lines.csv>
note that this outputs to console, so pipe to file for easier use
(add this at the end: > output_file)
example:
input file is "addresses.txt" and i want to
awk 'BEGIN { FS = "\t" } ; { print NF-1 }' addresses.txt > tab_count.txt
The other approach using echo |tr|wc is really really slow.
Note to self: learn awk and sed!
Java: In a static method, how do we load a file from a jar in classpath
Task: inside a static method, load a file contained in a jar that is
in our classpath.
First approach:
static void loadFromClasspath(String filename)
InputStream in = MyClass.class.getClassLoader().getResourceAsStream(filename);
// test
System.out.println("file found?" + in.available());
in.close()
main()
MyClass.loadFromClasspath("/this/is/a/package/file.txt");
-- was getting NPE on call to in.available
Second approach:
changed one line to
InputStream in = MyClass.class.getResourceAsStream(filename);
WORKS!
Warning:
Trying
MyClass.loadFromClasspath("file.txt");
does *not* work
in our classpath.
First approach:
static void loadFromClasspath(String filename)
InputStream in = MyClass.class.getClassLoader().getResourceAsStream(filename);
// test
System.out.println("file found?" + in.available());
in.close()
main()
MyClass.loadFromClasspath("/this/is/a/package/file.txt");
-- was getting NPE on call to in.available
Second approach:
changed one line to
InputStream in = MyClass.class.getResourceAsStream(filename);
WORKS!
Warning:
Trying
MyClass.loadFromClasspath("file.txt");
does *not* work
Subscribe to:
Posts (Atom)