info.michaelchurch.arule.controllers.ContactController.java Source code

Java tutorial

Introduction

Here is the source code for info.michaelchurch.arule.controllers.ContactController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package info.michaelchurch.arule.controllers;

import info.michaelchurch.arule.services.ContactProcessor;
import info.michaelchurch.arule.valueobject.Comment;
import javax.inject.Inject;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author michael
 */
@Controller
public class ContactController {

    @Inject
    public ContactController(ContactProcessor contactProcessor) {
        this.contactProcessor = contactProcessor;
    }

    private ContactProcessor contactProcessor;

    @RequestMapping(method = RequestMethod.POST)
    public String Process(Model model, @Valid Comment comment, BindingResult result) {

        if (result.hasErrors()) {
            return "contact";
        }

        if (contactProcessor.process(comment)) {
            model.addAttribute("verdict", "Success");
        } else {
            model.addAttribute("verdict", "Failure");
        }
        return "contact";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String loadForm(Model model) {
        model.addAttribute(new Comment());
        return "contact";
    }

    @ExceptionHandler(Exception.class)
    public String handleError() {
        return "error";
    }

}