Controllers in Web MVC framework, interpret user inputs and transform such input in to a view that is to be viewed by the user. Spring has implemented several controllers in a very abstractive way. They are form-specific controllers, command-based controllers and controllers that execute wizard-style logic. Spring's basic for the controllers is the org.springframework.web.servlet.mvc.Controller interface.
public interface Controller {
ModelAndView handleRequest(
HttpServletRequest request,
HttpServletResponse response) throws Exception;
}
handleRequest method is responsible for handling a request and returning an appropriate modelandview.
AbstractController - Spring controllers inherit the AbstractController to get a basic infrastructure. Then we have to override the method handleRequestInternal(HttpServletRequest, HttpServletResponse). Here is an example.
public class MyController extends AbstractController {
public ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView("hello");
mav.addObject("message", "Hello World!");
return mav;
}
}
ParameterizableViewController - This is same as the AbstractController except we can specify the view name that it will return in the web application context.
UrlFilenameViewController - This checks the URL and retrieves the file name of the file request and uses that as the view name.
MultiActionController - This allows the aggregation of multiple request handling methods to one controller.
Command Controllers
Command controllers allow to interact with data objects and dynamically bind the parameters from the HttpServletRequest to the data object specified.
AbstractCommandController - This is a command controller that is capable of binding request parameters to a data object. This controller does not provide form functionality.
AbstractFormController - Provides the form submission support.
SimpleFormController - A form controller that provides even more support when creating a form with a corresponding command object.
Wednesday, February 23, 2011
Tuesday, February 1, 2011
Data binding in Spring MVC
Property editors can be used to bind String to various data types in Spring MVC. Here, enum, int and Date bindings are described.
EnumEditor:
import java.beans.PropertyEditorSupport;
@SuppressWarnings("unchecked")
public class EnumEditor extends PropertyEditorSupport {
private Class clazz;
public EnumEditor(Class clazz) {
this.clazz = clazz;
};
public String getAsText() {
return (getValue() == null ? "" : ((Enum) getValue()).name());
}
public void setAsText(String text) throws IllegalArgumentException {
setValue(Enum.valueOf(clazz, text));
}
EnumClass:
public enum EnumClass{
RED, GREEN, BLUE
}
Register in controller:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(EnumClass.class,
new EnumEditor(EnumClass.class));
}
import java.beans.PropertyEditorSupport;
@SuppressWarnings("unchecked")
public class IntEditor extends PropertyEditorSupport {
private Class clazz;
public IntEditor(Class clazz) {
this.clazz = clazz;
}
public String getAsText() {
Integer value = (Integer) getValue();
if (value.intValue() == 0) {
return "";
} else {
return value.toString();
}
}
public void setAsText(String value) {
if (value != null && value.length() > 0) {
setValue(Integer.parseInt(value));
} else {
setValue(0);
}
}
Register in controller:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(int.class, new IntEditor(int.class));
}
@SuppressWarnings("rawtypes")
public class DateEditor extends PropertyEditorSupport {
private Class clazz;
public DateEditor(Class clazz) {
this.clazz = clazz;
}
public void setAsText(String value) {
try {
setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));
} catch (ParseException e) {
setValue(null);
}
}
public String getAsText() {
return new SimpleDateFormat("yyyy-MM-dd hh:mm a").format((Date) getValue());
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
public Class getClazz() {
return clazz;
}
}
Register in controller:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new DateEditor(Date.class));
}
- Enum Type
EnumEditor:
import java.beans.PropertyEditorSupport;
@SuppressWarnings("unchecked")
public class EnumEditor extends PropertyEditorSupport {
private Class clazz;
public EnumEditor(Class clazz) {
this.clazz = clazz;
};
public String getAsText() {
return (getValue() == null ? "" : ((Enum) getValue()).name());
}
public void setAsText(String text) throws IllegalArgumentException {
setValue(Enum.valueOf(clazz, text));
}
EnumClass:
public enum EnumClass{
RED, GREEN, BLUE
}
Register in controller:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(EnumClass.class,
new EnumEditor(EnumClass.class));
}
- Int Type
import java.beans.PropertyEditorSupport;
@SuppressWarnings("unchecked")
public class IntEditor extends PropertyEditorSupport {
private Class clazz;
public IntEditor(Class clazz) {
this.clazz = clazz;
}
public String getAsText() {
Integer value = (Integer) getValue();
if (value.intValue() == 0) {
return "";
} else {
return value.toString();
}
}
public void setAsText(String value) {
if (value != null && value.length() > 0) {
setValue(Integer.parseInt(value));
} else {
setValue(0);
}
}
Register in controller:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(int.class, new IntEditor(int.class));
}
- Date Type
@SuppressWarnings("rawtypes")
public class DateEditor extends PropertyEditorSupport {
private Class clazz;
public DateEditor(Class clazz) {
this.clazz = clazz;
}
public void setAsText(String value) {
try {
setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));
} catch (ParseException e) {
setValue(null);
}
}
public String getAsText() {
return new SimpleDateFormat("yyyy-MM-dd hh:mm a").format((Date) getValue());
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
public Class getClazz() {
return clazz;
}
}
Register in controller:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new DateEditor(Date.class));
}
Subscribe to:
Posts (Atom)