Struts2整合SiteMesh

阅读更多
1.导入Struts2的jar 和 sitemesh.jar 和 Struts2-sitemesh-plugin.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
sitemesh-2.4.2.jar
struts2-core-2.3.4.jar
struts2-sitemesh-plugin-2.3.4.1.jar
xwork-core-2.3.4.jar

2.在web.xml中配置 Struts sitemesh拦截器 注意有一定的顺序




	

	struts-cleanup
	org.apache.struts2.dispatcher.ActionContextCleanUp



	sitemesh
	com.opensymphony.module.sitemesh.filter.PageFilter

	

	struts2
	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter



	struts-cleanup
	/*



	sitemesh
	/*



	struts2
	/*



  
    index.jsp
  
  




3.在webroot下的descorators目录下建立 myDecorator.jsp 装饰器页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
<%@ taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    <decorator:title default="装饰器页面"/>
    
   	

  
  
  
  	
top


bottom


4.在web-inf 下建立 装饰器描述文件 decorators.xml


	
		/songs/*
	



5.建立 实体类 Song.java
private String songName;
	private String singer;
	private String songTime;
//get set


6.action
package com.sh.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.sh.entity.Song;

public class AllSongs extends ActionSupport {
	private List allSongs=new ArrayList();

	public List getAllSongs() {
		return allSongs;
	}

	public void setAllSongs(List allSongs) {
		this.allSongs = allSongs;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		Song song1=new Song();
		song1.setSongName("浪子心声");
		song1.setSinger("刘德华");
		song1.setSongTime("3:41");
		
		Song song2=new Song();
		song2.setSongName("中国话");
		song2.setSinger("SHE");
		song2.setSongTime("4:18");
		

		Song song3=new Song();
		song3.setSongName("丁香花");
		song3.setSinger("唐磊");
		song3.setSongTime("4:05");
		
		allSongs.add(song1);
		allSongs.add(song2);
		allSongs.add(song3);
		
		return SUCCESS;
	}	
}



7.配置  struts.xml



 
   
   
   		
   			/allSongs.jsp
   		
   



8.allSongs.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    Struts2AndStieMesh


  
  
  
    	

  • 歌名
  • 歌手
  • 时长




9.访问localhost:8080/Struts2AndSiteMesh/songs/allSongs.action 可以看到 页面中加入
  top 和bottom
  如果将 decorations.xml中的pattern 改成 /singer/*
  那上面的访问的页面就没有经过装饰器页面 修饰
  • Struts2AndSiteMesh.zip (3.5 MB)
  • 下载次数: 147

你可能感兴趣的:(SiteMesh,Struts2)