Tiles 3和spring MVC 4 - 错误404

时间:2014-08-07 08:44:47

标签: java spring maven spring-mvc tiles-3

我正在使用maven,spring,hibernate,tiles ....来发布我的网页.... 我的问题是当我在pom文件中添加tile的依赖项并运行" mvn clean package" 我总是得到错误404,无论我试图访问哪个页面。当我删除这个依赖关系和chage for" InternalViewResolver"一切正常。 需要一些帮助。 这是我的档案。

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>

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


<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>

<!-- <listener>
<listener-class>org.apache.tiles.web.startup.TilesListener</listener-class>
</listener> -->

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

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

<welcome-file-list>
    <welcome-file>home</welcome-file>
</welcome-file-list>

root context:

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

</beans>

servlet上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:beans="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven/>
<context:annotation-config/>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->


<mvc:resources mapping="/resources/**" location="/resources/" />

    <context:component-scan base-package="com.controller.*, com.models.*" />



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

        <beans:bean id="viewResolver" 
                class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
                <beans:property name="viewClass"> 
                        <beans:value> 
                                org.springframework.web.servlet.view.tiles3.TilesView 
                        </beans:value> 
                          </beans:property> 
        </beans:bean> 


    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/myonlineshopdb"/>
    <property name="username" value="stacy"/>
    <property name="password" value="stacy"/>
    </bean> 

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan"
            value="com.entities" />
        <property name="hibernateProperties">
            <props>
                <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean   class="org.springframework.dao.annotation.
     PersistenceExceptionTranslationPostProcessor"/>      

    </beans>

tiles.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
   "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

<tiles-definitions>
<definition name="management" template="/WEB-INF/views/shared/management.jsp">
    <put-attribute name="left" value="/WEB-INF/views/shared/left.jsp"/>
    <put-attribute name="body" value=""/>
</definition>

<definition name="brands" extends="management">
    <put-attribute name="body" value="/WEB-INF/views/management/brands.jsp"/>
</definition>

<definition name="home" template="/WEB-INF/home.jsp"/>
</tiles-definitions>

两个完全没有被调用的控制器,而tile项依赖项包含在我的项目中:

package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/home")
public class Home {

@RequestMapping("/")
public String welcome(){
    return "home";
}

}


package com.controller.management;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.servlet.ModelAndView;  
import com.dao.BrandsDao;

@Controller  
@RequestMapping("/management") 
public class BrandsController{  

@Autowired
private BrandsDao brandsDao;

@RequestMapping(value="/brands")  
public ModelAndView showBrands() {  

System.out.println("from controller");  
return new ModelAndView("brands", "brands", brandsDao.getAllBrandValues());  
}  
}  

1 个答案:

答案 0 :(得分:0)

我有类似的问题,我没有扫描每一行,

  • 请记住,至少需要一个Spring 3.2.x(我认为是3.2.7)版本才能支持拼贴3我知道你提到了第4弹,但只是想把它放在核对清单上。

**最重要的是确保你有以下罐子

  1. 公地BeanUtils的-1.8.0
  2. 公地消化器-2.0
  3. JCL-过SLF4J-1.7.6
  4. SLF4J-API-1.7.6
  5. 由于这篇文章是最新的,正如你所说的使用Spring 4.x.确保你的依赖是(maven)

    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>${tiles-version}</version>
    </dependency>
    

    如果有帮助,请告诉我。感谢。