spring mvc 404-资源不可用

时间:2015-08-17 19:37:50

标签: spring-mvc

您好我正在使用apache derby和Spring mvc来访问DB的联系方式。 我面临“HTTP状态404-”,因为“请求的资源不可用”。 以下是我的资源。

WEB.XML:

<display-name>SpringMvcJdbcTemplate</display-name>
<context-param>
    <param-name>contextClass</param-name>
    <param-value>
        org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>SpringDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>SpringDispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

MvcConfiguration.java:

@Configuration
@ComponentScan(basePackages="net.codejava.spring")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public DataSource getDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("org.apache.derby.jdbc.ClientDriver");
    //dataSource.setUrl("jdbc:mysql://localhost:3306/contactdb");
    dataSource.setUrl("jdbc:derby://localhost:1527/APP");

    return dataSource;
}

@Bean
public ContactDAO getContactDAO() {
    return new ContactDAOImpl(getDataSource());
}

HomeController.java:

public class HomeController {

@Autowired
private ContactDAO contactDAO;

@RequestMapping(value="/")
public ModelAndView listContact(ModelAndView model) throws IOException{
    List<Contact> listContact = contactDAO.list();
    model.addObject("listContact", listContact);
    model.setViewName("home");

    return model;
}

@RequestMapping(value = "/newContact", method = RequestMethod.GET)
public ModelAndView newContact(ModelAndView model) {
    Contact newContact = new Contact();
    model.addObject("contact", newContact);
    model.setViewName("ContactForm");
    return model;
}

@RequestMapping(value = "/saveContact", method = RequestMethod.POST)
public ModelAndView saveContact(@ModelAttribute Contact contact) {
    contactDAO.saveOrUpdate(contact);
    return new ModelAndView("redirect:/");
}

@RequestMapping(value = "/deleteContact", method = RequestMethod.GET)
public ModelAndView deleteContact(HttpServletRequest request) {
    int contactId = Integer.parseInt(request.getParameter("id"));
    contactDAO.delete(contactId);
    return new ModelAndView("redirect:/");
}

@RequestMapping(value = "/editContact", method = RequestMethod.GET)
public ModelAndView editContact(HttpServletRequest request) {
    int contactId = Integer.parseInt(request.getParameter("id"));
    Contact contact = contactDAO.get(contactId);
    ModelAndView model = new ModelAndView("ContactForm");
    model.addObject("contact", contact);

    return model;
}

请让我知道我在哪里犯错误。

0 个答案:

没有答案