做template时配置文件的解析

1.页面中有Page.action?returnPage=scene
2.PageAction.java 
这里是把配置文件加载进来了。
这里的包simple.xml是用来解析xml文件。
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.stream.Format;
import org.simpleframework.xml.stream.HyphenStyle;
import org.simpleframework.xml.stream.Style;
@Override
public String execute() throws Exception {
URL url = this.getClass().getClassLoader().getResource(
				"page-config.xml");
if (url == null) {
	return "error";
}

String path = url.toString().startsWith("file:") ? url.toString()
		.substring(5) : url.toString();
File file = new File(path);
Style style = new HyphenStyle();
Format format = new Format(style);
Serializer serializer = new Persister(format);
PageConfigFactory pageFactory = serializer.read(
		PageConfigFactory.class, file);
PageConfig pageConfig = pageFactory.getPageConfigByName(returnPage);
...这里页面中的returnPage=scene,得调用下面的PageConfigFactory.java
...
}

 

3.PageConfigFactory.java
public class PageConfigFactory {
	
@ElementList(required = false)
List<PageConfig> pageConfigs = new ArrayList<PageConfig>();

public List<PageConfig> getPageConfigs() {
	return pageConfigs;
}

public void setPageConfigs(List<PageConfig> pageConfig) {
	this.pageConfigs = pageConfig;
}

public PageConfig getPageConfigByName(String name){
	PageConfig pageConfig = getPageConfig(name);
	if (pageConfig == null){
		pageConfig = getPageConfig("default");
	}
	return pageConfig;
}

private PageConfig getPageConfig(String name){
	for (PageConfig pageConfig : pageConfigs) {
		if (name.equals(pageConfig.getPageName())){
			return pageConfig;
		}
	}
	return null;
}

 

4.PageConfig.java
@Root(name = "pageconfig")
public class PageConfig {
@Attribute
private String pageName;
@Element(required = false)
private boolean pageLogin;
@Element(required = false)
private String pageType;
@Element(required = false)
private String pageSorting;
@Element(required = false)
private String pageSearchBy;
@Element(required = false)
private String pageSearchValue;
@Element(required = false)
private boolean pageFuzzySearch;
@Element(required = false)
private boolean showTotalMovieScenes;

public String getPageName() {
	return pageName;
}

public void setPageName(String pageName) {
	this.pageName = pageName;
}

 

5.page-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<page-config-factory>
   <page-configs class="java.util.ArrayList">
      <pageconfig page-name="default">
         <page-login>false</page-login>
         <page-type></page-type>
         <page-sorting></page-sorting>
         <page-search-by></page-search-by>
         <page-search-value></page-search-value>
      </pageconfig>
      <pageconfig page-name="index">
      	 <page-type>movie</page-type>
      	 <page-sorting>date</page-sorting>
      	 <page-size>5</page-size>
      </pageconfig>
      <pageconfig page-name="scene">
      	 <page-type>scene</page-type>
      	 <page-size>5</page-size>
      </pageconfig>
      <pageconfig page-name="playerscene">
      	 <page-login>true</page-login>
      	 <show-scene>true</show-scene>
      	 <view-video>true</view-video>
      </pageconfig>
      <pageconfig page-name="playertourscene">
      	 <show-scene>true</show-scene>
      	 <view-tour>true</view-tour>
      </pageconfig>
   </page-configs>
</page-config-factory>

 

你可能感兴趣的:(xml)