使用jodd form簡化SpringMVC中Controller的配置
任意的一個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>