有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring MVC格式化程序/转换器显示400个错误请求

我对spring&;冬眠我正在写一个新项目,与本教程有多对多的双向关系-http://websystique.com/springmvc/springmvc-hibernate-many-to-many-example-annotation-using-join-table/,但我扩展了它。我有一个带有游戏添加(在教程中是User)和类别添加(在教程中是UserProfile)的表单。我为类别另外编写了formatter,但当我试图通过网站添加它时,它返回了400个错误请求。我的项目出了什么问题?我想添加其他形式的游戏和其他形式的类别。以下是我的格式化程序:

@Component
public class CategoryToGameConverter implements Converter<Object, Category>{
    @Autowired
    CategoryService categoryService;

    public Category convert(Object element) {
        Integer id = Integer.parseInt((String) element);
        Category category = categoryService.findById(id);
        System.out.println("Category: " + category);
        return category;
    }
}

@Component
public class GameToCategoryConverter implements Converter<Object, Game>{

    @Autowired
    GameService gameService;

    public Game convert(Object element) {
        Integer id = Integer.parseInt((String) element);
        Game game = gameService.findById(id);
        System.out.println("Game: " + game);
        return game;
    }
}

控制器:

@Controller
@RequestMapping("/games")
@SessionAttributes("categories")
public class AppController {

@Autowired
GameService gameService;

@Autowired
CategoryService categoryService;

@Autowired
MessageSource messageSource;

/**
 * This method will list all existing users.
 */
@RequestMapping(value = { "/", "/list" }, method = RequestMethod.GET)
public String listUsers(ModelMap model) {
    List<Game> games = gameService.findAllGames();
    model.addAttribute("games", games);
    return "gameslist";
}

/**
 * This method will provide the medium to add a new user.
 */
@RequestMapping(value = { "/newgame" }, method = RequestMethod.GET)
public String newGame(ModelMap model) {
    Game game = new Game();
    model.addAttribute("game", game);
    model.addAttribute("edit", false);
    return "registration";
}

/**
 * This method will be called on form submission, handling POST request for
 * saving user in database. It also validates the user input
 */
@RequestMapping(value = { "/newgame" }, method = RequestMethod.POST)
public String saveGame (@Valid Game game, BindingResult result,
        ModelMap model) {

    if (result.hasErrors()) {
        return "registration";
    }

    gameService.saveGame(game);

    model.addAttribute("success", "Game " + game.getGameName() + " "+ game.getGameDescription() + " registered successfully");
    //return "success";
    return "registrationsuccess";
}


@ModelAttribute("categories")
public Set<Category> initializeCategories() {
    return new HashSet<>(categoryService.findAll());
}

}

@Controller
@RequestMapping("/k")
public class CategoryController {

@Autowired
GameService gameService;

@Autowired
CategoryService categoryService;

@Autowired
MessageSource messageSource;

@RequestMapping(value = { "/{categoryName}" }, method = RequestMethod.GET)
public String viewGamesByCategory(@PathVariable String categoryName, ModelMap model) {
    try {
        Category category = categoryService.findByName(categoryName);
        Set<Game> gameSet = category.getGamesList();
        model.addAttribute("catName", categoryName);
        model.addAttribute("gamesList", gameSet);
        model.addAttribute("exception", false);
    } catch (NullPointerException exc) {
        exc.printStackTrace();
        model.addAttribute("exception", true);
    }
        return "category";
}
@RequestMapping("/list")
public String showCategoryList(ModelMap model) {
    model.addAttribute("categoryList", new HashSet<>(categoryService.findAll()));
    return "categorylist";
}

@RequestMapping(value = { "/new/category" }, method = RequestMethod.GET)
public String newCategory(ModelMap model) {
    Category category = new Category();
    System.out.println(category.getCategoryName());
    model.addAttribute("category", category);
    model.addAttribute("edit", false);
    return "categoryregistration";
}

@RequestMapping(value = { "/new/category" }, method = RequestMethod.POST)
public String saveCategory (@Valid Category category, BindingResult result,
                        ModelMap model) {
    System.out.println("zapis");
    if (result.hasErrors()) {
        return "categoryregistration";
    }

    categoryService.saveCategory(category);
    model.addAttribute("success", "Category " + category.getCategoryName() + " registered successfully");
    //return "success";
    return "categoryregistrationsuccess";
}
}

谢谢你的回复


共 (0) 个答案