posts - 22, comments - 17, trackbacks - 0, articles - 15
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          使用Jodd的優點

          Posted on 2006-12-02 15:48 碼農cz 閱讀(2073) 評論(0)  編輯  收藏 所屬分類: Spring Around
          簡化和統一 controller ,拋棄 extends SimpleFormController ,統一使用 implements Controller 的方式。

          2.? 簡化 JSP 頁面的 bind, 不需要一個字段一個字段的綁定。

          3.? bean 沒有任何要求,可以使用任意的 bean 做為 formBean.

          ?

          使用方法簡介:

          jodd.jar 放到 web-inf->lib 下,在 web.xml 里聲名標簽:

          < taglib >

          ???????
          < taglib-uri > jodd </ taglib-uri >

          ???????
          < taglib-location > /WEB-INF/lib/jodd.jar </ taglib-location >

          ????
          </ taglib >

          ?

          任意的一個 javaBean做為FormBean

          package ?caike;?

          public ? class ?User?{

          ???????
          private ?String?userName;?

          ???????
          private ?String?password;
          ?????

          ???????
          public ?String?getPassword()?{

          ??????????????
          return ?password;

          ???????}?

          ???????
          public ? void ?setPassword(String?pwd)?{

          ??????????????
          this .password? = ?pwd;

          ???????}?

          ???????
          public ?String?getUserName()?{

          ??????????????
          return ?userName;

          ???????}?

          ???????
          public ? void ?setUserName(String?username)?{

          ??????????????
          this .userName? = ?username;

          ???????}
          ?

          }

          JSP 頁面使用 jodd tag:, 比如對應用戶登錄頁面的

          <% @page?import = " caike.User " %>

          <% @taglib?uri = " jodd " ?prefix = " jodd " %>

          ?

          < jodd:form? beans ="user" ?scopes ="session" >

          < form? action ="my.htm" ?method ="post" ? >
          ?

          ????
          < table? width ="300" ?border ="0" ?cellspacing ="0" ?cellpadding ="0"

          ???????align
          ="center" ?class ="white" > ?

          ???????
          < tr >

          ???????????
          < td? height ="32" ?align ="right" ?width ="107" > 用戶名: </ td >

          ???????
          < td? height ="32" ?width ="193" >< input? type ="text" ?name ="userName"

          ??????????????class
          ="input" ?size ="20" > ? </ td > ???????? ?

          ???????
          </ tr >

          ???????
          < tr >

          ???????????
          < td? height ="33" ?align ="right" ?width ="107" > 密碼: </ td >

          ???????????
          < td? height ="33" ?width ="193" >< input? type ="password" ?name ="password"

          ??????????????class
          ="input" ?size ="21" ></ td >

          ???????
          </ tr >
          ??????

          ????????
          < tr >

          ???????????
          < td? height ="69" ?align ="center" ?colspan ="2" >

          ????????????
          < input? type ="submit" ?name ="Submit" ?value ="登錄" >

          ???????
          </ tr >

          ????
          </ table >

          </ form >

          </ jodd:form >

          ?

          Dispatch-servelt.xml 中對 controller 的配置

          <bean id="myController" class="caike.MyController">????

          ??? </bean>

          ??? 不再需要這種方式:

          ??? <!--

          ?????? <bean id="myController" class="caike.MyFormController">????

          ?????? <property name="commandClass" value="caike" />??????

          ?????? <property name="formView" value="userForm" />???

          ?????? </bean>

          ??? -->

          controller 中取出 user

          package ?caike;?

          import ?javax.servlet.http.HttpServletRequest;

          import ?javax.servlet.http.HttpServletResponse;?

          import ?jodd.bean.BeanUtil;?

          import ?org.springframework.web.servlet.ModelAndView;

          import ?org.springframework.web.servlet.mvc.Controller;

          ?

          public ? class ?MyController? implements ?Controller?{

          ?

          ???????
          public ?ModelAndView?handleRequest(HttpServletRequest?request,

          ?????????????????????HttpServletResponse?response)?
          throws ?Exception?{

          ?

          ??????????????User?user?
          = ? new ?User();

          ??????????????BeanUtil.load(user,?request);

          ??????????????System.out.println(
          " 用戶名: " ? + ?user.getUserName()? + ? " ?密碼: " ? + ?user.getPassword());

          ??????????????
          return ? null ;

          ???????}

          ?

          }

          ?

          需要注意的地方:

          表單中對應的名字 name javaBean 里對應的屬性名要相同。

          <input type="text" name="userName"

          ????????????? class="input" size="20">

          public class User {

          ?????? private String userName;
          ?????? .......


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 丹棱县| 和林格尔县| 丰都县| 辉县市| 历史| 萨嘎县| 自治县| 杭锦后旗| 库尔勒市| 四平市| 乐平市| 林周县| 汉沽区| 阿拉善左旗| 福贡县| 离岛区| 麻阳| 黄山市| 诏安县| 阳新县| 安徽省| 建宁县| 海兴县| 额尔古纳市| 五寨县| 托里县| 仪陇县| 宜兴市| 梁河县| 克东县| 桂林市| 农安县| 恩平市| 精河县| 宣恩县| 察隅县| 霍州市| 如皋市| 长垣县| 万源市| 敦煌市|