0

我正在尝试将 Apache Tiles 添加到我正在玩的一个简单的 Spring MVC webapp 中,但我似乎无法让它工作(它在没有 Tiles 的情况下工作)。我提出的任何请求都会返回 400 个错误请求,日志中不会出现任何内容(甚至设置为 DEBUG),所以我不确定从哪里开始调试。据我所知,控制器映射的方法永远不会被调用,因为那里有登录并且它没有出现在日志中(加上在此之前我会从 spring 中获得很多关于解析到控制器的映射的调试信息它之前实际上被称为 - 现在没有出现)。

我的配置文件如下(都在/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>

瓷砖.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;
    }
}

使用 maven 嵌入式 tomcat 插件部署它并转到 localhost:8080/myapp/product/1 只会给出 HTTP 400 代码,而没有任何其他迹象表明出现问题。数据库中有一个具有该 ID 的产品,并且从控制器向下的所有内容都可以正常工作,就像我在添加图块之前尝试过的那样。

很抱歉代码丢失,但我现在无法让它工作一段时间,我不知道还有什么可以尝试或从哪里开始调试。

当返回 400 错误请求时,是否有某种方法可以强制记录问题所在?

4

1 回答 1

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>
于 2012-07-26T08:33:46.807 回答