请求的资源在我的书店应用程序中不可用

时间:2016-04-21 22:54:34

标签: java xml spring hibernate spring-mvc

我一直收到这个错误,但不知道如何解决这个问题。这是我完整的web.xml文件,因为我认为这只是一个web.xml问题。或者是否有任何其他文件可能导致此请求的资源问题......

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/dispatcherServlet/servlet-context.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

这是我的bookcontroller代码

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

    @Autowired
    private BookService bookService;

    @RequestMapping(value = { "/", "/listBooks" })
    public String listBooks(Map<String, Object> map) {

        map.put("book", new Book());
        map.put("bookList", bookService.listBooks());

        return "/book/listBooks";
    }

    @RequestMapping("/get/{bookId}")
    public String getBook(@PathVariable Long bookId, Map<String, Object> map) {

        Book book = bookService.getBook(bookId);

        map.put("book", book);

        return "/book/bookForm";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String saveBook(@ModelAttribute("book") Book book,
            BindingResult result) {

        bookService.saveBook(book);

        /*
         * Note that there is no slash "/" right after "redirect:" So, it
         * redirects to the path relative to the current path
         */
        return "redirect:listBooks";
    }

    @RequestMapping("/delete/{bookId}")
    public String deleteBook(@PathVariable("bookId") Long id) {

        bookService.deleteBook(id);

        /*
         * redirects to the path relative to the current path
         */
        // return "redirect:../listBooks";

        /*
         * Note that there is the slash "/" right after "redirect:" So, it
         * redirects to the path relative to the project root path
         */
        return "redirect:/book/listBooks";
    }

这是我的root-context.xml代码

<context:component-scan base-package="np.com.mshrestha.bookstore" />

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!-- <property name="ignoreUnresolvablePlaceholders" value="true" /> -->
        <property name="locations">
            <list>
                <value>classpath:database.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.url}" p:username="${jdbc.username}" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />

        <property name="packagesToScan">
            <list>
                <value>np.com.mshrestha.bookstore.model</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

我也可以在我的控制台上看到此警告

WARNING: No mapping found for HTTP request with URI [/bookstore/] in DispatcherServlet with name 'dispatcherServlet'

请问如何在我的应用程序中一劳永逸地解决此错误。非常感谢

0 个答案:

没有答案