1

I am using Spring 4 to develop a web application.

I have this Controller

@RequestMapping(value = "/submitIdesFalse", method = RequestMethod.POST)
public void setSubmitIdesFalse(Map<String, Object> model, Locale loc) throws UnknownHostException, DataIntegrityViolationException, DataException, MysqlDataTruncation{
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    final String userName = authentication.getName();

    final Company company = companyService.getCompanyByUser(userName);
    final Integer reportingYear = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR) - 1; // Previous year
    final License license = licenseService.getLicense(company.getId(), reportingYear);

    if(license.getIsSubmitted()){
        log.debug("Files already submitted");
    }else{
    Date date = new Date();
    license.setId(license.getId());
    license.setIsSubmitted(true);
    license.setDateSubmitted(new Timestamp(date.getTime()));
    license.setUserSubmitted(userName);
    licenseService.saveLicense(license);

    log.debug("User Name is: " + userName);
    log.debug("Company " + company.getName() + " set to Submitted FATCA Status");   

    final String emailList = this.fatcaWebProperties.getProperty(ApplicationPropertyName.EMAIL_LIST.getPropertyName());
    final String[] emailListSplited = emailList.split(",");
    final InetAddress ip = InetAddress.getLocalHost();
    final TimeZone timeZone = TimeZone.getTimeZone("Canada/Eastern");
    final String localDateTime = FatcaWebUtility.getDate();
    final String opesDateTime = FatcaWebUtility.getDateWithTimeZone(timeZone);
    final String companyName = company.getName();
    final Long companyId = company.getId();
    if(!company.getName().equals("OPES")){
        for (int i = 0; i < emailListSplited.length; i++) {
            emailSenderService.sendSubmittedOpesMail(ip, emailListSplited[i], companyId, companyName, localDateTime, opesDateTime, userName);
        }
    }       

}

It's a void method and the form that calls this page is:

<form:form method="post" action="submitIdesFalse.html">                                                                             
    <input type="submit" id="sendGenerateSubmit" value="Generate and Submit" />
</form:form>

After the controller is called it returns a 404 page saying that submitIdesFalse.jsp was not found but my controller is void

I tried to make the method return a String and it returns the page I call, but when I set void it returns this weird 404.

I posted all my controller to make sure I am not missing anything, sorry about the amount of code.

By the way, I am using Tomcat 7.

Any ideas?

José Mendes
  • 950
  • 2
  • 14
  • 30

2 Answers2

3

It's as simple as to put @ResponseBody to your void method. Further reading

Sercan Ozdemir
  • 4,641
  • 3
  • 34
  • 64
  • 1
    This worked just fine for me and IMHO should be the accepted answer (at least as of 2023, seeing the accepted answer is from 2015). – Tobias Gierke Apr 13 '23 at 07:11
2

You if want to keep your controller as void - but I can't see any reason for doing this - you have to manage the HttpServerResponse yourself, so you need something like

public void setSubmitIdesFalse(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response, Locale loc)

After your controller has done its job you set a proper HttpServletResponse, as we were used to do with "normal" Servlets, before MVC frameworks such as Spring came around to make our life easier :-)

A Controller is one of the three component of the MVC pattern. A standard controller is supposed to return something, a "View" - even empty with just a HTTP 200 OK - this is why it is not usually void, and may return a String, a ModelAndView or a ResponseBody (RESTFull controllers).

m c
  • 1,104
  • 1
  • 12
  • 21
  • You are right, I missed the part that I "need to return something". Thank you bro, I didn't apply your changes but I figured out why wasn't working. – José Mendes Sep 10 '15 at 14:51