创建springmvc orm应用程序。有两个控制器,即playercontroller和teamcontroller,用requestmapping注解以执行与这些控制器相关的操作。我使用java配置web.xml、dispatcher和applicationcontextconfig为datasource、sessionfactory和hibernatetransactionmanager.using mysql数据库定义bean。
在运行应用程序时,它会给出错误-javax.servlet.servletexception:servlet[dispatcher]的servlet.init()引发异常org.springframework.beans.factory.unsatifiedpendencyException
这里是webmvcconfig->
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.stackroute")
public class WebMvcConfig implements WebMvcConfigurer {
/*
* Define the bean for view resolver so that it can be used to resolve the JSP
* files which are existing in /WEB-INF/views folder. A ViewResolver is capable
* of mapping logical view names to actual views, such as a JSP or a HTML page.
*/
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
return bean;
}
}
当我将componentscan basepackage作为-com.stackroute.model
应用程序给出的错误是-get/sa\u spring\u mvc\u orm\u assessment/debug o.s.web.servlet.dispatcherservlet没有Map-完成404找不到
当我将basepackage更改为-com.stackroute时
应用程序给出了如下错误-
错误
例外
javax.servlet.ServletException: Servlet.init() for servlet [dispatcher] threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:832)
Root Cause
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'teamController' defined in file [/Users/vikash/Desktop/Assesment/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sa_spring_mvc_orm_assessment/WEB-INF/classes/com/stackroute/controller/TeamController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'teamServiceImpl' defined in file [/Users/vikash/Desktop/Assesment/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sa_spring_mvc_orm_assessment/WEB-INF/classes/com/stackroute/service/TeamServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.stackroute.dao.TeamDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:701)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:577)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:529)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:169)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:832)
Root Cause
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'teamServiceImpl' defined in file [/Users/vikash/Desktop/Assesment/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/sa_spring_mvc_orm_assessment/WEB-INF/classes/com/stackroute/service/TeamServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.stackroute.dao.TeamDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:273)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1237)
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:701)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:577)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:529)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:169)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:832)
Root Cause
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.stackroute.dao.TeamDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1644)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1203)
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:273)
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1237)
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1164)
org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:857)
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:760)
org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1308)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1154)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:701)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:577)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:529)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:169)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:832)
我应该给哪个基本包裹?
com.stackroute是根包,所有其他类都在com.stackroute.model com.stackroute.controller中
com.stackroute.config-包含webmvcconfig applicationcontextconfig和WebAppInitialzer
道和服务也是如此。
下面是我的控制器和applicationcontextconfig的代码
代码
@Controller
@RequestMapping(path="/team")
public class TeamController {
/*
*
*
* 1. Display the list of existing Team from the persistence data. Each Team
* element should contain Team name, Team lead and list of players 2. Add a new
* team which should contain the Name, Lead 3. Delete an existing team 4. Update
* an existing team
*
*/
private TeamService teamService;
@Autowired
public TeamController(TeamService teamService) {
this.teamService = teamService;
}
@GetMapping(value = "/")
// @GetMapping(value = "/team")
public String indexPage(ModelMap model) throws TeamDoesNotExistException{
Player player = new Player();
Team team = new Team();
List<Team> teams = this.teamService.getAllTeams();
model.addAttribute("teams", teams);
model.addAttribute("team", team);
model.addAttribute("player", player);
return "index";
}
// @PostMapping(value = "/team/add")
@PostMapping(value = "/add")
public String addTeam(@ModelAttribute("team") Team newTeam, ModelMap model) {
if (newTeam.getName().isEmpty() || newTeam.getLead().isEmpty() || newTeam.getPlayers().isEmpty()) {
Player player = new Player();
Team team = new Team();
model.addAttribute("player", player);
model.addAttribute("team", team);
model.addAttribute("teams", teamService.getAllTeams());
return "index";
}
try {
teamService.createTeam(newTeam);
model.addAttribute("team", newTeam);
model.addAttribute("teamPlayers", newTeam.getPlayers());
} catch (TeamAlreadyExistException e) {
model.addAttribute("teamErrorMessage", e.toString());
return "index";
}
return "redirect:/";
// return "redirect:/team";
}
// @PostMapping(value = "/team/update")
@PostMapping(value = "/update")
public String updateTeam(@ModelAttribute("team") Team newTeam, ModelMap model) {
if (newTeam.getName().isEmpty() || newTeam.getLead().isEmpty() || newTeam.getPlayers().isEmpty()) {
Player player = new Player();
Team team = new Team();
model.addAttribute("player", player);
model.addAttribute("team", team);
model.addAttribute("teams", teamService.getAllTeams());
return "index";
}
try {
teamService.updateTeam(newTeam);
model.addAttribute("team", newTeam);
model.addAttribute("teamPlayers", newTeam.getPlayers());
} catch (TeamDoesNotExistException e) {
model.addAttribute("teamErrorMessage", e.toString());
e.printStackTrace();
return "index";
}
return "redirect:/index";
}
// @GetMapping("/team/delete")
@GetMapping("/delete")
public String deleteTeam(@RequestParam("name") String teamName, ModelMap model) {
if(teamName.equals(""))
{
Player player = new Player();
Team team = new Team();
model.addAttribute("player", player);
model.addAttribute("team", team);
model.addAttribute("teams",teamService.getAllTeams());
return "index";
}
try {
teamService.deleteTeam(teamName);
} catch (TeamDoesNotExistException e) {
model.addAttribute("errorMessage", e.toString());
e.printStackTrace();
return "index";
}
return "redirect:/";
// return "redirect:/team";
}
// @GetMapping("/team/players")
@GetMapping("/players")
public String getTeamPlayers(@RequestParam("name") String teamName, ModelMap model) {
try {
List<Player> players = teamService.getAllPlayers(teamName);
model.addAttribute("teamPlayers", players);
for (Player player : players) {
model.addAttribute("team", player.getTeam());
}
} catch (TeamDoesNotExistException e) {
model.addAttribute("teamErrorMessage", e.toString());
e.printStackTrace();
}
return "redirect:/index";
}
}
@Controller
@RequestMapping(path= "/player")
public class PlayerController {
/*
*
*
* 1. Display the list of existing Player from the persistence data. Each Player
* element should contain emailId, name, and details. 2. Add a new player which
* should contain the emailId, name, and details 3. Delete an existing player 4.
* Update an existing player
*
*/
private PlayerService playerService;
@Autowired
public PlayerController(PlayerService playerService) {
this.playerService = playerService;
}
// @GetMapping("/player")
// @RequestMapping(value ="player" , method=RequestMethod.GET)
@GetMapping("/")
public String indexPage(ModelMap model) {
List<Player> players = playerService.getAllPlayers();
model.addAttribute("teamPlayers", players);
for (Player player : players) {
model.addAttribute("teams", player.getTeam());
model.addAttribute("player", player);
}
return "index";
}
// @PostMapping(value = "player/addPlayer")
@PostMapping(value = "/addPlayer")
public String addPlayer(@ModelAttribute("player") Player newPlayer, ModelMap model)
throws PlayerAlreadyExistException {
if (newPlayer.getDetails().isEmpty() || newPlayer.getEmailId().isEmpty() || newPlayer.getName().isEmpty()
|| newPlayer.getName().isEmpty()) {
Player player = new Player();
Team team = new Team();
model.addAttribute("player", player);
model.addAttribute("team", team);
return "index";
}
try {
playerService.addPlayer(newPlayer);
model.addAttribute("player", newPlayer.getTeam());
model.addAttribute("teamPlayers", playerService.getAllPlayers());
return "redirect:/";
} catch (PlayerAlreadyExistException e) {
model.addAttribute("errorMessage", e.toString());
e.printStackTrace();
return "index";
}
}
// @GetMapping("player/delete")
@GetMapping("/delete")
public String deletePlayer(@RequestParam("emailId") String emailId, ModelMap model) {
try {
playerService.deletePlayer(emailId);
return "redirect:/";
} catch (PlayerDoesNotExistException e) {
model.addAttribute("errorMessage", e.toString());
e.printStackTrace();
return "index";
}
}
// @GetMapping("player/get")
@GetMapping("/get")
public String getPlayer(@RequestParam("emailId") String emailId, ModelMap model) {
try {
Player player = this.playerService.getPlayer(emailId);
model.addAttribute("player", player);
return "redirect:/";
} catch (PlayerDoesNotExistException e) {
e.printStackTrace();
model.addAttribute("getPlayerErrorMessage", e.toString());
return "index";
}
}
// @PostMapping(value = "player/update")
@PostMapping(value = "/update")
public String updatePlayer(@ModelAttribute("player") Player newPlayer, ModelMap model) {
if (newPlayer.getDetails().isEmpty() || newPlayer.getEmailId().isEmpty() || newPlayer.getName().isEmpty()
|| newPlayer.getName().isEmpty()) {
Player player = new Player();
Team team = new Team();
model.addAttribute("player", player);
model.addAttribute("team", team);
return "index";
}
try {
playerService.updatePlayer(newPlayer);
return "redirect:/index";
} catch (PlayerDoesNotExistException e) {
model.addAttribute("errorMessage", e.toString());
return "index";
}
}
}
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:db.properties")
public class ApplicationContextConfig {
// Read db properties with @Value annotation and store private variables
// see the variables names in db.properties file of resources folder.
// MYSQL_USER
// MYSQL_PASSWORD
// MYSQL_DRIVER
// MYSQL_URL
@Value("${MYSQL_USER}")
private String userName;
@Value("${MYSQL_PASSWORD}")
private String password;
@Value("${MYSQL_DRIVER}")
private String driver;
@Value("${MYSQL_URL}")
private String url;
/*
* Define the bean for DataSource. In our application, we are using MySQL as the
* dataSource. To create the DataSource bean, we need to know: 1. Driver class
* name 2. Database URL 3. UserName 4. Password
*/
@Bean("datasource")
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public LocalSessionFactoryBean getSessionFactory(DataSource dataSource) throws IOException {
LocalSessionFactoryBean loclSessionFactoryBean = new LocalSessionFactoryBean();
loclSessionFactoryBean.setDataSource(dataSource);
// loclSessionFactoryBean.setPackagesToScan(new String[] { "com.stackroute.model" });
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
properties.put("hibernate.show_sql", "true");
// properties.put("hibernate.hbm2ddl.auto", "auto");
loclSessionFactoryBean.setHibernateProperties(properties);
loclSessionFactoryBean.setAnnotatedClasses(Player.class, Team.class);
loclSessionFactoryBean.afterPropertiesSet();
return loclSessionFactoryBean;
}
@Bean
public HibernateTransactionManager getHibernateTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory);
return transactionManager;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!