【问题标题】:Controller returning 404 page with void method控制器使用 void 方法返回 404 页面
【发布时间】:2015-12-06 20:13:13
【问题描述】:

我正在使用 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

有什么想法吗?

【问题讨论】:

  • 您是否有任何显示事件顺序的日志消息?
  • 日志文件中没有错误消息,我看到的最后一条是来自if(license.getIsSubmitted()){ log.debug("Files already submitted"); }的“文件已提交”
  • 提交后你想做什么?什么都不显示?在这种情况下,您是否使用 ajax 发布表单?

标签: java spring-mvc servlets spring-4


【解决方案1】:

就像将@ResponseBody 放入您的 void 方法一样简单。 Further reading

【讨论】:

    【解决方案2】:

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

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

    在你的控制器完成它的工作之后,你设置一个适当的 HttpServletResponse,就像我们过去对“普通”Servlet 所做的那样,在诸如 Spring 之类的 MVC 框架出现之前让我们的生活更轻松:-)

    控制器是 MVC 模式的三个组件之一。一个标准控制器应该返回 something,一个“视图”——即使只是一个 HTTP 200 OK 也是空的——这就是为什么它 not 通常是无效的,并且可能返回一个字符串、ModelAndView 或 ResponseBody(RESTFull 控制器)。

    【讨论】:

    • 你说得对,我错过了“需要退货”的部分。谢谢兄弟,我没有应用您的更改,但我知道为什么不起作用。
    猜你喜欢
    • 2019-10-06
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2020-05-12
    相关资源
    最近更新 更多