Spring MVC 3 和 Apache Tiles 2,但没有控制器

发布于 2024-11-25 21:51:38 字数 5933 浏览 1 评论 0原文

我正在尝试让 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";
    }
}

我知道我在这里丢弃了很多代码,对此我深表歉意。我已经这样做好几天了,我已经失去了耐心。提前致谢。

I am trying to get Spring MVC 3 and Apache Tiles 2 to play nice together. I can get simple pages to display and those pages have been built with Tiles templates, but for the life of me I cannot get my controller to be called. I would appreciate any help.

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";
    }
}

I know I have dumped a lot of code here and I apologize for that. I have been at this for days and I am running out patience. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-12-02 21:51:38

您的 RequestMapping 应该只是“/index”或“/index2”而不是“/index.html”。您的servletmapping“*.html”将确保Spring的DispatcherServlet处理该调用。

Your RequestMapping should just be "/index" or "/index2" not "/index.html". Your servletmapping "*.html" will ensure that the Spring's DispatcherServlet handles the call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文