вторник, 9 августа 2011 г.

Немного о Tiles часть 2

Что же такое Tiles2 и как с ним можно работать (один из вариантов) я описал в 1 статье - Немного о Tiles. Тут я опишу второй вариант работы с этим фреймворком на пару с Spring MVC. Библиотеки которые мне понадобились можно посмотреть в первом посте. В чем координальное отличие части 1 от части 2? - Только в том, что адреса и пути к страницам прописываются в property файле.

Классы контроллера я разместил в пакете com.company.web:
AboutController.java
FriendsController.java
HomeController.java
OfficeController.java
WelcomeController.java
Ну и для примера один из контроллеров (Все остальные оформлены также)
package com.company.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FriendsController {

    @RequestMapping("/friends.htm")
    public String redirect()
    {
        return "friends";
    }
}

Все jsp я сложил в /web-inf/jsp папку
Тайлс лежат в  /web-inf/tiles
библиотеки тегов в /web-inf/tags/jstl
Так выглядит страничка baseLayout.jsp
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title><tiles:insertAttribute name="title" ignore="true" />title>
    <head>
    <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>
    <body>
<html>

Ну и сам tiles-defs.xml:

<1.0" encoding="UTF-8" ?>

       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>

  <definition name="baseLayout" template="/WEB-INF/tiles/baseLayout.jsp">
      <put-attribute name="title"  value="Template"/>
      <put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/>
      <put-attribute name="menu"   value="/WEB-INF/tiles/menu.jsp"/>
      <put-attribute name="body"   value="/WEB-INF/tiles/body.jsp"/>
       <put-attribute name="footer"   value="/WEB-INF/tiles/footer.jsp"/>
  <definition>
  
  <definition name="welcome" extends="baseLayout">
      <put-attribute name="title"  value="Welcome"/>
      <put-attribute name="body"   value="/home.htm"/>
  <definition>
  
  <definition name="friends" extends="baseLayout">
      <put-attribute name="title"  value="Friends"/>
      <put-attribute name="body"   value="/WEB-INF/jsp/friends.jsp"/>      
  <definition> 
<definition name="office" extends="baseLayout">
      <put-attribute name="title"  value="Office"/>
      <put-attribute name="body"   value="/WEB-INF/jsp/office.jsp"/>      
  <definition> 
<tiles-definitions>
web.xml:
xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 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_4.xsd">

    <display-name>eximiusdisplay-name>
    <servlet>
        <servlet-name>dispatcherservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <load-on-startup>1load-on-startup>
    <servlet>
    <servlet-mapping>
        <servlet-name>dispatcherservlet-name>
        <url-pattern>*.htmurl-pattern>
    <servlet-mapping>
    <welcome-file-list>
        <welcome-file>redirect.jspwelcome-file>
    <welcome-file-list>
    <jsp-config>
        <taglib>
            <taglib-uri>Coretaglib-uri>
            <taglib-location>/WEB-INF/tags/jstl/c.tldtaglib-location>
        <taglib>
    <jsp-config>
<web-app>

ну и собственно dispatcher-servlet.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: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/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <bean name="/home.htm" 
        class="com.company.web.HomeController">
    <bean>

     <bean id="viewResolver" 
         class="org.springframework.web.servlet.view.ResourceBundleViewResolver" 
         p:basename="views" >
        <property name="order" value="0" />
    <bean>

   <bean id="viewResolver2" 
           class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="1" />
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    <bean>
    
    <context:component-scan base-package="com.company.web" />
    
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" 
        p:definitions="/WEB-INF/tiles-defs.xml" />    
    
beans>
Нас редеректит с welcome-file указаной в web.xml на страницу приветствия.
Ну а в самом property файл прописано какой контроллер какую вьюшку обрабатывает. Прописать это надо каким то следующим образом
welcome.(class)=org.springframework.web.servlet.view.tiles2.TilesView
welcome.url=welcome

friends.(class)=org.springframework.web.servlet.view.tiles2.TilesView
friends.url=friends

office.(class)=org.springframework.web.servlet.view.tiles2.TilesView
office.url=office

about.(class)=org.springframework.web.servlet.view.JstlView
about.url=/WEB-INF/jsp/about.jsp












Комментариев нет:

Отправить комментарий