Jodd 是一個開源項目, http://jodd.sourceforge.net , 有一個好用的 jsp 標簽,可以大大簡化有表單輸入的 controller 。
使用
Jodd
的優點:
<!--[if !supportLists]-->1.<!--[endif]-->簡化和統一controller,拋棄extends SimpleFormController,統一使用implements Controller的方式。
<!--[if !supportLists]-->2.<!--[endif]-->簡化JSP頁面的bind,不需要一個字段一個字段的綁定。
<!--[if !supportLists]-->3.<!--[endif]-->對bean沒有任何要求,可以使用任意的bean做為formBean.
使用方法簡介:
<!--[if !supportLists]-->1.<!--[endif]-->把jodd.jar放到web-inf->lib下,在web.xml里聲名標簽:
??????? < taglib-uri > jodd </ taglib-uri >
??????? < taglib-location > /WEB-INF/lib/jodd.jar </ taglib-location >
???? </ taglib >
<!--[if !supportLists]-->2.<!--[endif]-->任意的一個javaBean做為FormBean
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;
???????}
?
}
<!--[if !supportLists]-->3.<!--[endif]-->在JSP頁面使用jodd tag:,比如對應用戶登錄頁面的
<% @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 >
<!--[if !supportLists]-->4.<!--[endif]-->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>
??? -->
<!--[if !supportLists]-->5.<!--[endif]-->在controller中取出user
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;
.......
http://www.aygfsteel.com/calvin/archive/2005/08/24/10914.html