Spring

Dynamic list binding in Spring MVC

http://mattfleming.com/node/134

Validation in MultiActionController

This example just works!

public class EventValidator implements Validator {
    public boolean supports(Class clazz) {
        //return EventDO.class.equals(clazz);
        return clazz.equals(EventDO.class);
    }
 
    public void validate(Object obj, Errors e) {
        ValidationUtils.rejectIfEmpty(e, "title","error_mandatory","title cannnot be null");
    }
}
 
----
 
public class EventController extends MultiActionController{
    BindingResult errors; //Declaring errors as instance variable to catch error
 
    //the last param acts as a command object
    public ModelAndView submitEvent(HttpServletRequest request, HttpServletResponse response, EventDO event) throws Exception {
 
        validate(event);
        if (errors.hasErrors()) {
 
            return  new ModelAndView("events/events",errors.getModel());
        }
 
        return new ModelAndView("events/events");
   }
    public void validate(Object command)
    {
        ValidationUtils.invokeValidator(new EventValidator(), command, errors);
    }
 
    @Override
    protected void bind(HttpServletRequest request, Object command) throws Exception {
        ServletRequestDataBinder binder = createBinder(request, command);
        binder.bind(request);
        errors = binder.getBindingResult();
    }
}
 
----
 
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
 
<html>
 
<body>
<form:form method="post" name="suggestionsController" action="events.htm?method=submitEvent">
<form:errors path="title"/> <input name="title"/>
 
</form:form>
 
</body>
 
</html>
 
----
 
     <bean id="eventController" class="com.iranoz.controller.EventController">
            <property name="methodNameResolver" ref="paramResolver"/> 
        <property name="validators" ref="eventValidator"/>
      </bean>

Spring method security in controllers

Method security is better to be done in service layer not controllers. Controllers can be protected using url (<intercept-url pattern="/productManager.htm/**" access="ROLE_SUPER_USER" />)
http://static.springsource.org/spring-security/site/faq.html#faq-method-security-in-web-context

Cron

    <bean name="everyNightJobs" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.ads.common.service.job.DailyJobs" />
        <property name="jobDataAsMap">
            <map>
                <entry key="jdbcDao"><ref bean="jdbcDao"/></entry> 
           </map>
        </property>
    </bean>
 
    <bean id="everyNight" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="everyNightJobs" />
        <!-- 
        run every morning at 3 AM. 
        -->
        <property name="cronExpression" value="0 0 3 * * ?" />
    </bean>
 
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="everyNight" />
            </list>
        </property>
    </bean>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License