控制器使用void方法返回404页面

问题描述 投票:1回答:2

我正在使用Spring 4开发Web应用程序。

我有这个控制器

@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);
        }
    }       

}

这是一个void方法,调用此页面的形式是:

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

控制器被调用后,它返回一个404页面,说找不到submitIdesFalse.jsp,但我的控制器是void

我试图让方法返回一个String并返回我调用的页面,但是当我设置void时它会返回这个奇怪的404。

我发布了我的所有控制器,以确保我没有遗漏任何东西,抱歉代码量。

顺便说一下,我正在使用Tomcat 7

有任何想法吗?

java spring-mvc servlets spring-4
2个回答
2
投票

如果你想让你的控制器保持无效 - 但我看不出有任何理由这样做 - 你必须自己管理HttpServerResponse,所以你需要像

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

在你的控制器完成它的工作后,你设置了一个正确的HttpServletResponse,就像我们习惯于使用“普通”Servlet一样,然后像Spring这样的MVC框架来让我们的生活更轻松:-)

Controller是MVC模式的三个组成部分之一。一个标准的控制器应该返回一些东西,一个“视图” - 甚至是空的,只有一个HTTP 200 OK - 这就是为什么它通常不是空的,并且可能返回一个String,一个ModelAndView或一个ResponseBody(RESTFull控制器)。


0
投票

@ResponseBody放到你的void方法就好了。 Further reading

© www.soinside.com 2019 - 2024. All rights reserved.