This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Struts and the fly likes String value substitution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "String value substitution" Watch "String value substitution" New topic

String value substitution

nachagoni rishi

Joined: Oct 14, 2004
Posts: 26
Is there any way i can replace "ticketNumber==null" and "plateNumber==null" with value of errorField, so that my code is shorter.

public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

HttpSession session = request.getSession();
String errorField = (String) session.getAttribute("errorField");
if (errorField.equalsIgnoreCase("ticketNumber")) {

if ((ticketNumber == null) || (ticketNumber.trim().length() == 0))
errors.add(errorField,new ActionError("error." + errorField + ".null"));
if (errorField.equalsIgnoreCase("plateNumber")) {
if ((plateNumber == null) || (plateNumber.trim().length() == 0))
errors.add(errorField,new ActionError("error." + errorField + ".null"));

return errors;
alan do
Ranch Hand

Joined: Apr 14, 2005
Posts: 354
import the GenericValidator class and use the isBlankOrNull method.

-/a<br />certified slacker...yes, my last name is 'do' - <a href="" target="_blank" rel="nofollow"></a>
I agree. Here's the link:
subject: String value substitution
Similar Threads
ActionError problem in Struts 2.0
struts form being cleared in IE 6
Error with validation
ValidatorForm exception in struts framework
Advice for Struts forms with validation