Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources] - نسخه متنی

Larne Pekowsky

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







19.5. Eliminating Form Beans


The form bean used in Listing 19.8 had no code and was reduced to a simple value container. This happens fairly often when using the validator because the provided validations cover all common situations. In this case the bean can be eliminated through the use of a DynaBean, as covered in Chapter 8. Recall from that chapter that such beans are somewhat like Maps, except that values have a type as well as a name.

A DynaBean can be used as a form bean by replacing the definition of the form-bean in struts-config.xml with an entry defining the properties of the bean. CalculatorForm2 could be completely replaced with the following entry:


<form-bean
name="calculatorForm3"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="number1" type="java.lang.Double"/>
<form-property name="number2" type="java.lang.Double"/>
</form-bean>

The action handler needs only a slight modification to use this DynaBean, which is shown in Listing 19.10.


Listing 19.10. Using a dynamic bean


package com.awl.toolbook.chapter19;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.util.Locale;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.util.*;
import org.apache.struts.validator.DynaValidatorForm;
public final class CalculatorAction3 extends Action {
public ActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
DynaValidatorForm f = (DynaValidatorForm) form;
// Build the model
Calculator calc = new Calculator();
calc.setNumber1(
((Double) f.get("number1")).doubleValue());
calc.setNumber2(
((Double) f.get("number2")).doubleValue());
calc.computeSum();
// Store the model in the request so the result
// page can get to it
request.setAttribute("calc",calc);
return (mapping.findForward("success"));
}
}

As this example illustrates, values are obtained from a dynamic bean through the DynaValidatorForm class. Individual values are obtained via called to get(), just as they are with a Map. Here the values are cast to Doubles, and then doubleValue() is used to get the primitive value to hand off to the bean.


/ 207