Spring MVC 3 и Apache Tiles 2, но без контроллера - PullRequest
0 голосов
/ 20 июля 2011

Я пытаюсь заставить Spring MVC 3 и Apache Tiles 2 хорошо играть вместе. Я могу отобразить простые страницы, и эти страницы были созданы с помощью шаблонов Tiles, но я не могу заставить мой контроллер вызываться. Буду признателен за любую помощь.

web.xml

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/web-application-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>tiles</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>tiles</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

web-application-context.xml

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

    <!-- Imports the configurations of the different infrastructure systems of the application -->
    <import resource="webmvc-context.xml" />

</beans>

webmvc-context.xml

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

    <!-- Static resource mapping (css, js, images, etc) -->
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resouces/" />

    <!-- Required stuff for autowiring of controllers -->
    <context:annotation-config />
    <context:component-scan base-package="com.tarigmaa.gem" />
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <!-- Static views (no controller) -->
    <!-- <mvc:view-controller path="/index.html" /> -->

    <!-- Tiles initialization -->
    <bean id="tilesConfigurer"
          class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"
          p:definitions="/WEB-INF/tiles-defs/templates.xml" />

    <bean id="tilesViewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver"
          p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" />

    <!-- Map URLs to controllers -->
    <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="alwaysUseFullPath" value="true" />

        <property name="mappings">
            <props>
                <prop key="/index.html">exampleController</prop>
                <prop key="/index2.html">exampleController</prop>
            </props>
        </property>
    </bean>

    <!-- Define the controllers -->
    <bean id="exampleController" class="com.tarigma.GEM.HomeController" />

</beans>

com.tarigma.GEM.HomeController

package com.tarigma.GEM;

import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome from /index.html");
        model.addAttribute ("title", "Nik");
        return "index";
    }

    @RequestMapping(value = "/index2.html", method = RequestMethod.GET)
    public String home2(Locale locale, Model model) {
        logger.info ("Welcome from /index2.html");
        model.addAttribute("title", "Jeff");
        return "index2";
    }
}

Я знаю, что здесь выложено много кода, и я прошу прощения за это. Я был в этом в течение нескольких дней, и у меня заканчивается терпение. Заранее спасибо.

1 Ответ

0 голосов
/ 21 июля 2011

Ваше RequestMapping должно быть просто "/ index" или "/ index2", а не "/index.html".Ваше отображение сервлетов "* .html" гарантирует, что Spring DispatcherServlet обрабатывает вызов.

...