首先,我们定义一个总体的tiles视图 /tiles/mainTemplate.jsp
首先使用:<tiles:getAsString name="title"/>打印tiles配置文件中定义的字符串属性title
然后是分别加载header,content,footer三个jsp页面,共同组成mainTemplate.jsp
< 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 控制类:
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
"-//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
< html >
< head >
</ head >
< body >
this is courseContent
</ body >
</ html >
spring配置文件:
<! 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
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
"-//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 " %>