I have a problem with spring & hibernate. I am writing a new project with many to many bidirectional relation with this tutorial - http://websystique.com/springmvc/springmvc-hibernate-many-to-many-example-annotation-using-join-table/ , but I extended it. I have a form with game add (in tutorial it is User) and category add ( in tutorial it is UserProfile ) . I wrote formatter additionally for category but it returns 400 bad request when I tried to add it by website . What is wrong in my project? I want to adding games by other form and categories by other form. Here are my formatters:
@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;
}
}
and
@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;
}
}
Controllers:
@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());
}
}
and
@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";
}
}
Thank for your reply!