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

时间:2015-10-30 17:35:10

标签: java spring spring-mvc spring-validator

我来自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}";
  }
}

验证失败而不是渲染&#34; addUser&#34;查看它只是打印&#34; addUser&#34;在屏幕上。我究竟做错了什么?可以看到完整的来源here

修改

如果我按照以下几个答案的建议从@ResponseBody方法移除addUser,我会

  

HTTP状态500 - java.lang.IllegalStateException:都不是   BindingResult也不是bean名称&#39;命令的明确目标对象。   可用作请求属性

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)