Spring MVC没有找到控制器

时间:2014-04-27 20:18:41

标签: java spring maven spring-mvc

我一直在处理我的网络项目,一切都很顺利,我做了一些改变(我无法弄清楚它是什么),然后事情就停止了。以前当我导航到URL之一时,我会得到我现在创建的页面,我得到404错误。我已经添加了代码示例。

BookController.java

package com.LibraryManagement.controllers;

import java.util.ArrayList;
import java.util.List;



import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.LibraryManagement.access.impl.BookDAOImpl;
import com.LibraryManagement.entities.Book;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookDAOImpl bookDOAImpl;

    /**
     * Create a new book.
     * 
     */
    @RequestMapping(value = "/addBook", method = RequestMethod.GET) 
    public ModelAndView addBook() {                                
        return new ModelAndView("newBook", "book", new Book());
    } 

    @RequestMapping(value = "/addBook", method = RequestMethod.POST)
    public String displayNewBook(@ModelAttribute("book") @Valid Book book, BindingResult result, ModelMap model) {
        if(result.hasErrors()) {
            return "newBook";
        }
        List<Book> books = new ArrayList<Book>();
        books.add(book);
        model.addAttribute("message", "New book");
        model.addAttribute("books", books);
        try {
            bookDOAImpl.createBook(book.getTitle(), book.getAuthor(), book.getPublisher(), book.getPublicationDate(), book.getIsbn(), book.isAvailable());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "displayBooks";
    }  


    /**
     * Delete a book given the ID of the book.
     * 
     */
    @RequestMapping(value = "/deleteBookId", method = RequestMethod.GET) 
    public ModelAndView deleteBookId() {                                
        return new ModelAndView("deleteBookId", "book", new Book());
    }

    @RequestMapping(value = "/deleteBookId", method = RequestMethod.POST)
    public String displayDeletedMemberId(@ModelAttribute("book") Book book, ModelMap model) {
        List<Book> books = new ArrayList<Book>();
        books.add(book);
        model.addAttribute("message", "Delete book with id "+book.getId());
        try {
            bookDOAImpl.deleteBook(book.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "displayBooks";
    }

    /**
     * Delete a book given the name of the book.
     * 
     */
    @RequestMapping(value = "/deleteBookTitle", method = RequestMethod.GET) 
    public ModelAndView deleteBookTitle() {                                
        return new ModelAndView("deleteBookTitle", "book", new Book());
    }

    @RequestMapping(value = "/deleteBookTitle", method = RequestMethod.POST)
    public String displayDeletedMemberTitle(@ModelAttribute("book") Book book, ModelMap model) {
        List<Book> books = new ArrayList<Book>();
        books.add(book);
        model.addAttribute("message", "Delete book with title "+ book.getTitle());
        try {
            bookDOAImpl.deleteBook(book.getTitle());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "displayBooks";
    }

    /**
     * List all books.
     * 
     */
    @RequestMapping(value="/all", method=RequestMethod.GET)
    public String displayAllMembers(ModelMap model){        
        List<Book> books = bookDOAImpl.getAllBooks();
        model.addAttribute("message", "A list of all the books");
        model.addAttribute("books", books);
        return "displayBooks";
    }

    /**
     * List the books on loan given the member ID.
     * 
     */
    @RequestMapping(value="/loanedBooks/{memberId}", method=RequestMethod.GET)
    public String displayLoanedBooks(@PathVariable String memberId, ModelMap model){        
        List<Book> books = bookDOAImpl.getLoanedBooks(Integer.parseInt(memberId));
        model.addAttribute("message", "List the books on loan by "+memberId);
        model.addAttribute("books", books);
        return "displayBooks";
    }

    /**
     * List all books on loan by all members.
     * 
     */
    @RequestMapping(value="/loanedBooks", method=RequestMethod.GET)
    public String displayLoanedBooks(ModelMap model){       
        List<Book> books = bookDOAImpl. getLoanedBooks();
        model.addAttribute("message", "List the books on loan");
        model.addAttribute("books", books);
        return "displayBooks";
    }

}

的web.xml

<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping> 

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml, /WEB-INF/application-context.xml</param-value>
    </context-param>

</web-app>

应用context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" 
    default-autowire="byName">

    <context:annotation-config/>
    <context:component-scan base-package="com.LibraryManagement.*" />
    <context:property-placeholder location="classpath:prop.properties"/>
</beans>

MVC-调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd" > 

    <context:component-scan base-package="com.LibraryManagement.*" />
    <mvc:annotation-driven validator="validator"/>    
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"></bean>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.thehit</groupId>
    <artifactId>WebLibraryManagement</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>WebLibraryManagement Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <jdk.version>1.7</jdk.version>
        <org.springframework.version>4.0.0.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.dbunit</groupId>
            <artifactId>dbunit</artifactId>
            <version>2.4.9</version>
        </dependency>

        <dependency>
            <groupId>com.github.springtestdbunit</groupId>
            <artifactId>spring-test-dbunit</artifactId>
            <version>1.1.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.0.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>  

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>

        <!--
            Core utilities used by other modules.
            Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Expression Language (depends on spring-core)
            Define this if you use Spring Expression APIs (org.springframework.expression.*)
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!-- 
            Bean Factory and JavaBeans utilities (depends on spring-core)
            Define this if you use Spring Bean APIs (org.springframework.beans.*) 
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!--
            Application Context (depends on spring-core, spring-expression, spring-aop, spring-beans) 
            This is the central artifact for Spring's Dependency Injection Container and is generally always defined
        -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>   

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.0.1.Final</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
</dependencies>

  <build>
        <finalName>WebLibraryManagement</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

任何帮助都会非常感激

0 个答案:

没有答案