0

I have only Java-based configuration in Spring Application. I have created user entity and Dao for finding / creating users. The controller has two GET methods - one for user and one for users list and one POST method - for creating / editing a user.

After running at Tomcat 7 server it gives 404 error and message that 'The requested resource is not available.

Dispatcher servlet is configured in SpringWebAppInitializer.class:

public class SpringWebAppInitializer  implements WebApplicationInitializer {

 @Override
 public void onStartup(ServletContext servletContext) throws ServletException {

    AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
    appContext.register(ApplicationContextConfig.class);

    servletContext.addListener(new ContextLoaderListener(appContext));

    AnnotationConfigWebApplicationContext dispatcherContext =  new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(DispatcherServlet.class);

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");    
    }
}

View resolver is defined in ApplicationContextConfig.class:

@Configuration
@ComponentScan("pl.wybornie.entity")
@EnableTransactionManagement
public class ApplicationContextConfig extends WebMvcConfigurerAdapter {

@Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {

    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/pages/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
    }
 //other configuration beans...
}

Jsp files for userList and userForm are in WebContent project's folder under: /WEB-INF/pages/user/*.jsp

Controllers responsible for geting views are pasted below:

@Controller
@SessionAttributes({"user"})
public class UserController {

@Autowired
private UserDao userDao;

@RequestMapping(value = "/user_list.html", method = RequestMethod.GET)
public String list(Model model, HttpServletRequest request) {
    model.addAttribute("userList", userDao.usersList());

    return "user/userList";
}

@RequestMapping(value = "/create.html", method = RequestMethod.GET)
public String edit(@RequestParam(value="id", required=false) Long id, Model model) {
    User user = userDao.findOrCreate(id);       
    model.addAttribute("user", user);
    return "user/userForm";
}

@RequestMapping(method = RequestMethod.POST)
public String submitForm(@ModelAttribute("user") User user, BindingResult bindingResult, HttpServletRequest request, Model model) {

    userDao.saveOrUpdate(user);

    return "redirect:user_list.html";
}

  public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
  }
}
argh
  • 143
  • 4
  • 16

1 Answers1

3

Your class should extend WebMvcConfigurerAdapter class.

@Configuration
@ComponentScan("pl.wybornie.entity")
@EnableWebMvc
public class ApplicationContextConfig extends WebMvcConfigurerAdapter{


}
Parth Solanki
  • 3,268
  • 2
  • 22
  • 41