Saturday, October 6, 2012
Why Constructor Injection Should Be Used
Sunday, September 23, 2012
Using Spring Form Binding When the View Resolver Doesn't Support It (Take 2)
Broken Exceptions
Into the View Resolver
But where? Like before I wanted to keep this piece decently modularized and Spring didn't provide much to help. After fishing through the source code for a bit the best place seemed to be to intercept the call to render() on the View interface. public void render(Map<String, ?> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
This can be modified using a Decorative wrapper:
public class RequestContextViewDecorator implements View {
private final View innerView;
private final ApplicationContext applicationContext;
public RequestContextViewDecorator(View innerView, ApplicationContext applicationContext) {
this.innerView = innerView;
this.applicationContext = applicationContext;
}
The first issue is getting around that pesky wildcard capture "?" in the model. This is easily done by a small method to get back a known safe type:
private Map<String, Object> getTypedMap(Map<String, ?> model) {
Map%lt;String, Object> typedMap = new HashMap%lt;String, Object>();
typedMap.putAll(model);
return typedMap;
}
This could easily be refactored in to an abstract class and then use a Template abstract method which receives the typed map if you have other classes doing similar things or like to add extra classes to keep things focused. This object then inherits the same behavior covered from the first post, throwing what it needs in the model and then delegating to the wrapped View:
@Override
public void render(Map%lt;String, ?> untypedModel, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map model = getTypedMap(untypedModel);
if (exposeSpringMacroHelpers) {
if (!model.containsKey(MODEL_KEY)) {
model.put(MODEL_KEY, new RequestContext(request, response, ((WebApplicationContext) applicationContext).getServletContext(), model));
}
}
....
innerView.render(model, request, response);
}
Now just plug in to to your view resolver (in this case the Surf(again) view resolver):
public class RequestContextPageViewResolver extends PageViewResolver {
@Override
protected View loadView(String viewName, Locale locale) throws Exception {
return new RequestContextViewDecorator(super.loadView(viewName, locale), this.getApplicationContext());
}
}
And there you have it (after you wire in your View resolver of choice in your Spring config): a nice OOP way of integrating the request context needed for Spring form binding in a modular way to a View resolver which for some reason or another does not have the functionality in its inheritance hierarchy.
Sunday, August 26, 2012
CSS Recipe for Making Elements Fill Their Container Height
Quick Disclaimer
Honestly my single biggest reason for this workaround is that I loathe having to open up a graphics editor for something like this, particularly to only perform a slight adjustment I'm also neurotic enough that I want the static resources associated with a site to be truly relevant, so that even one file that is conceptually redundant irks me. This workaround isn't pristine as it throws an element in the HTML which is solely for design purposes, though at this point I'm also increasingly viewing the DOM as the optimal place to pollute in little ways in the interest of keeping other more complicated aspects simple and organized. This solution is also limited to some scenarios.
Also as a quick note I'm writing this in HTML but I'm not doing it in any way orderly since I'm just typing into blogger, so inspect the elements rather than reading the source.
The Problem
Looks good...until
The Solution
(Again pretending you would scroll down to see that part overlapping the "window"). And...perfect(ish). So long as your DOM is in decent shape this works like a charm and has been put through the paces on several sites and seems to be quirk-proof in major browsers. This could also be handled by JavaScript on page load. Overall a slightly more complicated solution than a background image and may not be for everyone...but it is likely more maintainable and is also arguably more conceptually accurate than attaching the primary visual representation of one element to a resource associated with another element.
Quick Amendment
I realized after I wrote this that it is missing another piece: that container should expand to hold the sidebar if needed now that it has a relative height. Since the sidebar is presently floated it's not expanding the container. This can be solved by the typical clearfix workaround.
Saturday, August 18, 2012
JavaScript: More than a Scripting Language?
Background
Why JavaScript Rocks
JavaScript is dynamite!...
Should you use JavaScript
The Future
Sunday, August 12, 2012
Using Spring Form Binding When the View Resolver Doesn't Support It
The situation
Standard solution and what's going on
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="exposeSpringMacroHelpers"><value>true</value></property>
<property name="exposeRequestAttributes"><value>true</value></property>
<property name="exposeSessionAttributes"><value>true</value></property>
</bean>
where the first property does the needed set-up for form binding (the other two merge request and session info into the model). Tracking this down through the source code this setting is ultimately passed from the view resolver to the AbstractTemplateView (source) which adds a RequestContext to the model (and does the request & session merging).
Re-using that behavior
Saturday, August 11, 2012
Bouncing Google Play apps onto a Kindle Fire
Kindle Fire Notes
Rooting (not required)
Instructions (the significant part)
Step 1. Install app on Android device (on the source device)
Step 2. Stage the downloaded apk in an accessible location (source device)
Getting to the .apk
First you need a way to access the apk and then track it down. Like most other sites I'll recommend ES File Explorer for a file manager. I started to poke around to find the file system path to the app I was looking for, but with ES you don't even need to that. After it is opened bring up the menu and goto Manager->App Manager which will list the installed Apps. Backup the app you want to bounce to the Kindle and keep track of the directory where the backup will be stored.Staging the .apk
Step 3. Install the apk on the Kindle Fire
Conclusion
Saturday, July 7, 2012
Why Becoming a Casually Practicing Emacs-er is a Good Religious Choice
What about that other religion?
That heading will more or less the end of the religion metaphor (even thought it is a heading). In the *nix world there are two old families of text editors which have "religious" followings: vi & emacs. From my perspective vi is far more "Unix-y"; it's minimal, powerful, modular...like a well written C program (like Unix itself). Emacs on the other hand is overtly lisp-y, with an almost fluid sense of indirection and dynamism, making it a misfit in the *nix world (at least from what I've seen,even though Guile is a "standard" I've only run across it in GnuCash). There is also a related performance difference in that vi programs are normally lighter weight, but that difference is inconsequential on modern computers.I made the jump from vi (Vim) to Emacs (GNU Emacs) a couple years ago since I found Emacs more conducive to self-discovery with the combination of descriptive commands and powerful info system. But...if you want to become a devotee and actual user of either then that is entirely a matter of preference and I'm not saying one is better than the other by itself. vi has a fatal flaw in regards to the portability of knowledge, however, in that it has the concept of modes (so for instance to edit content you would enter an edit mode and then to move the cursor you would switch back to command mode and use what are often the same keys). This concept doesn't map to most newer editors.