首页 > 解决方案 > 将 Tiles 与 Springmvc 集成时出现错误 404

问题描述

我第一次使用 Tiles,但在与 Spring 4 集成时遇到问题。我正在尝试将应用程序部署到 Tomcat 服务器 8。当我尝试访问 jsp 时,我得到“HTTP 404”。

这是我的文件:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Spring-Hibernate</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>tiles</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>tiles</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

瓷砖-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 


    <mvc:annotation-driven/>
    <context:annotation-config />

    <context:component-scan  
        base-package="com.app.controller" />  

    <bean id="viewResolver"  
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">  
        <property name="viewClass">  
            <value>  
                org.springframework.web.servlet.view.tiles3.TilesView  
            </value>  
        </property>  
    </bean>  
    <bean id="tilesConfigurer"  
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">  
        <property name="definitions">  
            <list>  
                <value>/WEB-INF/tiles.xml</value>

            </list>  
        </property>  
    </bean> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3306/springdb"></property>
            <property name="password" value="passw"></property>
            <property name="username" value="root"></property>
            <property name="suppressClose" value="true"></property>
        </bean>

    <bean id="dataSourceProxy"
        class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
        p:targetDataSource-ref="dataSource" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
        p:dataSource-ref="dataSourceProxy" p:configLocation="classpath:/hibernate.cfg.xml" />


    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory" />

</beans>

瓷砖.xml:

<definition name="base-template" template="/WEB-INF/template/template.jsp">
    <put-attribute name="title"   value="Spring MVC Tiles Integration" />
    <put-attribute name="content" value="" />
    <put-attribute name="header"  value="/WEB-INF/template/header.jsp" />
    <put-attribute name="footer"  value="/WEB-INF/template/footer.jsp" />
</definition>

<definition name="customers" extends="base-template">
    <put-attribute name="content" value="/WEB-INF/views/customers.jsp" />
</definition>

客户控制器

    @Controller
    public class CustomerController {

        @Autowired
        private CustomerService cService;

        @RequestMapping(value = "/customers", method = RequestMethod.GET)
        public ModelAndView getAllCustomers(Model model) {

            return new ModelAndView("customers", "customersList", cService.listCustomers());
        }
}

我的 pom.xml 上的图块依赖项:

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-jsp</artifactId>
    <version>3.0.8</version>
</dependency>

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-servlet</artifactId>
    <version>3.0.8</version>
</dependency>
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-template</artifactId>
    <version>3.0.8</version>
</dependency>
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-el</artifactId>
    <version>3.0.8</version>
</dependency>

这是我的错误:

WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/application/customers] in DispatcherServlet with name 'tiles'

我的 WebContent 文件夹如下所示

标签: javaspring-mvctilesapache-tilestiles-3

解决方案


您的错误让我认为您可能输入了错误的 URL。

您收到 404 错误是因为您正在调用一个没有映射的 URL。尝试只/customers在您的 URL 中使用,看看是否有效。


推荐阅读