1. 程式人生 > >使用jodd form簡化SpringMVC中Controller的配置

使用jodd form簡化SpringMVC中Controller的配置

Jodd是一個開源專案,有一個好用的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><taglib-uri>jodd</taglib-uri><taglib-location>/WEB-INF/lib/jodd.jar</taglib-location </taglib> 或者

任意的一個javaBean做為FormBean

package joddform;

public class User {

 private String userName;

 private String password;

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

}

JSP頁面使用jodd tag:,比如對應使用者登入頁面的

<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ include file="/WEB-INF/jsp/common/taglib.jsp"%>
<html>
 <head>
  <title>jodd from測試</title>
 </head>

 <body>
  <jodd:form bean="user" scope="session">
   <form action="joddform.do" method="post">

    <table width="300" align="center">

     <tr>
      <td>
       使用者名稱:
      </td>

      <td>
       <input type="text" name="userName">
      </td>

     </tr>

     <tr>
      <td>
       密碼:
      </td>

      <td>
       <input type="password" name="password">
      </td>
     </tr>


     <tr>
      <td>

       <input type="submit" name="Submit" value="登入">
      </td>
     </tr>
    </table>
   </form>
  </jodd:form>

 </body>
</html>

Dispatch-servelt.xml中對controller的配置

<bean id="joddformController" class="joddform.JoddformController">
 </bean>

 不再需要這種方式:

<!--        <bean id="joddformController" class="joddform.JoddformController">            <property name="commandClass" value="user" />              <property name="formView" value="joddform/joddform" />           </bean>     --> package joddform; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jodd.bean.BeanUtil; import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; public class JoddformController implements Controller {  public ModelAndView handleRequest(HttpServletRequest request,  HttpServletResponse response) throws Exception {   User user = new User();   BeanUtil.load(user, request);   if (StringUtils.isBlank(user.getUserName())
    && StringUtils.isBlank(user.getPassword())) {
   return new ModelAndView("joddform/joddform");
  } else {
   System.out.println("使用者名稱:" + user.getUserName() + " 密碼:"
     + user.getPassword());
   return null;
  }
 }
}

 <bean id="joddformhandlerMapping"
  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
   <value>joddform.do=joddformController</value>
  </property>
 </bean>
 <bean id="joddformController" class="joddform.JoddformController">
 </bean>

 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>