Use jquery remote validation with struts2


Written on April 15, 2008 – 3:39 pm | by mpayne

I feel that jquery’s validation plugin gives a much better UI treatment than the struts client side validation.  In addition, there are case where one does not want to pay any Dojo penality.

To make a remote (server side) validation call, the server must return a true/false JSON response.  There is a Struts 2 JSON plugin , however in this then response needs to be a plain “true”/”false” answer, not attribute name = “{ “response”: true}.  Because of this, the Struts2 built-in “stream” result seems to make the most sense.

The xml configuration for such an action may look something like:

<package name=”ajax”  extends=”default” namespace=”/ajax“>

<interceptors>
<interceptor-stack name=”slimStack”>
<interceptor-ref name=”exception” />
<interceptor-ref name=”alias” />
<interceptor-ref name=”servletConfig” />
<interceptor-ref name=”prepare” />
<interceptor-ref name=”chain” />
<interceptor-ref name=”currentUserAware”/>
<interceptor-ref name=”staticParams” />
<interceptor-ref name=”params”/>
</interceptor-stack>
</interceptors>

<default-interceptor-ref name=”slimStack” />
<action name=”validPassword” class=”com.something.security.ui.action.RemotePasswordValidation”>
<result type=”stream“>
<param name=”contentType”>text/html</param>
<param name=”inputName”>inputStream</param>
</result>
</action>

</package>

Note that if you are using Sitemesh, you will want to make sure that these type of responses do not get decorated, because of this I put this action in own namespace (/ajax) and Sitemesh is configured to exclude
<pattern>/ajax/*</pattern>.

For the action to produce a stream reponse, it simply needs to return an InputStream.
example:
public class RemotePasswordValidation {

public RemotePasswordValidation() {
uberPattern = Pattern.compile(”^(?=.*(\\d|[^a-zA-Z\\d]))(?=.*([a-z]|[^a-zA-Z\\d]))(?=.*([A-Z]|[^a-zA-Z\\d]))(?!.*\\s).{6,12}$”);
}

private InputStream inputStream;
private Pattern uberPattern;
private String password;
private Boolean valid = false;

public Boolean isValid() {
return valid;
}

public InputStream getInputStream() {
return inputStream;
}

public String execute() {
valid = validPassword(getPassword());
inputStream = new java.io.ByteArrayInputStream(valid.toString().getBytes());
return Action.SUCCESS;
}

public boolean validPassword(String password) {
boolean result = false;

if (password != null) {
if (password.trim().length() >= 6) {
if (uberPattern.matcher(password).find()) result=true;
}
}

return result;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

The actual definition in javascript might look something like the following:

<script type=”text/javascript”>
<!–
$().ready(function() {

$(”#password”).focus();

$(”#passwordChange”).validate({
rules: {
password: {
required: true,
minlength: 6,
remote: “${base}/ajax/validPassword.action”
},
confirmPassword: {
required: true,
minlength: 6,
equalTo: “#password”
}
},
messages: {
password: {
required: “Provide a password”,
rangelength: jQuery.format(”Enter at least {0} characters”),
remote: jQuery.format(”{0} does not meet password policy requirements.”)
},
confirmPassword: {
required: “Repeat your password”,
minlength: jQuery.format(”Enter at least {0} characters”),
equalTo: “Enter the same password as above”
}
}
});
});
–>
</script>
Screen shot of running example

Other thoughts?

  • No I didn’t have to resort to server side validation for just testing a password against a regex pattern.  There are certainly many ways to skin the cat, but this provides an example of remote jquery validation that could be extended for more complex and needs, like testing against some type of password history in ActiveDirectory.

Looks like Jetty is gaining in popularity for production deployment


Written on April 14, 2008 – 9:59 pm | by mpayne

Found the following post -&amp;gt; Jetty improves in Netscraft survey again.


Got to remember to share this, due to it being a recent converation at work

jquery grid / table plugins


Written on April 7, 2008 – 11:21 pm | by mpayne

Several interesting plugins to pick from.

http://tablesorter.com/docs/index.html

http://trirand.com/jqgrid/jqgrid.html

http://www.reconstrukt.com/ingrid/example3.html#

http://webplicity.net/flexigrid/

http://motherrussia.polyester.se/docs/tablesorter/

jquery Air


Written on April 7, 2008 – 8:00 pm | by mpayne

Pretty nice tutorial and prototype of jquery being used for booking a fight.

XWork 2.1.1 is out


Written on April 3, 2008 – 4:34 pm | by mpayne

XWork 2.1.1 is out and should be in the maven repos shortly.

This release has my patch for inherited packages
Complete change log here.

Hopefully this means a Struts 2.1.1 release is coming shortly.

Test entry


Written on April 1, 2008 – 8:55 pm | by mpayne

Here is my test entry

Does this plugin work?

View Results

Loading ... Loading …

mvnrepository.com down?


Written on March 30, 2008 – 12:21 pm | by mpayne

Anyone else having trouble with this site?  Haven’t been able to query the maven repo since friday?  Are there any other decent search sites for maven?

Location for Zeitronix wideband in 08 STI


Written on March 24, 2008 – 11:25 pm | by mpayne

I currently have the Zeitronix Wideband LCD Display installed in my Legacy.  I would like to take this with me when I move the to the STI. It is very handy to have readings for AFR, Lambda, RPM, Vacuum, Boost, EGT, Throttle Position, 0-5V Sensor all in one place.
A concern I have is that the double din radio in the STI does not leave room for a convient location in the cubby as my legacy does.


Hello world!


Written on March 10, 2008 – 12:36 am | by admin

Welcome to Blog.sutternow.com. This is your first post. Edit or delete it, then start blogging!

Struts2 maven broken


Written on March 5, 2008 – 12:00 am | by mpayne

[INFO] artifact org.apache.struts:struts2-sitemesh-plugin: checking for updates from central
[WARNING] An error occurred during dependency resolution of the following artifact:

    org.apache.struts:struts2-sitemesh-pluginnull

Caused by: Couldn’t find a version in [2.0.11.1] to match range [2.0.11,)
  org.apache.struts:struts2-sitemesh-plugin:jar:null

Is this a maven problem or a struts2 problem with the release artifact