View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-1-13 下午06:21:13
4    */
5   package gboat2.base.plugin.struts.convention;
6   
7   import java.util.LinkedHashMap;
8   import java.util.Map;
9   
10  import org.apache.struts2.convention.ConventionsServiceImpl;
11  import org.apache.struts2.osgi.DefaultBundleAccessor;
12  
13  import com.opensymphony.xwork2.config.entities.ActionConfig;
14  import com.opensymphony.xwork2.config.entities.PackageConfig;
15  import com.opensymphony.xwork2.config.entities.ResultTypeConfig;
16  import com.opensymphony.xwork2.inject.Inject;
17  import com.opensymphony.xwork2.util.ClassLoaderUtil;
18  
19  /**
20   * 扩展 Struts2 内置的 ConventionsServiceImpl,修复无法从 bundle 中获取 Action 类信息的问题。<br>
21   * 使用方法为在 struts.xml 中加入如下配置:<br>
22   * <code>
23   * &lt;bean name="gboat2"  type="org.apache.struts2.convention.ConventionsService" class="gboat2.base.plugin.struts.GboatConventionsServiceImpl"/&gt;<br>
24   * &lt;constant name="struts.convention.conventionsService" value="gboat2" /&gt;
25   * </code>
26   * @date 2012-1-13
27   * @author lysming
28   * @since 1.0
29   * @see org.apache.struts2.convention.ConventionsServiceImpl
30   */
31  public class GboatConventionsServiceImpl extends ConventionsServiceImpl {
32  	private String resultPath;
33  
34  	@Inject
35  	public GboatConventionsServiceImpl(@Inject("struts.convention.result.path") String resultPath) {
36  		super(resultPath);
37  		this.resultPath = resultPath;
38  	}
39  
40  	@Override
41  	public String determineResultPath(ActionConfig actionConfig) {
42  		if (actionConfig == null) {
43  			return resultPath;
44  		}
45  
46  		try {
47  			Class<?> clazz = null;
48  			try {
49  				clazz = ClassLoaderUtil.loadClass(actionConfig.getClassName(), this.getClass());
50  			} catch (ClassNotFoundException e) {
51  				clazz = DefaultBundleAccessor.getInstance().loadClass(actionConfig.getClassName());
52  			}
53  			return super.determineResultPath(clazz);
54  		} catch (ClassNotFoundException e) {
55  			throw new RuntimeException("Invalid action class configuration that references an unknown class named [" + actionConfig.getClassName() + "]", e);
56  		}
57  	}
58  
59      @Override
60      public Map<String, ResultTypeConfig> getResultTypesByExtension(PackageConfig packageConfig) {
61          Map<String, ResultTypeConfig> results = packageConfig.getAllResultTypeConfigs();
62          Map<String, ResultTypeConfig> resultsByExtension = new LinkedHashMap<String, ResultTypeConfig>();
63          resultsByExtension.put("jsp", results.get("dispatcher"));
64          resultsByExtension.put("html", results.get("dispatcher"));
65          resultsByExtension.put("vm", results.get("velocity"));
66          resultsByExtension.put("ftl", results.get("freemarker"));
67          return resultsByExtension;
68      }
69  
70  }