(no subject)
"eric sherman" <[email protected]> Fri, 8 Oct 2004 11:02:51 -0500 (CDT)
| Newsgroups | gmane.comp.java.open-symphony.cvs |
|---|---|
| Message-ID | <35136.63.115.106.66.1097251371.fusewebmail-23551@www.fusemail.com> |
------=_20041008110251_31378
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
i've made some extremely simple enhancements to ww1's ActionSupport which
are very useful to CommandDriven actions. i submit it to you as it
appears in my codebase. i would appreciate feedback.
1. automatic do[Command]Validation calls -- highly useful across the
board. if do[Command]Validation() doesn't exist, no problem. if it does,
call it.
2. proxy methods for SUCCESS, INPUT, ERROR, LOGIN and custom return codes
-- no more 'return command+"."+SUCCESS;' type stuff. perhaps i've been
doing something wrong, command+"."+foo is the only way i'd been getting
CommandDriven return codes to work properly. maybe this is a non issue.
3. automatically show input view if no input was submitted -- this is
probably not suitable for inclusion into the main project, but something
i've found supremely useful myself. i'm sure there are other ways to
implement it. basically, if the action has a 'submit' property, and if
it's null, show the input view. otherwise, continue with execute. at the
end of the execution, it resets 'submit' to null, so that if there are
chained actions, they don't think they have gotten submitted data. this
requires that when a form is submitted in a view, there be a 'submit'
property sent. easiest to name the submit button 'submit' or something.
works for me, but is probably too clunky to include in the main project.
here's the code:
package com.ccs.ww.action;
import webwork.action.CommandDriven;
import java.lang.reflect.Method;
public class Action extends webwork.action.ActionSupport implements
CommandDriven {
public String execute() throws Exception {
boolean hasSubmit = false;
boolean hasCommand = false;
if (command != null && !command.equals("")) {
hasCommand = true;
}
// if we have a submit field, and it is null, show the input view
try {
Method getSubmit = getClass().getMethod("getSubmit", null);
hasSubmit = true;
String submit = (String)getSubmit.invoke(this, null);
if (submit == null) {
return INPUT();
}
}
catch (NoSuchMethodException e) {
// guess we don't have a submit field
}
// if we're command-driven, try doCommandValidation and display
INPUT on errors
if (hasCommand) {
StringBuffer methodName = new StringBuffer("do");
methodName.append(command);
methodName.append("Validation");
methodName.setCharAt(2,
Character.toUpperCase(methodName.charAt(2)));
try {
Method doCommandValidation =
getClass().getMethod(methodName.toString(), null);
doCommandValidation.invoke(this, null);
if (invalidInput()) {
return INPUT();
}
}
catch (NoSuchMethodException e) {
// oh well, no validation method for this command
}
}
String returnCode = super.execute();
if (hasSubmit) {
Class[] argList = {String.class};
Object[] args = {null};
Method setSubmit = getClass().getMethod("setSubmit", argList);
// set submit to null
setSubmit.invoke(this, args);
}
return returnCode;
}
public String SUCCESS() {
if (command != null && !command.equals("")) {
return command+"."+SUCCESS;
}
else {
return SUCCESS;
}
}
public String INPUT() {
if (command != null && !command.equals("")) {
return command+"."+INPUT;
}
else {
return INPUT;
}
}
public String ERROR() {
if (command != null && !command.equals("")) {
return command+"."+ERROR;
}
else {
return ERROR;
}
}
public String LOGIN() {
if (command != null && !command.equals("")) {
return command+"."+LOGIN;
}
else {
return LOGIN;
}
}
public String CUSTOM(String view) {
if (command != null && !command.equals("")) {
return command+"."+view;
}
else {
return view;
}
}
}
thanks,
-eric
------=_20041008110251_31378
Content-Type: text/java; name="Action.java"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="Action.java"
package com.ccs.ww.action;
import webwork.action.CommandDriven;
import java.lang.reflect.Method;
public class Action extends webwork.action.ActionSupport implements CommandDriven {
public String execute() throws Exception {
boolean hasSubmit = false;
boolean hasCommand = false;
if (command != null && !command.equals("")) {
hasCommand = true;
}
// if we have a submit field, and it is null, show the input view
try {
Method getSubmit = getClass().getMethod("getSubmit", null);
hasSubmit = true;
String submit = (String)getSubmit.invoke(this, null);
if (submit == null) {
return INPUT();
}
}
catch (NoSuchMethodException e) {
// guess we don't have a submit field
}
// if we're command-driven, try doCommandValidation and display INPUT on errors
if (hasCommand) {
StringBuffer methodName = new StringBuffer("do");
methodName.append(command);
methodName.append("Validation");
methodName.setCharAt(2, Character.toUpperCase(methodName.charAt(2)));
try {
Method doCommandValidation = getClass().getMethod(methodName.toString(), null);
doCommandValidation.invoke(this, null);
if (invalidInput()) {
return INPUT();
}
}
catch (NoSuchMethodException e) {
// oh well, no validation method for this command
}
}
String returnCode = super.execute();
if (hasSubmit) {
Class[] argList = {String.class};
Object[] args = {null};
Method setSubmit = getClass().getMethod("setSubmit", argList);
// set submit to null
setSubmit.invoke(this, args);
}
return returnCode;
}
public String SUCCESS() {
if (command != null && !command.equals("")) {
return command+"."+SUCCESS;
}
else {
return SUCCESS;
}
}
public String INPUT() {
if (command != null && !command.equals("")) {
return command+"."+INPUT;
}
else {
return INPUT;
}
}
public String ERROR() {
if (command != null && !command.equals("")) {
return command+"."+ERROR;
}
else {
return ERROR;
}
}
public String LOGIN() {
if (command != null && !command.equals("")) {
return command+"."+LOGIN;
}
else {
return LOGIN;
}
}
public String CUSTOM(String view) {
if (command != null && !command.equals("")) {
return command+"."+view;
}
else {
return view;
}
}
}
------=_20041008110251_31378--
-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl