码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC集成Tiles使用方法

时间:2015-12-30 23:54:38      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:

首先,我们定义一个总体的tiles视图 /tiles/mainTemplate.jsp
首先使用:<tiles:getAsString name="title"/>打印tiles配置文件中定义的字符串属性title
然后是分别加载header,content,footer三个jsp页面,共同组成mainTemplate.jsp

<%@taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles"%>
<html>
<head>

<title><tiles:getAsString name="title"/></title>
</head>
<body>
the title is:<tiles:getAsString name="title"/><br>
<tiles:insert name="header"/><br>
<tiles:insert name="content"/><br>
<tiles:insert name="footer"/><br> 
</body>
</html>

header.content.footer页面如下:

<html>  
<head>  
</head>  
<body>  
this is from header  
</body>  
</html>  
  
  
<html>  
<head>  
</head>  
<body>  
this is from content  
</body>  
</html>  
  
  
<html>  
<head>  
</head>  
<body>  
this is from footer</body>  
</html>  

Spring MVC 控制类:

package Action;  
  
import java.util.ArrayList;  
import java.util.List;  
  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import model.Student;  
  
import org.springframework.validation.BindException;  
import org.springframework.web.servlet.ModelAndView;  
import org.springframework.web.servlet.mvc.AbstractCommandController;  
  
public class TilesHomeController extends AbstractCommandController {  
  
  
    protected ModelAndView handle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, BindException arg3) throws Exception {  
        return null;  
    }  
  
    protected ModelAndView handleRequestInternal(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {  
  
          
         return new ModelAndView("courseDetail","","");  
    }        
}    

 当我们运行上面这个控制器时候,系统会自动去寻找courseDetail.jsp这个文件,但我们还没有定义这个文件,所以,我们要这样写tiles-def.xml

<!DOCTYPE tiles-definitions PUBLIC  
       "-//Apache Software Foundation//DTD Tiles Configuration//EN"  
       "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">  
  
  
<tiles-definitions>  
  <definition name="template" page="/tiles/mainTemplate.jsp">  
    <put name="title" value="default title"/>  
    <put name="header" value="="/tiles/header.jsp"/>  
    <put name="content" value="/tiles/content.jsp"/>  
    <put name="footer" value="/tiles/footer.jsp"/>  
  </definition>  
    
<!--以courseDetail命名的配置,覆盖了模板中的title和content对应的页面-->  
  <definition name="courseDetail" extends="template">  
    <put name="title" value="courseDetail title"/>  
    <put name="content" value="/tiles/courseContent.jsp"/>  
  </definition>  
  
</tiles-definitions>  

/title/courseContent.jsp

<%@taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>  
<html>  
<head></head>  
<body>  
     this is courseContent  
</body>  
</html>  
 

spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >  
<beans>  
  
<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
 <property name="mappings">  
   <props>  
       <prop key="/tiles.mvc">TilesController</prop>  
   </props>  
 </property>  
</bean>  
  
  
<bean id="TilesController" class="Action.TilesHomeController">  
</bean>  
  
<!-- tiles页面解析器-->   
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  <property name="viewClass">  
    <value>org.springframework.web.servlet.view.tiles.TilesView</value>  
  </property>  
</bean>  
  
  
<!-- tiles配置器-->  
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles.TilesConfigurer">  
 <property name="definitions">  
   <list>  
     <value>/WEB-INF/train-def.xml</value>  
   </list>  
 </property>  
</bean>   
</beans>  

 

运行/title.mvc (mvc是我自定义的扩展名)我们可以看到如下结果:

courseDetail title
this is from header
this is courseDetail
this is from footer

 

如果我们需要在每一个页面的header部分显示一些我们从数据库里读取的数值,我们可以在不同的控制器中加入访问数据库的代码,但这样显得很啰唆,因为header部门在每个页面都是公用的,也就是说不同的页面只有content部分不同,这样,我们就会有很多冗余代码,为了解决这个问题,我们可以使用spring为我们提供的ComponentControllerSupport

package Action;  
  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
  
import model.Student;  
  
import org.apache.struts.tiles.ComponentContext;  
import org.springframework.web.servlet.view.tiles.ComponentControllerSupport;  
  
public class HeaderTilesController extends ComponentControllerSupport {  
  
  
    protected void doPerform(ComponentContext context, HttpServletRequest request,  
            HttpServletResponse response) throws Exception {  
         Student stu=new Student();  
         stu.setName("gaoxiang");  
         stu.setSex("male");  
         //tiles context中存放简单数据  
      context.putAttribute("name", stu.getName());  
         context.putAttribute("sex", stu.getSex());  
         //tiles context中存放对象  
      context.putAttribute("student", stu);  
         //request中存放数据  
      request.setAttribute("requestname", stu.getName()+"-request");  
         request.setAttribute("requestsex", stu.getSex()+"-request");  
    }  
  
}  

改写train-def.xml

<!DOCTYPE tiles-definitions PUBLIC  
       "-//Apache Software Foundation//DTD Tiles Configuration//EN"  
       "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">  
  
  
<tiles-definitions>  
  <definition name="template" page="/tiles/mainTemplate.jsp">  
    <put name="title" value="default title"/>  
    <put name="header" value=".header"/>  
    <put name="content" value="/tiles/content.jsp"/>  
    <put name="footer" value="/tiles/footer.jsp"/>  
  </definition>  
    
  <definition name="courseDetail" extends="template">  
    <put name="title" value="courseDetail title"/>  
    <put name="content" value="/tiles/courseContent.jsp"/>  
  </definition>  
    
  <definition name=".header" page="/tiles/header.jsp" controllerClass="Action.HeaderTilesController">  
  </definition>  
</tiles-definitions>  

改header.jsp

<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>  

 

Spring MVC集成Tiles使用方法

标签:

原文地址:http://www.cnblogs.com/zhujiabin/p/5090386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!