ExamModifyController.java from crlove at Krugle
Show ExamModifyController.java syntax highlighted
package com.witfriend.apollo.web.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.keyvalue.DefaultMapEntry;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.witfriend.apollo.domain.Exam;
import com.witfriend.apollo.domain.exception.exam.ExamExistedException;
import com.witfriend.apollo.service.ExamService;
import com.witfriend.apollo.web.validator.ExamValidator;
import com.witfriend.zeus.domain.exception.IdentifierRequiredException;
import com.witfriend.zeus.domain.exception.IllegalIdentifierException;
import com.witfriend.zeus.web.validator.IdentifierValidator;
/**
* DOCUMENT ME!
* @author wangyz
* @since 2006.10.07
* @version $Id$
*/
public class ExamModifyController extends SimpleFormController {
private ExamService examService;
public ExamModifyController() {
Class<Exam> clazz = Exam.class;
setBindOnNewForm(true);
setCommandClass(clazz);
setCommandName(StringUtils.uncapitalize(ClassUtils.getShortClassName(clazz)));
setSessionForm(true);
setSupportedMethods(new String[] {METHOD_GET, METHOD_POST});
setValidators(new Validator[] {
IdentifierValidator.getInstance(),
ExamValidator.getInstance()
});
}
@Required
public void setExamService(ExamService examService) {
this.examService = examService;
}
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
binder.setAllowedFields(isFormSubmission(request) ? new String[] {Exam.IDENTIFIER_PROPERTY_NAME, "title"} : new String[] {Exam.IDENTIFIER_PROPERTY_NAME});
binder.setRequiredFields(binder.getAllowedFields());
}
@Override
protected void onBindOnNewForm(HttpServletRequest request, Object command, BindException errors) throws Exception {
onBind(request, command, errors);
ValidationUtils.invokeValidator(getValidator(), command, errors);
onBindAndValidate(request, command, errors);
}
@Override
protected void onBind(HttpServletRequest request, Object command, BindException errors) throws Exception {
if(errors.hasFieldErrors(Exam.IDENTIFIER_PROPERTY_NAME)) {
throw new IdentifierRequiredException();
}
}
@Override
protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) throws Exception {
if(errors.hasFieldErrors(Exam.IDENTIFIER_PROPERTY_NAME)) {
throw new IllegalIdentifierException(Exam.class.cast(command).getId());
}
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
try {
return new ModelAndView(onSubmit(command, errors).getViewName()).addObject(Exam.IDENTIFIER_PROPERTY_NAME, Exam.class.cast(command).getId());
} catch(ExamExistedException eee) {
errors.reject(eee.getCode(), eee.getArguments(), eee.getMessage());
}
return showForm(request, response, errors);
}
@Override
protected void doSubmitAction(Object command) throws Exception {
examService.modify(Exam.class.cast(command));
}
@Override
protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
Exam exam = examService.load(Exam.class.cast(command).getId());
if(!isFormSubmission(request)) {
request.getSession().setAttribute(getFormSessionAttributeName(request), exam);
}
return MapUtils.putAll(new HashMap(), new Map.Entry[] {
new DefaultMapEntry(getCommandName(), exam)
});
}
}
See more files for this project here