gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:59:34 25 4
gpt4 key购买 nike

我来自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” View ,而是简单地在屏幕上打印“addUser”。我究竟做错了什么?完整源码可见here .

编辑

如果我删除 @ResponseBody来自addUser下面几个答案建议的方法,我得到

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

最佳答案

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

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

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

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

关于Java Spring 4 验证失败时,它不渲染表单,而是简单地打印 View 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33441792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com