Java Spring 4 验证失败时,不渲染表单,而是简单地打印视图名称

Java Spring 4 on failed validation, instead of rendering form it simply prints view name

我来自 PHP 世界,正在尝试在业余时间学习 Java Spring 框架。在我决定向表单添加验证之前,我一直做得很好。下面是我的代码。

package com.invokedynamic.spring;


import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.invokedynamic.spring.User;

/**
 * Handles requests for the application home page.
 */
@Controller
public class RestController {

  private static final Logger logger = LoggerFactory.getLogger(RestController.class);
  private Map<String, User> users = new HashMap<String, User>();

  public RestController() {
    // pre-initialize the list of issuers available ...

      users.put("pinkal", new User("pinkal", "pinkal123", "pinkal@gmail.com", "USA"));
      users.put("mahesh", new User("mahesh", "mahesh123", "mahesh@gmail.com", "USA"));
  }

  /**
   * Simply selects the home view to render by returning its name.
   */
  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String home(Locale locale, Model model) {
    logger.info("Welcome home! The client locale is {}.", locale);

    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);

    model.addAttribute("serverTime", formattedDate );

    return "status";
  }

  @RequestMapping(value="/users", method=RequestMethod.GET)
  @ResponseBody
  public Map<String, User> getAllUsers() {
    logger.info("Inside getAllUsers() method...");

    return users;
  }

  @RequestMapping(value="/user/{username}", method=RequestMethod.GET)
  @ResponseBody
  public User getUserByUsername(@PathVariable("username") String username) {
      User user = users.get(username);

    if (user != null) {
      logger.info("Inside sgetUserByUsername, returned: " + user.toString());
    } else {
      logger.info("Inside getUserByUsername, ticker: " + username + ", NOT FOUND!");
    }
    return user;
  }

  @RequestMapping(value="/user/delete/{username}", method=RequestMethod.GET)
  @ResponseBody
  public User deleteIssuerByTicker(@PathVariable("username") String username) {
      User user = users.remove(username);

    if (user != null) {
      logger.info("Inside deleteIssuerByTicker, deleted: " + user.toString());
    } else {
      logger.info("Inside deleteIssuerByTicker, ticker: " + username + ", NOT FOUND!");
    }
    return user;
  }

  @RequestMapping(value="/user/create", method=RequestMethod.GET)
  public ModelAndView addUser() {

    return new ModelAndView("addUser", "command", new User());
  }

  @RequestMapping(value="/user/addUser", method=RequestMethod.POST)
  @ResponseBody
  public String addUser(@Valid @ModelAttribute("user") User user, BindingResult result, Model model) {
      if (result.hasErrors()) {           
          return "addUser";
      }
    if (user != null) {
      logger.info("Inside addIssuer, adding: " + user.toString());
    } else {
      logger.info("Inside addIssuer...");
    }
    users.put(user.getUsername(), user);
    return "{\"success\":1}";
  }
}

在验证失败而不是呈现 "addUser" 视图时,它只是在屏幕上打印 "addUser"。我究竟做错了什么?完整源码可见here.

编辑

如果我按照下面几个答案的建议从 addUser 方法中删除 @ResponseBody,我会得到

HTTP Status 500 - java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute

这是因为 @ResponseBody 注释。检查这个:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ResponseBody.html

这是因为您正在使用 @ResponseBody 注释。

当您使用此注释时,从此处返回的数据将被视为存在于正文中的数据,而不是被视为视图名称并由 viewResolver 解析。

还将以下属性添加到您的表单标记中,如下所示

<f:form method="POST" action="addUser" commandName="user">