2012-07-15 139 views
0

我想添加Apache瓷磚到一個簡單的Spring MVC webapp我玩,我似乎無法得到它的工作(它沒有瓷磚工作)。我做的任何請求都會返回400個錯誤的請求,日誌中沒有任何內容(甚至設置爲DEBUG),所以我不確定從何處開始刪除。據我可以告訴控制器映射的方法從來沒有被調用,因爲有記錄在那裏,它不會出現在日誌中(加之前,我會得到大量來自春的調試信息關於解決映射到控制器之前它實際上被稱爲 - 現在不出現)。彈簧mvc 3與瓷磚2給400請求不好

我的配置文件如下(所有在/ WEB-INF /): web.xml中:

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

    <display-name>myapp</display-name>  

    <!-- Enable escaping of form submission contents --> 
    <context-param> 
     <param-name>defaultHtmlEscape</param-name> 
     <param-value>true</param-value> 
    </context-param>   
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value> 
    </context-param> 

    <filter> 
     <filter-name>CharacterEncodingFilter</filter-name> 
     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
     <init-param> 
      <param-name>encoding</param-name> 
      <param-value>UTF-8</param-value> 
     </init-param> 
     <init-param> 
      <param-name>forceEncoding</param-name> 
      <param-value>true</param-value> 
     </init-param> 
    </filter> 
    <filter> 
     <filter-name>HttpMethodFilter</filter-name> 
     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>CharacterEncodingFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <filter-mapping> 
     <filter-name>HttpMethodFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

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

    <!-- Handles Spring requests --> 
    <servlet> 
     <servlet-name>myapp</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>myapp</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <session-config> 
     <session-timeout>10</session-timeout> 
    </session-config> 
</web-app> 

MYAPP-servlet.xml中:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 
    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-3.1.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

    <!-- The controllers are autodetected POJOs labeled with the @Controller 
     annotation. --> 
    <context:component-scan base-package="com.myapp.controller" 
     use-default-filters="false"> 
     <context:include-filter expression="org.springframework.stereotype.Controller" 
      type="annotation" /> 
    </context:component-scan> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
     up static resources --> 
    <mvc:resources location="/resources/" mapping="/resources/**" /> 

    <!-- Allows for mapping the DispatcherServlet to "/" by forwarding static 
     resource requests to the container's default Servlet --> 
    <mvc:default-servlet-handler /> 

    <mvc:annotation-driven /> 

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

tiles.xml

<tiles-definitions>  
    <definition name="product_detail" template="/WEB-INF/layout/detail.jsp"> 
     <put-attribute name="header" value="/WEB-INF/view/header.jsp" /> 
     <put-attribute name="banner" value="" /> 
     <put-attribute name="body" value="/WEB-INF/view/product.jsp" /> 
     <put-attribute name="footer" value="/WEB-INF/view/footer.jsp" /> 
    </definition> 
</tiles-definitions> 

該佈局只包含一個div,用於包裝標籤的每個零件。所有視圖都包含簡單的代碼,如標題或div。

且用於控制器 ProductController.java:

@Controller 
@RequestMapping("/product") 
public class ProductController { 

    protected Logger logger = Logger.getLogger(getClass()); 

    @Autowired 
    private ProductService productService; 

    @RequestMapping(value = "/{id}", method = RequestMethod.GET) 
    public ModelAndView getProduct(@PathVariable Long id) { 
     logger.info("GET product " + id); 
     Product product = productService.find(id); 
     ModelAndView mv = new ModelAndView("product_detail", "product", product); 
     return mv; 
    } 
} 

與行家嵌入Tomcat插件和去到localhost部署此:8080/MyApp的/產物/ 1只是給HTTP 400代碼而沒有任何其他指示的東西出錯。數據庫中有一個產品與該ID以及控制器的所有內容一起工作,因爲我在添加瓷磚之前嘗試了它。

對不起,代碼丟失,但我不能得到這個工作一段時間了,我不知道還有什麼要嘗試或從哪裏開始調試。

有什麼方法可以強制記錄當400個錯誤的請求被返回時的問題是什麼?

回答

0

您錯過了servlet配置中對myapp-servlet.xml的引用。

<!-- Handles Spring requests --> 
<servlet> 
    <servlet-name>myapp</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/spring/myapp-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>myapp</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 
+0

不幸的是,這並沒有解決它。我很確定它會加載它,因爲設置不良路徑會導致FileNotFound錯誤。另外我還有一個印象:如果你沒有提供它,它會在WEB-INF中自動查找servletname-servlet.xml,我認爲這是以前的情況,因爲在加載容器時沒有發生錯誤。啓動日誌還包含有關映射控制器方法的消息,它假定它找到了我定義Controller掃描的servlet.xml文件。 – 2012-07-26 17:23:39