带有Tiles的Spring mvc使用.xhtml文件而不是.jsp

时间:2014-06-11 13:33:08

标签: spring jsf tiles

在我的项目中,我需要使用带弹簧的tile并使用.xhtml作为我的视图页面,但我不能使项目工作为whit.xhtml文件,只能使用.jsp

我的tile.xml

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

    <definition name="base.definition" template="/WEB-INF/views/templates/layout.xhtml">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/WEB-INF/views/templates/header.xhtml" />
        <put-attribute name="menu" value="/WEB-INF/views/templates/menu.xhtml" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/views/templates/footer.xhtml" />
    </definition>

    <definition name="Contact" extends="base.definition">
        <put-attribute name="title" value="Spring MVC - Contact Manager" />
        <put-attribute name="body" value="/WEB-INF/views/contact.xhtml" />
    </definition>

    <definition name="/" extends="base.definition">
        <put-attribute name="title" value="Spring MVC - Home" />
        <put-attribute name="body" value="/WEB-INF/views/home.xhtml" />
    </definition>

</tiles-definitions>

我的servlet配置

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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/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 -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

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

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

<!--    <beans:bean id='viewResolver' class='org.springframework.web.servlet.view. -->
<!--    InternalResourceViewResolver'> -->
<!--    <beans:property name='prefix' value='/WEB-INF/'/> -->
<!--    <beans:property name='suffix' value='.xhtml' /> -->
<!--    </beans:bean> -->



    <!-- End Tiles Configuration -->

    <context:component-scan base-package="edu.youtube.tutorial" />



</beans:beans>

我的web.xml

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

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <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>

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

</web-app>

我的template.xhtml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:tiles="http://tiles.apache.org/tags-tiles">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Estatistica</title>
</h:head>
<h:body>
    <table border="1" cellpadding="2" cellspacing="2" align="center">
        <tr>
            <td height="30" colspan="2"><tiles:insertAttribute name="header" /></td>
        </tr>
        <tr>
            <td height="250"><tiles:insertAttribute name="menu" /></td>
            <td width="350"><tiles:insertAttribute name="body" /></td>
        </tr>
        <tr>
            <td height="30" colspan="2"><tiles:insertAttribute name="footer" /></td>
        </tr>
    </table>

</h:body>
</html>

和我的footer.xhtml(作为例子)

work tork borrk test

我的HomeController.java

package edu.youtube.tutorial.controller;

import java.text.DateFormat;
import java.util.Date;
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);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );

        return "/";
    }

}

所以,我找到了viewresolver

<!--    <beans:bean id='viewResolver' class='org.springframework.web.servlet.view. -->
<!--    InternalResourceViewResolver'> -->
<!--    <beans:property name='prefix' value='/WEB-INF/'/> -->
<!--    <beans:property name='suffix' value='.xhtml' /> -->
<!--    </beans:bean> -->

但没有效果

0 个答案:

没有答案